我有一个将虚拟机从一个数据存储移动到另一个的脚本。我有其他逻辑来保护存储后端和结构免于过度订阅并等待在特定时间提交新请求(业务需求)。我只移动数据存储而不移动主机。我在 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