VMware Cloud Community
tdubb123
Expert
Expert

rejoin domain not rebooting after leaving domain

Trying to rejoin a machine to the domain. But I am only seeing one reboot on this. I think the script jumps directly from

Invoke-VMScript -vm $vm -GuestCredential $localcredential -ScriptType bat -ScriptText "netdom remove %computername% /domain:$domain /userd:$domainusername /passwordd:xxxxxx /reboot" -Verbose -RunAsync

to

Invoke-VMScript -vm $vm -GuestCredential $localcredential -ScriptType bat -ScriptText "netdom join %computername% /domain:$domain /userd:$domainusername /passwordd:yyyyyyy /reboot" -Verbose -RunAsync

and skips the first reboot. any idea?

$domain = "domain.com"

$domainusername = "administrator@$domain"

$domainpassword = "xxxxxx" | ConvertTo-SecureString -asPlainText -Force

$domcredential = New-Object System.Management.Automation.PSCredential($domainusername,$domainpassword)

$localadmin = "$computer\administrator"

$localadminpassword = "yyyyyyy" | ConvertTo-SecureString -AsPlainText -Force

$localcredential = New-Object System.Management.Automation.PSCredential($localadmin,$localadminpassword)

$vms = get-vm -name (get-content "c:\scripts\vms.txt")

ForEach ($vm in $vms)    {

Invoke-VMScript -vm $vm -GuestCredential $localcredential -ScriptType bat -ScriptText "netdom remove %computername% /domain:$domain /userd:$domainusername /passwordd:xxxxxx /reboot" -Verbose -RunAsync

sleep 30

Wait-Tools -VM $vm

write-host "rejoining $vm to the domain"

Invoke-VMScript -vm $vm -GuestCredential $localcredential -ScriptType bat -ScriptText "netdom join %computername% /domain:$domain /userd:$domainusername /passwordd:yyyyyyy /reboot" -Verbose -RunAsync

}

0 Kudos
13 Replies
LucD
Leadership
Leadership

I'm not a netdom specialist, did you already try with PowerShell (if available).

Remove-Computer -Credential domain1\admin -PassThru -Verbose

Restart-Computer -Confirm:$false

And for the join

Add-Computer -DomainName domain1 –credential domain1\admin -Restart -Force


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
tdubb123
Expert
Expert

when i use remove-computer its hanging

invoke-vmscript -vm server -guestcredential $localcrdential -scripttype bat "dir c:\"   - works

invoke-vmscript -vm server -guestcredential $localcredential -scripttype powershell "get-service" works

but

invoke-vmscript -vm server -guestcredential $localcredential -scripttype bat "remove-computer -credential $localcredential -passthru -verbose"

is hanging

0 Kudos
LucD
Leadership
Leadership

Did you also try the Remove-Computer remotely by using the ComputerName parameter and running the cmdlet from your station?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
tdubb123
Expert
Expert

yes no problem running it locally

remove-computer -computername servername -credential $domaincreds -passthru -force -restart

0 Kudos
LucD
Leadership
Leadership

Could it be that the Remove-Computer via Invoke-VMScript hangs due to a (confirmation) prompt?

Perhaps try adding the -Confirm:$false switch.

If that doesn't fix the issue perhaps use Start-Transcript/Stop-Transcript or redirect the output of the cmdlet to a file.

Perhaps that shows what causes the hang.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
tdubb123
Expert
Expert

i added the switch and still hanging. output does not show anything.

0 Kudos
LucD
Leadership
Leadership

This "Operation is not allowed in the current state" is strange.

What does this show?

(Get-VM -Name <VMname>).ExtensionData.Guest


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
tdubb123
Expert
Expert

Screen Shot 2018-12-11 at 1.03.42 PM.png

thanks

0 Kudos
LucD
Leadership
Leadership

The VMware Tools are running and ready to accept commands.

Are you still getting that state error when you run a simple command via Invoke-VMScript.
Perhaps try 1st with a simple dir or Get-Service


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
tdubb123
Expert
Expert

iu rebooted and its completed hung now

Screen Shot 2018-12-11 at 1.38.02 PM.png

i am able to run get-service and dir c:

0 Kudos
LucD
Leadership
Leadership

Are you sure UAC is not enabled in the guest OS on that VM?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
tdubb123
Expert
Expert

uac is off

0 Kudos
LucD
Leadership
Leadership

And are you seeing the same for all VMs that you try to leave/join the domain?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos