I'm using a function to gather datastore usage statistics which I've modified slightly, but how would I modify it to add the cluster name the datastores are sat in?
Function Get_DataStores{
$report = @()
foreach($hostname in get-vmhost){
Get-VMHost -name $hostname -Location $cluster | Get-Datastore | %{
$info = "" | select Host,DSName,CapacityMB,FreeSpaceMB,ProvisionedMB,UsedMB
$info.Host = $hostname
$info.DSName = $_.Name
$info.CapacityMB = ($_.ExtensionData.Summary.Capacity)/1MB
$info.FreeSpaceMB = ($_.ExtensionData.Summary.FreeSpace)/1MB
$info.ProvisionedMB = ((($_.ExtensionData.Summary.Capacity) - ($_.ExtensionData.Summary.FreeSpace)) + ($_.ExtensionData.Summary.Uncommitted))/1MB
$info.UsedMB = (($info.CapacityMB - $info.FreeSpaceMB)/1MB)
$report += $info
}
}
$csv = "C:\datastores.csv"
$report | Export-Csv $csv -NoTypeInformation
}
Thanks in advance
You could do it like this
Function Get_DataStores{
$report = @()
foreach($hostname in get-vmhost){
$cluster = Get-Cluster -VMHost $hostname
Get-VMHost -name $hostname | Get-Datastore | %{
$info = "" | select Cluster,Host,DSName,CapacityMB,FreeSpaceMB,ProvisionedMB,UsedMB
$info.Cluster = $cluster.Name
$info.Host = $hostname
$info.DSName = $_.Name
$info.CapacityMB = ($_.ExtensionData.Summary.Capacity)/1MB
$info.FreeSpaceMB = ($_.ExtensionData.Summary.FreeSpace)/1MB
$info.ProvisionedMB = ((($_.ExtensionData.Summary.Capacity) - ($_.ExtensionData.Summary.FreeSpace)) + ($_.ExtensionData.Summary.Uncommitted))/1MB
$info.UsedMB = (($info.CapacityMB - $info.FreeSpaceMB)/1MB)
$report += $info
}
}
$csv = "C:\datastores.csv"
$report | Export-Csv $csv -NoTypeInformation
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You seem to be using the $cluster variable on the Location parameter.
What is in there, a cluster object or cluster name ?
And where did you initialise that variable ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Sorry, originally the function had a foreach cluster loop like so:
Function Get_DataStores($vcenter) {
$report = @()
foreach($hostname in get-vmhost){
foreach($cluster in Get-Cluster){
Get-VMHost -name $hostname -Location $cluster | Get-Datastore | %{
$info = "" | select Host,DSName,ClusterName,CapacityMB,FreeSpaceMB, ProvisionedMB,UsedMB
$info.Host = $hostname
$info.DSName = $_.Name
$info.ClusterName = $cluster
$info.CapacityMB = ($_.ExtensionData.Summary.Capacity)/1MB
$info.FreeSpaceMB = ($_.ExtensionData.Summary.FreeSpace)/1MB
$info.ProvisionedMB = ($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace + $_.ExtensionData.Summary.Uncommitted)/1MB
$info.UsedMB = ($info.Capacity - $_.ExtensionData.Summary.FreeSpace)/1MB
$report += $info
}
}
}
$csv = "C:\datastores.csv"
$report | Export-Csv $csv -NoTypeInformation
}
This resulted in a greater amount of results returned though. I think it brought back 29 datastores instead of 6.
You could do it like this
Function Get_DataStores{
$report = @()
foreach($hostname in get-vmhost){
$cluster = Get-Cluster -VMHost $hostname
Get-VMHost -name $hostname | Get-Datastore | %{
$info = "" | select Cluster,Host,DSName,CapacityMB,FreeSpaceMB,ProvisionedMB,UsedMB
$info.Cluster = $cluster.Name
$info.Host = $hostname
$info.DSName = $_.Name
$info.CapacityMB = ($_.ExtensionData.Summary.Capacity)/1MB
$info.FreeSpaceMB = ($_.ExtensionData.Summary.FreeSpace)/1MB
$info.ProvisionedMB = ((($_.ExtensionData.Summary.Capacity) - ($_.ExtensionData.Summary.FreeSpace)) + ($_.ExtensionData.Summary.Uncommitted))/1MB
$info.UsedMB = (($info.CapacityMB - $info.FreeSpaceMB)/1MB)
$report += $info
}
}
$csv = "C:\datastores.csv"
$report | Export-Csv $csv -NoTypeInformation
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That gives me the information I need but I have 74 datastores in the particular vCenter I ran the function against and it returned 368 results.
EDIT - I'm going through clearing the CSV manually but it seems the case that the extra results are duplicated information.
I think looking at the report I need I'd be best producing a separate section for this. That's great though and answers my question. Thanks Luc.
You most probably have shared LUNs with datastores on them, that means the same datastore is returned for multiple VMHosts.
You could do a Sort-Object with the DSname property and use the Unique switch
But that would mean, you would not see the datastore on all VMhosts where it visible
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference