Hi all
Working on an environment w/500+ ESX hosts and more than 2000 VMs, so the performance of my PowerCLI code is essential.
I'm very fond of get-view and use it whenever possible, alas, sometimes you need additional info...and it's slow...
An example fetching additional CD/DVD-ROM info lacking from views (like ISO path) :
$vm = Get-View -ViewType VirtualMachine -Property Name, Config.Hardware.Device -Filter @{"Name"="myVM"}
measure-command{get-vm -name $vm.name} | fl TotalSeconds
11,6428104 seconds
measure-command{get-vm -id $vm.MoRef} | fl TotalSeconds
0,6073553 seconds
measure-command{get-cddrive -vm $vm.name} | fl TotalSeconds
11,5655317 seconds
measure-command{get-vm -id $vm.MoRef | get-cddrive} | fl TotalSeconds
0,9418873 seconds
My experience is that lookups using names instead of IDs are about 10 times slower.
So I end up having my code connecting to my vCenter server twice to get the cd drive...
Now the question:
Several functions, like Get-CDDrive, do have the -id parameter.
How do I use it? Can I find it within the view or do I have to start constructing it (hack) based on the VMs MoRef and the drive's device ID?
Related post: https://communities.vmware.com/thread/254257
Hi Christian,
(Get-CDDrive -vm $vv).count
3
So in order to always hit the right cd drive you would then pass the id parameter
g#11:32:06> Get-CDDrive -vm $vv -id 'VirtualMachine-vm-58370/3001'|fl *
IsoPath :
HostDevice :
RemoteDevice :
ParentId : VirtualMachine-vm-58370
Parent : diag001
Uid : ...
ConnectionState : NotConnected, GuestControl, NoStartConnected
ExtensionData : VMware.Vim.VirtualCdrom
Id : VirtualMachine-vm-58370/3001
Name : CD/DVD drive 2
It is string the id. so if you have 2,3,4..n cd cd drives and you wish not to get always full list of them, but you are interested in a particular one, you would use its 'id' . It is not a moref in anyway.
(Get-CDDrive -vm $vv)[1].id.gettype() -> string
$vv.extensiondata.moref.gettype() -> ManagedObjectReference (VMware KB: Looking up Managed Object Reference (MoRef) in vCenter Server )
get-view -id 'VirtualMachine-vm-58370/3001' -> this will not work there is no view like this where /3001 is the device key.
get-view -id 'VirtualMachine-vm-58370' this will as there is a view like that.
I hope that explains it.
By the way , you can still make it faster
greg#11:43:16> (measure-command {(get-view -id VirtualMachine-vm-58370).Config.Hardware.Device|?{$_ -is [vmware.vim.virtualcdrom]}}).totalseconds
0.2729135
without moref but with name:
greg#11:48:30> (measure-command{ (get-view -viewtype virtualmachine -Filter @{'name'='diag001'}).Config.Hardware.Device|?{$_ -is [vmware.vim.virtualcdrom]}}).totalseconds
0.8053804
Thanks for the response, Grzesiekk - several helpful comments here, but I realize my question didn't fully declare my chicken/egg.
What I want is an overview of VMs with mounted ISOs. My cmdlet starts by fetching a view of all VMs with names matching the users' input. Based on this VM-view I pick out connected [vmware.vim.virtualcdrom]'s. Unfortunately, I cannot find the ID of the CDRom device in this view, which seems to be the combo of the VM's ID and the device's key-property.
...However... I was using Get-CDDrive to find the mounted ISO-file, without realizing that I already had that info hidden in my VM-view... OMG I love to dig through these views with Format-Custom!
PowerCLI> foreach($dev in $vmview.Config.Hardware.Device){ if($dev -is [vmware.vim.virtualcdrom]){$dev.deviceinfo|format-custom} }
class Description
{
Label = CD/DVD drive 1
Summary = ISO [ABD-ESX-B1-local] Images/en_windows_7_professional_n_with_sp1_x86_dvd_u_677328.iso
DynamicType =
DynamicProperty =
}
So now my cmdlet is spends 9 seconds instead of 69, to list 74 mounted ISOs among my 2000+ VMs 🙂
Hi, i don't know which part is still unclear. If the question is about the cd-rom id then the rule is i guess : moref.trostring()+'/'+'devID'
so
$vmview.moref.tostring()
VirtualMachine-vm-58370
($vmview.moref.tostring()+'/'+$vv.ExtensionData.config.Hardware.Device[-5].key)
VirtualMachine-vm-58370/3002
So that's the ID of that cd-rom
get-cddrive -vm $vv -id ($vmview.moref.tostring()+'/'+$vv.ExtensionData.config.Hardware.Device[-5].key)
So that's how you get the cd-rom id i guess.