vm-au-user
Contributor
Contributor

svmotion script wait-task error

I'm trying to get a script to work that will svmotion all machines listed in folder to a new datastore cluster and new host (which are in a separate cluster) once the migration is completed, power on the vm.

The error I'm getting

Wait-Task : Cannot validate argument on parameter 'Task'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.

At C:\temp\svmotion\final.ps1:11 char:18

+         Wait-Task -Task <<<<  $task

    + CategoryInfo          : InvalidData: (:) [Wait-Task], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.WaitTask

Here is my script

#Import Plugins and Connect to vCenter

Add-PSSnapin VMware.VimAutomation.Core

$creds = Get-Credential

$viserver = Read-Host "vCenter Server:"

Connect-VIServer -Server $viserver -Credential $creds

# Get a List of VM's from the relocation folder

$vmlist = get-vm -location "relocateme"

#Stoage vMotion each VM in selected cluster in a staged fashion

foreach($vm in $vmlist)

{

  Write-Host Relocating..."$vm"

  $task = Get-VM -Name $vm.Name | Move-VM -Datastore "Test Datastore" -Destination VMHOST1.NET

  Wait-Task -task $task

  Start-VM $vm.name

}

Once the script has exited get-vm -location "relocateme" shows the VM listed, however write-host $vmlist and write-host $vm both contain no data.

If I do write-host $task it is also blank. What am I missing?

The names of our VM's are long and contain special characters. Could this be the problem?. An example machine name is "AA-BB-CC-DD (Test Machine for Migrations)[T][+]"  (excluding the commas).

I'm using Powercli 5.1

Any help would be greatly appreciated.

0 Kudos
9 Replies
vm-au-user
Contributor
Contributor

I found the issue is definitely related to the VM Name, if I shorten the test machines name to about 8 Characters it works. The original name must be too long

Can I svmotion using some other identifier other than the VM name?

0 Kudos
LucD
Leadership
Leadership

That sounds very strange, the length of the displayname of the VM normally has nothing to do with this.

But you didn't use the RunAsync switch it seems

$task = Get-VM -Name $vm.Name | Move-VM -Datastore "Test Datastore" -Destination VMHOST1.NET -RunAsync

You don't get a Task object when you don't run the cmdlet in the background with the RunAsync switch


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vm-au-user
Contributor
Contributor

Thanks for your response.

Adding -RunAsync made no change

Attached is what I see when I run it. Notice when try to write out the contents of $task is comes back with 3 lines of blank space.

output.jpg

0 Kudos
LucD
Leadership
Leadership

Which vSphere version are you running this against ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vm-au-user
Contributor
Contributor

Vsphere 5.1 with ESXi hosts running 5.1.0. 799733

0 Kudos
vm-au-user
Contributor
Contributor

I've temporarily removed the $task = to try and get this working for 1 machine only, so this script is now this.

#Gets a list of Virtual Machines within the Folder that was identified before and assigns to a variable

$vms = get-vm -location "relocateme" | Select Name

Write-Host $vms

foreach ($vm in $vms) {

  Get-VM -Name $vm.Name | Move-VM -Datastore "Test Datastore" -Destination VMHOST1.NET -RunAsync

  }

I've renamed the machine and this is what I found:

AU-IT-SYD010 (Test Migration Machine)[TM][+]

(the original doesn't work)

AU-IT-SYD010

(works)

AU-IT-SYD010 (

(works)

AU-IT-SYD010 (Test Migration Machine)

(works)

AU-IT-SYD010 (Test Migration Machine)[

(fails!!!)

Get-VM : 9/05/2013 9:20:17 AM    Get-VM        The specified wildcard pattern is not valid: AU-IT-SYD010 (Test Migration Machine)[

At C:\temp\svmotion\svmotion7.ps1:14 char:8

So the problem is with the "[" symbol in the name of the machine.

Further more if I run the command with a "*" at the end it works.

Move-VM "AU-IT-SYD010 (Test Migration Machine)[TM][+]*" -Datastore "Test Datastore" -Destination VMHOST1.NET


Can I append a * to the end of each machine before the command is run?

0 Kudos
LucD
Leadership
Leadership

The square brackets are meta characters for the Name parameter, that probably explains why you're seeing the failures.

If you place the name in single quotes it should work.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vm-au-user
Contributor
Contributor

Thanks for your help, the one-liner works fine.

To do this in a loop, is there a way to update the contents of $vms to add single quote to before and after the name?

0 Kudos
LucD
Leadership
Leadership

You could use the single character meta character (?) , something like this

foreach ($vm in $vms) {

  Get-VM -Name $vm.Name.Replace('[','?').Replace(']','?') |

   Move-VM -Datastore "Test Datastore" -Destination VMHOST1.NET -RunAsync

}

But that is not a fool-proof method I admit.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos