VMware Cloud Community
astrolab
Contributor
Contributor
Jump to solution

Script to vmotion a specific VM

I am performing some benchmarking test on 2 VMs. Part of the test involves migrating a VM when certain I/O thresholds are reached. I wrote a simple script to vmotion one of these VMs :

$vm = Get-VM -Name Bench_VM | Get-View

$chost = $vm.Host.Name

if ($chost = ESX01.company.com)

{

Move-VM -VM Bench_VM -Destination ESX02.company.com

}

else

{

Move-VM -VM Bench_VM -Destination ESX01.company.com

}

I get an Error :

The term 'ESX01.company.com' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

What am I doing wrong?

Thank you

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

There are 2 problems with your script.

1) You can't get the hostname like that

2) The comparison in PowerShell uses the -eq operator.

This should work better

$vm = Get-VM -Name Bench_VM
$chost = ($vm | Get-VMHost).Name
if ($chost -eq "ESX01.company.com")
{
	Move-VM -VM Bench_VM -Destination ESX02.company.com
}
else
{
	Move-VM -VM Bench_VM -Destination ESX01.company.com
}

____________

Blog: LucD notes

Twitter: lucd22


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

View solution in original post

0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

There are 2 problems with your script.

1) You can't get the hostname like that

2) The comparison in PowerShell uses the -eq operator.

This should work better

$vm = Get-VM -Name Bench_VM
$chost = ($vm | Get-VMHost).Name
if ($chost -eq "ESX01.company.com")
{
	Move-VM -VM Bench_VM -Destination ESX02.company.com
}
else
{
	Move-VM -VM Bench_VM -Destination ESX01.company.com
}

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
astrolab
Contributor
Contributor
Jump to solution

Thanks. Assigned points. Why can't I get the hostname like that?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The following line

$vm = Get-VM -Name Bench_VM | Get-View

will store an object of type VirtualMachine in the variable $vm. If you look in the API reference you'll see that there is no property Host.Name in that object.

You could have done

$vm = Get-VM -Name Bench_VM
$chost = $vm.Host.Name
...

Come to think of it, this will in fact be faster than my previous solution.

____________

Blog: LucD notes

Twitter: lucd22


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

astrolab
Contributor
Contributor
Jump to solution

That's exactly what I had done originally, the error I was receiving was because of the " " after -eq .

Thanks

0 Kudos
LucD
Leadership
Leadership
Jump to solution

And the Get-View wasn't needed.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos