我有一个脚本将虚拟机从一个数据存储移动到另一个数据存储。 我有其他逻辑来保护存储后端和结构免于超额订阅,并等待在特定时间提交新的请求(业务需求)。 我只是在移动数据存储而不是主机。 我有一个围绕Move-VM的try catch块,因为我想处理失败。 前两三次我运行脚本只发生了一些错误,并按我的预期处理。 最后5次移动会返回错误,但VM移动请求成功,如我在vCenter中看到的任务所示。 这项工作也成功完成。 Move-VM不使用RunAsync开关。 什么会导致Move-VM返回错误,但成功提交移动请求。
$VMs = Get-VM -Name $ComputerName $CurrentDataStores = Get-Datastore foreach ($VM in $VMs){ foreach ($store in $CurrentDataStores){ if ($store.name -eq "$Datastore"){ $rawDatastore = $store } if ($store.id -Match $VM.DatastoreIdList){ $VMDatastore = $store.name } } if ($VMDatastore -eq "$Datastore"){ Write-Output "$VM : is already on requested Datastore" } else{ try { Move-VM -VM $VM -Datastore $rawDatastore -ErrorAction Stop } catch { Write-OutPut "$VM : Unable to move VM to new Datastore" continue } } } Move-VMDatastore : <VM Name> : Unable to move VM to new Datastore At line:1 char:1 + Move-VMDatastore -ComputerName <VM Name> -Datastore <Move TO Datastore> + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Move-VMDatastore Move-VMDatastore : 12/24/2014 12:50:02 AM Move-VM Operation is not valid due to the current state of the object. At line:1 char:1 + Move-VMDatastore -ComputerName <ComputerName[]> -Datastore <Datastore> + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Move-VMDatastore
我对这个问题的理解是,在执行Move-VM时,PowerCLI运行一个asynchronous任务,获取任务,然后在其上运行Wait-Task 。
如果操作非常快,这个问题就会失败。 这是一个受过教育的猜测,因为这是你完成任务的Wait-Task时得到的确切例外。
所以基本上你的问题是你的VMware服务器太快了 …
解决方法是使用-RunAsync开关并自己实现正确的行为。 就像是:
$Task = Move-VM -VM $VM -Datastore $rawDatastore -ErrorAction Stop -RunAsync while($true) { switch ($task.State) { 'Success' { $Task.Result; break } 'Error' { throw $Task.ExtensionData.Info.Error.LocalizedMessage } Start-Sleep 5 } }
编辑:我已经看到了Stop-VM和Remove-VM完全相同的问题,并以相同的方式解决它们
以防万一谁遇到这个问题,这似乎是$任务variables不会自行更新。 就我而言,我必须这样做:
while($task.state -eq "Running") { Start-Sleep 5 $task = Get-Task -ID $task.id }
我也有这个问题。 这似乎是什么东西导致从电源连接到vCenter服务器的腐败。 每次我运行它后,事实产生相同的错误。 我必须closures我的命令窗口并打开一个全新的窗口来解决问题。