- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Strange issue- Move-VM moves the VM, however it also errors out!
The script below is used to storage vmotion Templates by first converting them into VMs, migrate them, then convert them back again to a template. Strangely, the Move-VM command starts a storage migration, as viewed in the vCenter GUI, however it also errors out!?? I cannot figure out why it is doing this! And it seems to be doing it with all my templates.
I tested this from a PowerShell command line and the PowerShell IDE- same result.
Function:
function Move-VMTemplate
{
# This function will migrate a template VM to the target datastore.
# The template is first converted to a VM,
# then migrated, then converted back to a template.
param(
[string] $TemplateName,
[string] $DatastoreName
)
# Try
# {
if($TemplateName -eq ""){Write-Host "Enter a Template name"}
if($DatastoreName -ne ""){$svmotion = $true}
# Firstly, verify we have a target datastore presented to the vCenter:
$TargetDS = Get-Datastore $DatastoreName -ErrorAction Stop
Write-Host "$LineItem2 Converting to VM." -ForegroundColor DarkCyan
# Convert the template to a VM.
$oTemplateAsVM = Set-Template -Template (Get-Template $TemplateName) -ToVM -ErrorAction Stop
Write-Host "$LineItem2 Migrate template to target datastore." -ForegroundColor DarkCyan
# Move the VM to the target datastore.
$MoveVMToDS = Move-VM -VM (Get-VM $oTemplateAsVM) -Datastore ($TargetDS) -Confirm:$false -ErrorAction Stop
# Move-VMThin (Get-VM $oTemplateAsVM) (Get-Datastore $fDatastore)
Write-Host "$LineItem2 Converting VM to template." -ForegroundColor DarkCyan
# Convert the VM back to a template.
(Get-VM $oTemplateAsVM | Get-View).MarkAsTemplate() | Out-Null
Write-Host "$LineItem2 Conversion done." -ForegroundColor Green
# }
<# Catch
{
Write-Host "$LineItem2 Error: $($PSItem.ToString())." -ForegroundColor Red
Write-Host "$LineItem2 Skipping template [$TemplateName]." -ForegroundColor Red
}
#> }
I took out the Try/Catch just to see if I could get more info.
The function is called with this line:
$MoveVMTemplate = Move-VMTemplate -template $TemplateName -datastore $NewDatastoreName
Here are the values of the variables:
$TemplateName
nashadow_RHEL6.5-x64_SOE_v1.1_mgt_t1_syd_vmdata_0001
$NewDatastoreName
mgt_t1_syd_vmdata_0003
$TargetDS
Name FreeSpaceGB CapacityGB
---- ----------- ----------
mgt_t1_syd_vmdata_0003 4,659.154 4,680.000
(Get-VM $oTemplateAsVM)
Name PowerState Num CPUs MemoryGB
---- ---------- -------- --------
nashadow_RHEL6.5-... PoweredOff 1 2.000
Template is currently sitting on datastore "mgt_t1_syd_vmdata_0001 ".
When the Move-VM line executes, a new task appears in the vCenter GUI showing the VM being migrated, and it completes without issues, however the script produces the following error:
$PSItem
Move-VM : 22/04/2023 6:01:33 PM Move-VM Operation is not valid due to the current state of the object.
At C:\Users\jmilano\Documents\PowerShell_Scripts\Datastores\2023-03-04 NetApp Datastore Migration\Storage-MigrateTemplates.ps1:102 char:23
+ ... oveVMToDS = Move-VM -VM (Get-VM $oTemplateAsVM) -Datastore ($TargetDS ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Move-VM], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.MoveVM
The "CS/DVD Drive 1" is set to "Client Device" and not connected. The Template has one 60GB hard disk.
I can manually migrate the template once converted to a VM, using the vCenter GUI without issues. I can then migrate it back to its original datastore.
$PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
5 1 14393 5582
$PSVersionTable
Name Value
---- -----
PSVersion 5.1.14393.5582
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14393.5582
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1