Hello Guys,
I need some help on Powershell Script. As you all know most of the times we will not be able to login to the VMs using our Domain ID after reverting back VM to the old Snapshots due to SID mismatch. We are required to rejoin those VMs to Domain every time we revert back.
To Automate this task I have written a PS script. This can be run from local box and all we need to provide is VM name and Local Administrator and Domain User credentilas to rejoin. This will disjoin the box and rejoin and restart the Box.
Script :
--------
$cred = get-credential "Domain\"
$Domain = "pqa"
$usr = $cred.GetNetworkCredential().UserName
$pss = $cred.GetNetworkCredential().Password
foreach ($computer in $computers)
{
$localuser = get-credential "$computer\Administrator"
$computerObject = get-wmiobject Win32_ComputerSystem -Computer $Computer -Credential $localuser -Authentication 6 -Impersonation Impersonate
$rcw = $computerObject.UnjoinDomainOrWorkgroup()
if($rcw.ReturnValue -eq 0)
{
write-host "$Computer succesfully joined to the Workgroup" -Foregroundcolor "Green"
}
else
{
write-host "$Computer Failed to DisJoin from Domain" -Foregroundcolor "Red"
}
$rc = $ComputerObject.JoinDomainOrWorkGroup($Domain,$pss,$Domain + "\" + $usr,$null,3)
if($rc.ReturnValue -eq 0)
{
(gwmi win32_operatingsystem -ComputerName $computer -cred $localuser).Win32Shutdown(6) | out-null
write-host "$Computer succesfully joined to the Domain & Re-starting.." -Foregroundcolor "Green"
}
else
{
write-host "$Computer Failed to Join to the Domain" -Foregroundcolor "Red"
}
}
It ran succefully for few VMs but for most of the VMs I am getting the below error. It is saying unable to connect to the VM wmi from remote mchine.I am getting the below error. I am getting similar error when I run it on the particular VM.
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\Users\sreenivas.murthy\Desktop\Rejoindomain.ps1:58 char:36
+ $computerObject = get-wmiobject <<<< Win32_ComputerSystem -Computer $Computer -Credential $localuser -Authentica
tion 6 -Impersonation Impersonate
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
You cannot call a method on a null-valued expression.
At C:\Users\sreenivas.murthy\Desktop\Rejoindomain.ps1:59 char:51
+ $rcw = $computerObject.UnjoinDomainOrWorkgroup <<<< ()
+ CategoryInfo : InvalidOperation: (UnjoinDomainOrWorkgroup:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
VM Failed to DisJoin from Domain
You cannot call a method on a null-valued expression.
At C:\Users\sreenivas.murthy\Desktop\Rejoindomain.ps1:71 char:48
+ $rc = $ComputerObject.JoinDomainOrWorkGroup <<<< ($Domain,$pss,$Domain + "\" + $usr,$null,3)
+ CategoryInfo : InvalidOperation: (JoinDomainOrWorkGroup:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
VM Failed to Join to the Domain
Any assistance to fix this issue is greately appreciated. Please let me know if you need any additional information.
Thanks.