I'm running the following script as a Post-Power-On task to re-ip our VMs after SRM fails them over. I've been able to run this script successfully without the guest hostname variable (in testing, running it with the hostname assigned...one script per test VM), but I'm stumped on how to get the guest hostname variablized so whichever machine it runs on, it will take the hostname and use it in the GuestUser variable and in the -VM command. I've tried the Bolded section but it doesn't work.
$srv = Connect-VIServer 10.0.0.10 -User administrator -Password password
$Hostname = (get-wmiobject win32_computersystem).Name
$script = "%windir%\system32\cmd.exe /c c:\dr\drIPsettings.bat"
$HostUser = "root"
$HostPassword = "password"
$GuestUser = "$Hostname\Administrator"
$GuestPassword = "password"
$ScriptType = "bat"
Connect-VIServer 10.0.0.10 -Session $srv.SessionID
Invoke-VMScript -ScriptText $script -VM $Hostname -HostUser $HostUser -HostPassword $HostPassword -GuestUser $GuestUser -GuestPassword $GuestPassword -ScriptType $ScriptType
Thanks!
From the way you code the -VM parameter on the Invoke-VMScript cmdlet, it seems that the guest's Displayname is the same as the hostname.
Can't you just use a loop over the guests and then use the displayname
$script = "%windir%\system32\cmd.exe /c c:\dr\drIPsettings.bat" $HostUser = "root" $HostPassword = "password" $GuestPassword = "password" $ScriptType = "bat" Get-VM | %{ $GuestUser = $_.Name + "\Administrator" Invoke-VMScript -ScriptText $script -VM $_ -HostUser $HostUser -HostPassword $HostPassword -GuestUser $GuestUser -GuestPassword $GuestPassword -ScriptType $ScriptType }
You can code the Get-VM with more specific selection criteria or use a Where-Object after the Get-VM.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
From the way you code the -VM parameter on the Invoke-VMScript cmdlet, it seems that the guest's Displayname is the same as the hostname.
Can't you just use a loop over the guests and then use the displayname
$script = "%windir%\system32\cmd.exe /c c:\dr\drIPsettings.bat" $HostUser = "root" $HostPassword = "password" $GuestPassword = "password" $ScriptType = "bat" Get-VM | %{ $GuestUser = $_.Name + "\Administrator" Invoke-VMScript -ScriptText $script -VM $_ -HostUser $HostUser -HostPassword $HostPassword -GuestUser $GuestUser -GuestPassword $GuestPassword -ScriptType $ScriptType }
You can code the Get-VM with more specific selection criteria or use a Where-Object after the Get-VM.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, the hostname is the same as the VM name/display name. Where I'm getting stuck is that the PS script runs on the recovery site SRM server, but executes the invoke VMScript on each guest to re-IP the server. I'll try your modification. Thanks.
Does the script run on each guest or against each guest from a central location ?
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The PS1 runs on the recovery site SRM server which invokes the batch file that runs on each guest.
Then my script above should be ok.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
It failed. When I checked the logs it tried to run in x64 mode when it needs x32. Any ideas on how to specify the x32 mode?
Start PowerShell from the syswow64 directory.
$env:windir\syswow64\windowspowershell\v1.0\powershell.exe
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I think I have that part resolved...I pointed to the SysWOW64 in my redirect.bat. I'm no longer getting the x32 error.
I changed my redirect.bat so it looks like this:
@echo off C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" "& '%1'"
The only problem I have now is that it's timing out so I'm not sure it's pulling the server variable in when connecting.
The command I'm running in the SRM Post Power On Command is
c:\windows\system32\cmd.exe /c d:\redirect.bat d:\test1.ps1
Perhaps you can place a Start-Transcript and Stop-Transcript in the script.
That records everything that happens in a script to a file.
That way you could check what is going on.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That's what I ended up doing this morning. I indentifed the reason the script was stalling and fixed the issue.
Thanks a ton for your help, the script works!