Hello,
I need to edit this script so that it will also return the Cluster name and if possible the VC name as well as the DS name. Can anyone suggest what needs to be added to get these to show up on the report? Don't need to know VM or VMDK names.
$vCenterList = "VC1","VC2"
$cred = get-Credential
Function Percentcal {
param(
[parameter(Mandatory = $true)]
[int]$InputNum1,
[parameter(Mandatory = $true)]
[int]$InputNum2)
$InputNum1 / $InputNum2*100
}
foreach ($vcenter in $vCenterList)
{
Connect-VIServer -server $vcenter -credential $cred
$datastores = Get-Datastore | Sort Name
ForEach ($ds in $datastores)
{
if (($ds.Name -match “Shared”) -or ($ds.Name -match “”))
{
$PercentFree = Percentcal $ds.FreeSpaceMB $ds.CapacityMB
$PercentFree = “{0:N2}” -f $PercentFree
$ds | Add-Member -type NoteProperty -name PercentFree -value $PercentFree
}
}
}
$datastores | Select Name,@{N=”UsedSpaceGB”;E={[Math]::Round(($_.ExtensionData.Summary.Capacity – $_.ExtensionData.Summary.FreeSpace)/1GB,0)}},
@{N=”TotalSpaceGB”;E={[Math]::Round(($_.ExtensionData.Summary.Capacity)/1GB,0)}},
@{N=”FreeSpaceGB”;E={[Math]::Round(($_.ExtensionData.Summary.FreeSpace)/1GB,0)}}, PercentFree | Export-Csv C:\Temp\Report.csv -NoTypeInformation
Hi,
Try this @{N="Data center";E={$_.Datacenter}},@{N="Cluster";E={($_ | Get-DatastoreCluster).Name}}
Bye.
Are you wanting to see the clusters associated to a vcenter? Are you wanting to replace this last line as you don't need datastore free space reporting?
@{N=”FreeSpaceGB”;E={[Math]::Round(($_.ExtensionData.Summary.FreeSpace)/1GB,0)}}, PercentFree | Export-Csv C:\Temp\Report.csv -NoTypeInformation
Thanks I will try this out.
I mainly need to see what cluster each datastore is located in.
Hello, I need to see how much space is used, free, total size and free percent for each datastore.
Look it:
Select-Object name,@{N="Datacenter";E={$_.datacenter}},@{N="Cluster";E={$_|Get-DatastoreCluster}},@{N="Capacity";E={$_.CapacityGB}},@{N="Used";E={[Math]::Round(($_.CapacityGB - $_.FreeSpaceGB),2)}},@{N="Free";E={$_.FreeSpaceGB}},@{N="% free";E={[Math]::Round(($_.FreeSpaceGB * 100 / $_.CapacityGB),2)}}
Thanks for the info. When I run the script everything seems to work fine but the cluster still shows up blank for each datastore line.
If you run "Get-DatastoreCluster" by itself, do you get a return? My guess is no. Which means you do not have datastore clusters, you have host clusters. which will require a bit different of a script.
Hello, yes we use Host clusters. Any idea how to get this script to return host clusters based on what I posted int he original code?
I know there has to be a way to link them together on the report but I can't make the connection with what I have tried so far.
A datastore is a "datacenter" thing, so its possible for a datastore to be visible to multiple clusters. A datastore has a property that lists all the hosts that mount the datastore (extensiondata.host) so you can work your way backwards from there.
Get-Datastore | Select Name, @{N="Cluster"; E={($_.ExtensionData.Host | ForEach { Get-VMHost -Id $_.Key } | Get-Cluster | Select-Object -ExpandProperty Name) -Join ","}}
A bit of a breakout on how it works...
Depending on the # of hosts you have this may take a while to run (Get-VMHost is an expensive/slow cmdlet) so you could look at optimizing it using Get-View instead of Get-VMHost to get a host name that Get-Cluster can use from the VMHost ID...
Thanks for the info. I tried updating the script with the following but it doens't seem to be working. Am I inputing that code in the script correctly or does something else need to be edited? The parts in blue are what I am confused about, is there a specific way to get it all to tie togther? When I run it like below it shows a Cluster colunm and a name colunm and they both have a datastore name but they are different datastores.
$vCenterList = "vc1"
Function Percentcal {
param(
[parameter(Mandatory = $true)]
[int]$InputNum1,
[parameter(Mandatory = $true)]
[int]$InputNum2)
$InputNum1 / $InputNum2*100
}
foreach ($vcenter in $vCenterList)
{
Connect-VIServer -server $vcenter
$datastores = Get-Datastore | Sort Name
ForEach ($ds in $datastores)
{
if (($ds.Name -match “Shared”) -or ($ds.Name -match “”))
{
$PercentFree = Percentcal $ds.FreeSpaceMB $ds.CapacityMB
$PercentFree = “{0:N2}” -f $PercentFree
$ds | Add-Member -type NoteProperty -name PercentFree -value $PercentFree
}
}
}
$datastores | Select @{N="Cluster"; E={($_.ExtensionData.Host | ForEach { Get-VMHost -Id $_.Key } | Get-Cluster | Select-Object -ExpandProperty Name) -Join ","}},Name,@{N=”UsedSpaceGB”;E={[Math]::Round(($_.ExtensionData.Summary.Capacity – $_.ExtensionData.Summary.FreeSpace)/1GB,0)}},
@{N=”TotalSpaceGB”;E={[Math]::Round(($_.ExtensionData.Summary.Capacity)/1GB,0)}},
@{N=”FreeSpaceGB”;E={[Math]::Round(($_.ExtensionData.Summary.FreeSpace)/1GB,0)}}, PercentFree | Export-Csv C:\
Try running this and see what your results are....
$vCenterList = "vc1"
Function Percentcal {
param(
[parameter(Mandatory = $true)]
[int]$InputNum1,
[parameter(Mandatory = $true)]
[int]$InputNum2)
$InputNum1 / $InputNum2*100
}
foreach ($vcenter in $vCenterList)
{
Connect-VIServer -server $vcenter
$datastores = Get-Datastore | Sort Name
ForEach ($ds in $datastores)
{
if (($ds.Name -match “Shared”) -or ($ds.Name -match “”))
{
$PercentFree = Percentcal $ds.FreeSpaceMB $ds.CapacityMB
$PercentFree = “{0:N2}” -f $PercentFree
$ds | Add-Member -type NoteProperty -name PercentFree -value $PercentFree
}
}
}
$datastores | Select @{N="Cluster"; E={($_.ExtensionData.Host | ForEach { Get-VMHost -Id $_.Key } | Get-Cluster | Select-Object -ExpandProperty Name) -Join ","}} | export-csv c:\
Hello, when I run that the results kick out the datastore names under the Cluster column. Thanks.
Can you remove the "| export-csv c:\" from the end of the last line so the output goes to the screen? Other than that change I ran your script as posted and it produced the following (which looks ok to me).
With "Export-CSV" you should add a -NoTypeInformation parameter as well as specifying a file name (i.e. | Export-CSV -Path C:\Temp\DSUsage.csv -NoTypeInformation).. Also I've used a "comma" as the separator in the list of clusters when a datastore is visible to multiple clusters so depending on the app you're using to open the CSV file that may be an issue (you can change -join "," to -join "!"
Cluster : HQ-C01
Name : GLD-0004
UsedSpaceGB : 2418
TotalSpaceGB : 4096
FreeSpaceGB : 1678
PercentFree : 40.96
Cluster : HQ-C02,HQ-C03,HQ-C01
Name : LIB-0102
UsedSpaceGB : 1783
TotalSpaceGB : 2048
FreeSpaceGB : 265
PercentFree : 12.92
Cluster : HQ-C01
Name : PLT-0001
UsedSpaceGB : 894
TotalSpaceGB : 1024
FreeSpaceGB : 130
PercentFree : 12.67
Cluster : HQ-C01
Name : PLT-0028
UsedSpaceGB : 839
TotalSpaceGB : 1024
FreeSpaceGB : 185
PercentFree : 18.05
Cluster : HQ-C01
Name : SIL-0006
UsedSpaceGB : 2668
TotalSpaceGB : 4096
FreeSpaceGB : 1427
PercentFree : 34.85
One quick note is that if you're going to run this for a list of vCenters you'll need to move the $datastores | ... bits inside the ForEach loop; as written it will only give you output for the last vCenter in the list
That is very interesting. I have the full path and file name in the report section, and only testing on one VC and still getting just the datastore name instead of a cluster name. There must be some difference in our environment that is causing it to goof up. We are not using datastore clusters at the moment if that matters.
I also tried without the report option and the output is the same.
Phew, I found a mistake in what I was running, a typo and now it is exporting the cluster name as well as the DS name and sizes correctly!!
One thing though is it is running extremely slow like you had menitoned so I am going to see if I can use the other commandlet to speed it up. Is it as simple as chaning the terms out or are there other things that would need to be edited?
Post what you're thinking of changing...
This is what I am running now that seems to return the correct results, it just runs really slowly. The part in red below is what I tried changing to the get-view command but I think there is more to going from get-vmhost to get-view. I tried several different switches and extension but got NULL results.
$vCenterList = "vc1"
Function Percentcal {
param(
[parameter(Mandatory = $true)]
[int]$InputNum1,
[parameter(Mandatory = $true)]
[int]$InputNum2)
$InputNum1 / $InputNum2*100
}
foreach ($vcenter in $vCenterList)
{
Connect-VIServer -server $vcenter
$datastores = Get-Datastore | Sort Name
ForEach ($ds in $datastores)
{
if (($ds.Name -match “Shared”) -or ($ds.Name -match “”))
{
$PercentFree = Percentcal $ds.FreeSpaceMB $ds.CapacityMB
$PercentFree = “{0:N2}” -f $PercentFree
$ds | Add-Member -type NoteProperty -name PercentFree -value $PercentFree
}
}
}
$datastores | Select @{N="Cluster"; E={($_.ExtensionData.Host | ForEach { Get-VMHost -Id $_.Key } | Get-Cluster | Select-Object -ExpandProperty Name) -Join ","}},
Name,
@{N=”UsedSpaceGB”;E={[Math]::Round(($_.ExtensionData.Summary.Capacity – $_.ExtensionData.Summary.FreeSpace)/1GB,0)}},
@{N=”TotalSpaceGB”;E={[Math]::Round(($_.ExtensionData.Summary.Capacity)/1GB,0)}},
@{N=”FreeSpaceGB”;E={[Math]::Round(($_.ExtensionData.Summary.FreeSpace)/1GB,0)}},
PercentFree | export-csv C:\Info.csv -NoTypeInformation
Glad you got it going, I'll have a look at some "optimization" over the next day or two to get it running with reasonable speed. The main performance drag is "Get-VMHost" which is getting called for every host that can see every datastore (so if you have 50 datastores that can be seen by 30 hosts, that means the code is calling Get-VMHost 1500 times, and that call takes a second or so to run (so 25 minutes worth of execution time) . I'm going try replacing that with Get-View into a collection at the beginning of the script and pre-build the host-> cluster mapping so I can turn the select into just a variable lookup as opposed to having to run any queries...