How Can We Help?
Use lookup tables
Often you will need to look up values. To avoid to have to do this in each run through the loop, it often improves the execution time of your script, if you lookup those values once before the loop and store them in a hash table.
This snippet uses a Get-Datastore for each harddisk to find the canonicalname of a datastore.
1 2 3 4 5 6 |
Get-VM | Get-HardDisk | Select-Object @{N = 'VM'; E = {$_.Parent.name}}, Name, @{N = 'CanonicalName'; E = { $dsName = $_.Filename.Split(']')[0].TrimStart('[‘) (Get-Datastore -Name $dsName).ExtensionData.Info.Vmfs.Extent[0].Diskname }} |
The following snippet uses a hash table to translate the datastorename into a canonicalname.
1 2 3 4 5 6 7 |
$dsTab = @{} Get-Datastore | ForEach-Object { $dsTab.Add("[$($_.Name)]", $_.ExtensionData.Info.Vmfs.Extent[0].Diskname) } Get-VM | Get-HardDisk | Select-Object @{N = 'VM'; E = {$_.Parent.name}}, Name, @{N = 'CanonicalName'; E = {$dsTab[$_.Filename.Split(' ')[0]]}} |