Hello,
Got a problem with reporting infrastructure. I've got several things to report: VM name, DNS Name, Powerstate, etc but only for VMs which are in specific Datacenter, and as my scripting skills are quite bad the script is taking over 3 hours to run which is terrible. Could someone help with optimalisation?
$(foreach($dc in Get-Datacenter -Name "Datacentername"){
foreach($cluster in Get-Cluster -Location $dc){
foreach($esx in Get-VMHost -Location $cluster){
foreach($vm in Get-VM -Location $esx){
Get-VM $vm | Sort Name | Get-View | Select @{N='VM'; E={$vm.Name}},
@{N='DNS Name'; E={$vm.ExtensionData.Guest.Hostname}},
@{N='Powerstate'; E={$vm.Powerstate}},
@{N='CPUs'; E={$vm.NumCpu}},
@{N='Sockets'; E={$vm.NumCpu/$vm.ExtensionData.config.hardware.NumCoresPerSocket}},
@{N='Cores p/s'; E={$vm.ExtensionData.config.hardware.NumCoresPerSocket}},
@{N='Datacenter';E={$dc.Name}},
@{N='Cluster';E={$cluster.Name}},
@{N='Host';E={$esx.Name}},
@{N='OS' ;E={$vm.ExtensionData.summary.config.guestfullname}}}
}
}
})|Export-Csv C:\path\to\save\file.csv -NoTypeInformation
Try like this
$dcName = 'Datacentername'
$report = @()
$dc = Get-View -ViewType Datacenter -Property Name,HostFolder -Filter @{'Name'="^$($dcName)$"}
foreach($cluster in (Get-View -Id ((Get-View -Id $dc.HostFolder -Property ChildEntity).ChildEntity | where{$_.Type -eq 'ClusterComputeResource'}) -Property Name,Host)){
foreach($esx in Get-View -Id $cluster.Host -Property Name,VM){
if($esx.VM){
$report += Get-View -Id $esx.VM -Property Name,Guest,Runtime,Config.Hardware,Summary.Config |
Where{!$_.Config.Template} |
Select Name,
@{N='DNS Name'; E={$_.Guest.Hostname}},
@{N='Powerstate'; E={$_.Runtime.Powerstate}},
@{N='CPUs'; E={$_.Config.Hardware.NumCpu}},
@{N='Sockets'; E={$_.Config.Hardware.NumCpu/$_.Config.Hardware.NumCoresPerSocket}},
@{N='Cores p/s'; E={$_.Config.Hardware.NumCoresPerSocket}},
@{N='Datacenter';E={$dc.Name}},
@{N='Cluster';E={$cluster.Name}},
@{N='Host';E={$esx.Name}},
@{N='OS' ;E={$_.summary.config.guestfullname}}
}
}
}
$report | Export-Csv C:\path\to\save\file.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try like this
$dcName = 'Datacentername'
$report = @()
$dc = Get-View -ViewType Datacenter -Property Name,HostFolder -Filter @{'Name'="^$($dcName)$"}
foreach($cluster in (Get-View -Id ((Get-View -Id $dc.HostFolder -Property ChildEntity).ChildEntity | where{$_.Type -eq 'ClusterComputeResource'}) -Property Name,Host)){
foreach($esx in Get-View -Id $cluster.Host -Property Name,VM){
if($esx.VM){
$report += Get-View -Id $esx.VM -Property Name,Guest,Runtime,Config.Hardware,Summary.Config |
Where{!$_.Config.Template} |
Select Name,
@{N='DNS Name'; E={$_.Guest.Hostname}},
@{N='Powerstate'; E={$_.Runtime.Powerstate}},
@{N='CPUs'; E={$_.Config.Hardware.NumCpu}},
@{N='Sockets'; E={$_.Config.Hardware.NumCpu/$_.Config.Hardware.NumCoresPerSocket}},
@{N='Cores p/s'; E={$_.Config.Hardware.NumCoresPerSocket}},
@{N='Datacenter';E={$dc.Name}},
@{N='Cluster';E={$cluster.Name}},
@{N='Host';E={$esx.Name}},
@{N='OS' ;E={$_.summary.config.guestfullname}}
}
}
}
$report | Export-Csv C:\path\to\save\file.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Wow thanks for such a fast reply! This script takes 10 minutes to run and gives great results. Is there any way I could specify more than one datacenter name if I'd like to report for multiple datacenters at once?
Sure, the Filter expression expects a RegEx expression, in there we can have multiple values separated by |
Something like this
$dcName = 'Datacentername1','Datacentername2'
$dcExpression = "^$($dcName -join '$|^')$"
$report = @()
foreach($dc in (Get-View -ViewType Datacenter -Property Name,HostFolder -Filter @{'Name'=$dcExpression})){
foreach($cluster in (Get-View -Id ((Get-View -Id $dc.HostFolder -Property ChildEntity).ChildEntity | where{$_.Type -eq 'ClusterComputeResource'}) -Property Name,Host)){
foreach($esx in Get-View -Id $cluster.Host -Property Name,VM){
if($esx.VM){
$report += Get-View -Id $esx.VM -Property Name,Guest,Runtime,Config.Hardware,Summary.Config |
Where{!$_.Config.Template} |
Select Name,
@{N='DNS Name'; E={$_.Guest.Hostname}},
@{N='Powerstate'; E={$_.Runtime.Powerstate}},
@{N='CPUs'; E={$_.Config.Hardware.NumCpu}},
@{N='Sockets'; E={$_.Config.Hardware.NumCpu/$_.Config.Hardware.NumCoresPerSocket}},
@{N='Cores p/s'; E={$_.Config.Hardware.NumCoresPerSocket}},
@{N='Datacenter';E={$dc.Name}},
@{N='Cluster';E={$cluster.Name}},
@{N='Host';E={$esx.Name}},
@{N='OS' ;E={$_.summary.config.guestfullname}}
}
}
}
}
$report | Export-Csv C:\path\to\save\file.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference