I have a script that is moving VM's from one datastore to another. I have other logic to protect the Storage backend and fabric from over subscription and to wait to submit new requests at specific times (Business requirement). I am only moving datastores and not hosts. I have a try catch block around the Move-VM as I want to handle the failure. The first two or three times I ran the script only a few errors occur and were handled as I expected. The last 5 times the move returns an error but the VM move request is successful as indicated by the task I see in vCenter. This job completes successfully as well. The Move-VM is not using the RunAsync switch. What would cause Move-VM to return an error but successfully submit the move request.
$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