Bob24
Contributor
Contributor

script is running very slow

I have the following scripts that pulled # hard drives of eacg vms , each hard drvies capacity, VMhost vmrunning on, memory and cpu of the VM. This script taks forever to run. Any ideas why? @LucD . Thanks.

 

$report = @()
$datastores = Get-Datastore | where{$_.ExtensionData.Summary.MultipleHostAccess}

Foreach ($Datastore in $datastores){
$VMs = Get-VM
Foreach ($VM in $VMs){
$line = "" | Select Name, vCPU, 'Memory(GB)', VMserver, Datastore, HardDisk, Capacity
$line.Name = $VM.name
$line.vCPU = $vm.NumCpu
$line.'Memory(GB)' = $vm.MemoryGB
$line.VMserver = $vm.VMHost
$line.Datastore = $datastore.name
$line.HardDisk = ($VM | Get-HardDisk | Select @{N = 'HardDisk'; E= {$_.name}})
$line.Capacity = ($VM | Get-HardDisk | Select @{N = 'Capacity'; E= {$_.CapacityGB}})
$report += $line
}
}
$report | Export-csv C:\productionVMs2.csv

 

 

0 Kudos
2 Replies
LucD
Leadership
Leadership

I would first eliminate duplicate calls and use the pipeline instead of the Foreach loops.
Something like this

Get-Datastore -PipelineVariable ds |
Where-Object{$_.ExtensionData.Summary.MultipleHostAccess} |
Get-VM |
Get-HardDisk |
Select-Object -Property @{N='VM';E={$_.Parent.Name}},
    @{N='vCPU';E={$_.Parent.NumCpu}},
    @{N='Memory(GB)';E={$_.Parent.MemoryGB}},
    @{N='VMserver';E={$_.Parent.VMhost.Name}},
    @{N='Datastore';E={$ds.Name}},
    @{N='Harddisk';E={$_.Name}},
    @{N='Capacity';E={$_.CapacityGB}} |
Export-csv C:\productionVMs2.csv


You can make it probably faster by switching to Get-View cmdlets, but that will definitely make the script a bit more complex.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Bob24
Contributor
Contributor

it works. Thanks @LucD 

0 Kudos