VMware Cloud Community
slayer199
Contributor
Contributor
Jump to solution

PowerCLI Invoke-VMScript re-ip guest - how to get guest hostname variable

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!

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

0 Kudos
10 Replies
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
slayer199
Contributor
Contributor
Jump to solution

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.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
slayer199
Contributor
Contributor
Jump to solution

The PS1 runs on the recovery site SRM server which invokes the batch file that runs on each guest.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Then my script above should be ok.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
slayer199
Contributor
Contributor
Jump to solution

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?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

slayer199
Contributor
Contributor
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
slayer199
Contributor
Contributor
Jump to solution

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!

0 Kudos