VMware Cloud Community
DiStefan
Contributor
Contributor

Set-VM hangs when it's used in Invoke-Command

Hi!

In order of automating default tasks in a VDI environment I need to use Invoke-Command to change settings of a virtual machine running on a ESXi 5 Cluster.

The following script will not be finished:

$oSession = New-PSSession -ComputerName <fqdn to computer with vSphere PowerCLI>

[Object] $oResult = Invoke-Command -Session $oSession -ScriptBlock {
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
$VCsession = Connect-VIServer -Server <fqdn of vSphere Server> -User <account> -Password <Password> -WarningAction SilentlyContinue
Get-VM -Name <name of VM> | Set-VM -MemoryMB 3076 -Confirm:$false
Disconnect-VIServer -Server $VCsession -Confirm:$false
}
Remove-PSSession -Session $oSession -Confirm:$false

The RAM of the VM will set correctly and the vCenter shows the Task "Reconfigure virtual machine" with status "Completed", but Set-VM does not finish.

If I use the interactive shell everything works fine:

Enter-PSSession -ComputerName <fqdn to computer with vSphere PowerCLI>

[Object] $oResult = Invoke-Command -ScriptBlock {

Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue

$VCsession = Connect-VIServer -Server <fqdn of vSphere Server> -User <account> -Password <Password> -WarningAction SilentlyContinue
Get-VM -Name <name of VM> | Set-VM -MemoryMB 3076 -Confirm:$false

Disconnect-VIServer -Server $VCsession -Confirm:$false

}

Exit-PSSession

All Windows Servers running Windows Server 2008 R2 Standard, the vCenter Server is v5.0.0 Build 455964.

With best regards,

Stefan

Tags (2)
Reply
0 Kudos
3 Replies
LucD
Leadership
Leadership

Did you already try the Set-VM with the RunAsync switch ?

To clarify a bit, is the remote PSSession to the same machine as the one specified in the Invoke-VMScript ?


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

DiStefan
Contributor
Contributor

Thanks!

RunAsync solves the the hanging command. So I need to clarify in which way I can handle RunAsync in my scripts they are much more complex then the example script to rebuild the issue.

The communication path is like the following schema:

Managementsystem (executes Powershell script) --- <PSSession> ---> Adminserver (with vSphere PowerCLI installed) --- <https> ---> vCenter server

This communication path is necessary to fullfil central based process automation for several VDI environments on different locations.

With best regards,

Stefan

Reply
0 Kudos
LucD
Leadership
Leadership

Interesting setup, must be a challenge.

Btw did you already try using a transcript file in the code block ?

Perhaps that could show some further info why the Set-VM is hanging.


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

Reply
0 Kudos