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
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
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
Thanks. Assigned points. Why can't I get the hostname like that?
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
That's exactly what I had done originally, the error I was receiving was because of the " " after -eq .
Thanks
And the Get-View wasn't needed.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference