Quantcast
Channel: VMware Communities: Message List
Viewing all articles
Browse latest Browse all 144140

Re: Powershell Jobs not finishing when using Move-VM

$
0
0

Just wanted to bump this, since I had against the same problem myself recently and the workaround provided by jcouch was the only one that worked for me as well.

In my case it is a script that has to reboot a VM, but is iteself started via a remote powershell session. The script hung everytime during the execution of the PowerCLI cmdlet (any cmdlet really, tried executing one that needs no confirm, like get-vm or get-vmhost). I've tried following the advice posted here: http://communities.vmware.com/thread/449501, but the script stopped regardless of which credentials were used.

Here's the code, maybe someone will find it useful:

$pssession = New-PSSession -ComputerName vCenterServer

$creds = Get-Credential TESTDOMAIN\vmbooter

$sb = {

param($creds)

Add-PSSnapin VMware.VimAutomation.Core

Connect-VIServer vCenterServer -Credential $creds

$task = Restart-VM VMtobebooted -Confirm:$false -RunAsync

$taskrunning = $true

sleep 5

while ($RunTask)

{

$Tasks = Get-Task

    If ($Tasks -ne $null)

    {

        foreach ($Task in $Tasks)

            {

            if (($task.state -eq "Success") -or ($task.state -eq "Error"))

                {

                          $RunTask = $False

                Break

                }

            else

                {

                Write-Host "Waiting for task to finish..."

                }

            }

    }

    Else

    {

    break

    }

    start-Sleep 5

} # while END

Disconnect-viserver vCenterServer -Force -Confirm:$false

} # $sb scriptblock object END

Invoke-Command -Session $pssession -ScriptBlock $sb -ArgumentList $creds

Remove-PSSession $pssession


Viewing all articles
Browse latest Browse all 144140

Trending Articles