Hey guys,
Looking for help from a PowerCLI master...
I would like a script that would list all datastores for multiples vCenters in a csv format.
The columns would be Datastore Name, Capacity (GB), Provisionned Space (GB), Free Space (GB), Free Space (%), Host, Cluster, vCenter
I found one that exports it in HTML that is pretty good but I would like to have it in a CSV.
(Script Get DataStore Usage Report on email. )
Let me know if you can help me!
Thanks!
Try something like this
$report = foreach($vc in $global:DefaultVIServers){
Get-Datastore -Server $vc | %{
New-Object PSObject -Property ([ordered]@{
vCenter = $vc.Name
ESXi = (Get-VMHost -Datastore $_ -Server $vc | %{$_.Name.Split('.')[0]}) -join '|'
Cluster = Get-VMHost -Datastore $_ -Server $vc | Get-Cluster | Select -ExpandProperty Name
Name = $_.Name
CapacityGB = $_.CapacityGB
FreeSpaceGB = $_.FreeSpaceGB
ProvisionedGB = $_.CapacityGB - $_.FreeSpaceGB + ($_.ExtensionData.Summary.Uncommitted/1GB)
UsedGB = ($_.CapacityGB - $_.FreeSpaceGB)
FreePerc = "{0:P}" -f (1 - ($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB)
})
}
}
$report | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try something like this
$report = foreach($vc in $global:DefaultVIServers){
Get-Datastore -Server $vc | %{
New-Object PSObject -Property ([ordered]@{
vCenter = $vc.Name
ESXi = (Get-VMHost -Datastore $_ -Server $vc | %{$_.Name.Split('.')[0]}) -join '|'
Cluster = Get-VMHost -Datastore $_ -Server $vc | Get-Cluster | Select -ExpandProperty Name
Name = $_.Name
CapacityGB = $_.CapacityGB
FreeSpaceGB = $_.FreeSpaceGB
ProvisionedGB = $_.CapacityGB - $_.FreeSpaceGB + ($_.ExtensionData.Summary.Uncommitted/1GB)
UsedGB = ($_.CapacityGB - $_.FreeSpaceGB)
FreePerc = "{0:P}" -f (1 - ($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB)
})
}
}
$report | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hello LucD
Where we have to use viserver-connect command.
You use Connect-VIServer at the start of the script (1st line).
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you LucD
Created below script
$time_date = Get-Date -Format g
Start-Transcript -Path "D:\report $(get-date -f MM-dd-yyyy_HH_mm_ss).txt"
#Put vcenter list
$vcs = get-content D:\vcenterlist.txt
Foreach ($vc in $vcs)
{
Connect-VIServer $vc
$report = Get-Datastore -Server $vc | %{
New-Object PSObject -Property ([ordered]@{
vCenter = $vc
ESXi = (Get-VMHost -Datastore $_ -Server $vc | %{$_.Name.Split('.')[0]}) -join '|'
Cluster = Get-VMHost -Datastore $_ -Server $vc | Get-Cluster | Select -ExpandProperty Name
Name = $_.Name
CapacityGB = $_.CapacityGB
FreeSpaceGB = $_.FreeSpaceGB
ProvisionedGB = $_.CapacityGB - $_.FreeSpaceGB + ($_.ExtensionData.Summary.Uncommitted/1GB)
UsedGB = ($_.CapacityGB - $_.FreeSpaceGB)
FreePerc = "{0:P}" -f (1 - ($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB)
})
}
$report | Export-Csv -Path "D:\$vc $(get-date -f MM-dd-yyyy_HH_mm_ss).csv" -NoTypeInformation -UseCulture
#disconnect vcenter.
disconnect-viserver $vc -confirm:$false
}
Stop-Transcript
LucD,
After running the script it stuck forever
Thanks
vmk
Without any further information that is hard to diagnose I'm afraid.
How many vCenters, datastores... are involved?
Did you try to run it against a smaller sample (1 VC, a few datastores)?
Is it really stuck? Is the script still using CPU?
Did you try hitting <Enter> after the Warning message?
Did you stop/start you PS session before trying again?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, after couple of minutes
At D:\vmk\Datastore-freespace.ps1:11 char:23
+ Cluster = Get-VMHost -Datastore $_ -Server $vc | Get-Clus ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-VMHost], ViError
+ FullyQualifiedErrorId : Client20_QueryServiceImpl_RetrievePropertiesEx_ViError,VMware.VimAutomation.ViCore.Cmdle
ts.Commands.GetVMHost
Get-VMHost : 12/23/2019 4:43:35 PM Get-VMHost The HTTP service located at https://vc-
.com/sdk
is unavailable. This could be because the service is too busy or because no endpoint was found listening at the
specified address. Please ensure that the address is correct and try accessing the service again later.
At D:\vmk\Datastore-freespace.ps1:11 char:23
+ Cluster = Get-VMHost -Datastore $_ -Server $vc | Get-Clus ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-VMHost], ViError
+ FullyQualifiedErrorId : Client20_QueryServiceImpl_RetrievePropertiesEx_ViError,VMware.VimAutomation.ViCore.Cmdle
ts.Commands.GetVMHost
Thanks
vmk
Looks like there is an issue with one of the vCenters.
Can you do the following against al the vCenters, without an error?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, it fetching ESXi host.
Name ConnectionState PowerState NumCpu CpuUsageMhz CpuTotalMhz MemoryUsageGB MemoryTotalGB Version
Thanks
vmk
Any errors on this?
Get-Datastore -Server $vc | Get-VMHost -Server $vc
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference