I'm writing a PowerShell script to sequentially move VMs off of a specified host and onto other cluster hosts using Live Migration.
The operative command is Move-VM (or Move-SCVirtualMachine), which is part of the VirtualMachineManager module. However, when I run this cmdlet, it always returns results to the console, which I want to suppress. I don't want to lose the information that the command returns.
The commands
Move-VM -VM $vmToMigrate -VMHost $destinationHost
and $move = (Move-VM -VM $vmToMigrate -VMHost $destinationHost)
will both return information to the console whether the result of the command is success or failure. My script incorporates logic on how to proceed depending on the results stored in $move
, so I want the results of Move-VM
, I just don't want them echoing to the console. I have tried
($move = (Move-VM -VM $vmToMigrate -VMHost $destinationHost)) | out-null
but none of the information returned by Move-VM
is captured; the value of $move
is null.
What's the best way of storing the results returned by Move-VM
, without completely discarding what the cmdlet returns, while suppressing its output to the console? I want to avoid having my script output this .
Thanks!!
Updates:
I have tried using -ErrorAction SilentlyContinue
or setting $ErrorActionPreference
, but if there is an error, then no output is stored to $move
. I'd still want the output of Move-VM
, so I can examine $move
and execute different logic based on different types of failures.
I realize that I am essentially trying to accomplish what VMM's Maintenance Mode does (evacuate all VMs from a host and migrate them elsewhere in the cluster). However, VMM 2012 SP1 attempts to migrate multiple VMs at a time to/from Server 2008R2 hosts, which can't support more that 1 in & 1 out migration at once. Thusly, the migrations fail, and VMM will try to re-execute another group of migrations. Essentially, VMM gets stuck in a failure loop. The script avoids that by programmatically only migrating one VM at a time.
You might want to wrap that command in a call to powershell, for example:
powershell -command { write-host "a" } | out-null
or
$out = powershell -command { write-host "a" }
you won't see the a
being printed.
But when you do
write-host "a" | out-null
you will see the a
you could redirect output to a file ? PS V3 only :
#Invalid command:
ping -rrfrfr *> $env:temp\test.txt
$? # last execution status will be false
ping google.com *>> $env:temp\test.txt
$? #true as command is valid
$resu=gc $env:temp\test.txt
$resu
Try:
$move = Move-VM -VM $vmToMigrate -VMHost $destinationHost 2> err.txt
This will return normal stdout output to $move
and errors will be put in a file. If you're not interested in the errors at all then:
$move = Move-VM -VM $vmToMigrate -VMHost $destinationHost 2> $null
Assigning to a variable is how I suppress unwanted output in my scripts. If that fails I always precede my function call with [void]
, like so:
[void]Move-VM -VM $vmToMigrate -VMHost $destinationHost
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.