Hello Guys,
How did you improve one liner below I write for RDM ? I would like to see different approches.
Get-vm | Get-HardDisk -DiskType "rawphysical, rawvirtual" |
Select-Object @{"Name" = "VMName"; "Expression" = {$_.Parent}},
@{"Name" = "VMHost"; "Expression" = {$_.Parent.Host}},
@{"Name" = "VMFolder"; "Expression" = {$_.Parent.Folder}},
@{"Name" = "Datastore"; "Expression" = {(Get-VIObjectByVIView -MORef $_.extensiondata.backing.datastore).name}}
Best Regards,
Hello, vlife201110141-
Not too bad of a script -- a bit slower than it could be, though. If sticking with the traditional PowerCLI cmdlets Get-VM and Get-HardDisk, I probably would only change the last calculated property in the Select-Object statement to do a Get-View instead of Get-VIObjectByVIView, to help speed it up:
...
@{"Name" = "Datastore"; "Expression" = {(Get-View -Id $_.extensiondata.backing.datastore -Property Name).name}}
Or, to get more speed, I would write it as follows, taking fuller advantage of the potential speed of Get-View:
&{Get-View -ViewType VirtualMachine -Property Name, Runtime.Host, Parent, Config.Hardware.Device} | %{
$viewThisVM = $_
$viewThisVM.Config.Hardware.Device | ?{$_ -is [VMware.Vim.VirtualDisk]} | ?{"physicalMode","virtualMode" -contains $_.Backing.CompatibilityMode} | %{
New-Object -Type PSObject -Property @{
Name = $viewThisVM.Name
VMHost = (Get-View -Id $viewThisVM.Runtime.Host -Property Name).Name
VMFolder = (Get-View -Id $viewThisVM.Parent -Property Name).Name
Datastore = (Get-View -Id $_.Backing.Datastore -Property Name).Name
} ## end new-object
} ## end foreach-object
} ## end foreach-object
} | Select Name,VMHost,VMFolder,Datastore
I tested getting the RDM info for five (5) particular VMs with 41 RDMs total. The original way too 173 seconds, making the first change above cut that to 92 seconds, and using just Get-View dropped the run time to 28 seconds. What kinds of time differences do you see in your environment using the different versions?
Discussion moved from VMware vSphere™ Storage to VMware vSphere™ PowerCLI
Hello, vlife201110141-
Not too bad of a script -- a bit slower than it could be, though. If sticking with the traditional PowerCLI cmdlets Get-VM and Get-HardDisk, I probably would only change the last calculated property in the Select-Object statement to do a Get-View instead of Get-VIObjectByVIView, to help speed it up:
...
@{"Name" = "Datastore"; "Expression" = {(Get-View -Id $_.extensiondata.backing.datastore -Property Name).name}}
Or, to get more speed, I would write it as follows, taking fuller advantage of the potential speed of Get-View:
&{Get-View -ViewType VirtualMachine -Property Name, Runtime.Host, Parent, Config.Hardware.Device} | %{
$viewThisVM = $_
$viewThisVM.Config.Hardware.Device | ?{$_ -is [VMware.Vim.VirtualDisk]} | ?{"physicalMode","virtualMode" -contains $_.Backing.CompatibilityMode} | %{
New-Object -Type PSObject -Property @{
Name = $viewThisVM.Name
VMHost = (Get-View -Id $viewThisVM.Runtime.Host -Property Name).Name
VMFolder = (Get-View -Id $viewThisVM.Parent -Property Name).Name
Datastore = (Get-View -Id $_.Backing.Datastore -Property Name).Name
} ## end new-object
} ## end foreach-object
} ## end foreach-object
} | Select Name,VMHost,VMFolder,Datastore
I tested getting the RDM info for five (5) particular VMs with 41 RDMs total. The original way too 173 seconds, making the first change above cut that to 92 seconds, and using just Get-View dropped the run time to 28 seconds. What kinds of time differences do you see in your environment using the different versions?
This might be a bit faster in a larger environment
Get-View -ViewType VirtualMachine -Property Name,Config.Hardware.Device,Runtime.Host,Parent | where {$rdm = $_.Config.Hardware.Device | where{"physicalMode","virtualmode" -contains $_.Backing.CompatibilityMode}; $rdm} | Select @{"Name" = "VMName"; "Expression" = {$_.Name}}, @{"Name" = "VMHost"; "Expression" = {(Get-View -Id $_.Runtime.Host).Name}}, @{"Name" = "VMFolder"; "Expression" = {(Get-View -Id $_.Parent).Name}}, @{"Name" = "Datastore"; "Expression" = {(Get-View -Id $rdm.backing.datastore).name}}
But I admit it's less legible. Speed comes at a price I'm afraid.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Matt and Luc, thanks a lot guys. I have 50 hosts and over 400 VMs that's how I test.I put the scripts into $result = Measure-Command -Expression { }
to measure runtime.
Here is the results
Luc's script 56 sec 472 ms
Matt's script 54 sec 2 ms
The one I wrote 2 min 57 sec
To use "get-view" in every possible case is really making script blocks faster.That's what I am taking with me. Do you guys have this kind of other tips to make script blocks run faster ? The document like that would be lovely. I reviewed a couple of powershell books. Never seen this kind of tips. Get-view is well-known of course. I think Glenn discovered the magic of get-view... Thanks again...