I was doing some drs rules today and noticed( after all those years 😉 ) that i get only id’s for vms in output. So i can not see directly which vms for example are set to be together etc…
In order to quickly have the output with the names instead of ids i wrote this tiny enhancement:
[sourcecode language=”powershell”]
get-cluster XXX | Get-DrsRule | select Name, KeepTogether,Enabled, @{N=”VMnames”;E={ $_.Vmids|%{(get-view -id $_).name} } }
[/sourcecode]
*
I got a comment today under this post, so i thought i could just put the ‘normal/better’ version of this, since clearly this one does not make sort of logical sense, i mean of course it works, but it is really ugly because of the foreach of vmids , this is really ‘not the best idea’ to run get-view so many times if one can run it just once.
[sourcecode language=”powershell”]
get-cluster |Get-DrsRule | select Name, KeepTogether,Enabled, @{N=”VMnames”;E={ (get-view -id $_.vmids).name } }
[/sourcecode]
So this is more elegant plus it is actually way faster than the previous one.
They might look really similar to you, but in fact those are 2 different approaches.
The first one does a loop on an array of vmids and each iteration it asks get-view to fetch data.
The latter one is asking get-view just once, as we can pass to it ALL the vm ids. in the old day i would also do a param -property name for the get-view itself but these days (2020) the powercli is so much better optimized compared to 2012… then i dont see even a speed difference.
If the output is not optimized you can always use |ft -autosize at the end of the line.
For each of the virtual machine id i get the vm view and take its name.
You can do get-vm -id or get-view -id. If there is small amount of vms in your rules, that would not affect any performance of this command.
Tiny enhancement but saves some time on checking those ids manually.
3 comments
Awesome!
Thank you!
No problem, i modified that 1 line to make it 2020 ready hahaha 😉 since people are still reading this.