Automatic package restart doesn't start after adding VM to a domain

Dec 10, 2013 at 4:16 PM
Edited Dec 10, 2013 at 4:17 PM
Hi,

I'm doing the following within the context of the ChocolateyInstall.ps1
#Add computer to the domain
#$creds = Get-Credential
#Add-Computer -DomainName somedomain -Restart -DomainCredential $creds
Which works perfectly, the VM restarts, I dutifully login with the existing local computer account that I began package execution as (not a domain user account) and the BoxStarter package fails to kick off again.

Any thoughts?

I'll keep digging.

Lloyd
Coordinator
Dec 10, 2013 at 6:03 PM
Hi Lloyd

you want to remove the -restart param and use invoke-reboot instead. That's a boxstarter command that tells boxstarter to get things in order so the script will restart.

you will also want to have a check to call this only if the computer has not been joined otherwise it will add and reboot on every restart.

ill try to make self initiated restarts more clear on the docs.
Dec 10, 2013 at 9:43 PM
Matt,

Superb, thanks for the quick response. All makes sense, I'll drop in my snippet below once I've confirmed it works correctly.

Thanks again,

Lloyd
Dec 13, 2013 at 8:14 AM
Edited Dec 13, 2013 at 8:15 AM
Matt,

That worked perfectly, for completeness and for anyone else stumbling over this in the future here is the script I use to determine whether to join the domain.
        #Check if this workstation is a domain member, taken from http://msdn.microsoft.com/en-us/library/windows/desktop/aa394102(v=vs.85)
        $computer = Get-WMIObject win32_computersystem
        $memberStatus = ($computer).domainrole
        $domainName = "domainname"
        if ($memberStatus -eq 0 -or $memberStatus -eq 2)
        {
            #Add the computer to the domain if it's a 'Standalone Workstation' (0) or 'Standalone Server' (2) only
            Add-Computer -DomainName $domainName -Force
            #Add 'Domain Users' to the Local Administrators group on the computer
            $group = [ADSI]"WinNT://./Administrators,group"
            $group.Add("WinNT://$domainName/Domain Users,group")

            if (Test-PendingReboot){Invoke-Reboot}
        }
Coordinator
Dec 13, 2013 at 8:25 AM
Thats great. I may usethat in a function for the winconfig module to add a machine to a domain.
Dec 18, 2013 at 9:47 AM
A further modification to the above to prompt the user on whether to add the computer to the domain, useful if running installs disconnected from the domain.
#Check if this workstation is a domain member, taken from http://msdn.microsoft.com/en-us/library/windows/desktop/aa394102(v=vs.85)
$computer = Get-WMIObject win32_computersystem
$memberStatus = ($computer).domainrole
$domainName = "domainname"
if ($memberStatus -eq 0 -or $memberStatus -eq 2)
{
    #Prompt the user to select whether to join the domain
    $title = "Join $domainName domain?"
    $message = "Would you like to join this computer $($env:computername) to the domain $domainName?"
    $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
        "Select 1 to join this computer $($env:computername) to the domain $domainName."
    $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
        "Select 2 to NOT join this computer $($env:computername) to the domain $domainName."
    $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
    $deploymentOption = $host.ui.PromptForChoice($title, $message, $options, 1) 

    if ($deploymentOption -eq 0)
    {
        #Add the computer to the domain if it's a 'Standalone Workstation' (0) or 'Standalone Server' (2) only
        Add-Computer -DomainName $domainName -Force
        #Add 'Domain Users' to the Local Administrators group on the computer
        $group = [ADSI]"WinNT://./Administrators,group"
        $group.Add("WinNT://$domainName/Domain Users,group")

        if (Test-PendingReboot){Invoke-Reboot}
    }
}