VMware Cloud Community
BBB36
Enthusiast
Enthusiast
Jump to solution

Find list of datastores of a certain free space in clusters in multiple vCenters

I'm looking to get a list of datastores of at least 8TB free space in any cluster found in each vCenters then export the vCenter, Datacenter, cluster name and datastores information to a CSV file. The script I currently gathers some results but isn't accurate. For exapmple I don't think I have the calculated properties right because the current exported results displays a lot of information I don't need. Can someone please help refine it so it displays the results exacly how I need it to?

I still haven't also figured out how to include the vCenter name in the script, which I've pasted below. Thank you.

$Creds = Get-Credential -Message "Enter username and password" -ErrorAction SilentlyContinue;

$vCenters = (Get-Content "C:\VC.txt")

foreach ($vCenter in $vCenters) {

$Connection = Connect-VIServer $vCenter -Protocol https -Credential $Creds

        if (!$Connection){return "ERROR: No connection to $vCenter"}

}

Get-Cluster -Server $global:DefaultVIServers -PipelineVariable cluster |

    ForEach-Object -Process {

    Get-Datastore -Name *

        Select   @{N='Cluster';E={$_Cluster.Name}}

        @{N='Datacenter';E={$_.Datacenter}}

        @{N='Datastore Browser Path';E={$_.DatastoreBrowserPath}}

        @{N='Datastore Name';E={$_.Name}}

        @{N='Datastore Capacity';E={$_.CapacityGB}}

        @{N='Datastore Free Space';E={$_.FreeSpaceGB}} |

        Where ({$_.FreeSpaceGB -like "817*" -and $_.DatastoreBrowserPath -notcontains "*local-storage-1"}) | Select -First 1

}|

Export-Csv -Path .\Output.csv -Append -NoTypeInformation -UseCulture

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

See if this works for you

$Creds = Get-Credential -Message "Enter username and password" -ErrorAction SilentlyContinue

$vCenters = (Get-Content "C:\VC.txt")


$report = foreach ($vCenter in $vCenters) {

    Connect-VIServer $vCenter -Protocol https -Credential $Creds | Out-Null


    Get-Cluster -Server $vCenter -PipelineVariable cluster |

    ForEach-Object -Process {

        Get-Datastore -RelatedObject $cluster |

        where{$_.FreeSpaceGB -gt 8GB -and $_.DatastoreBrowserPath -notcontains "*local-storage-1"} |

        Select @{N='vCenter';E={$vCenter}},

            @{N='Cluster';E={$Cluster.Name}},

            @{N='Datacenter';E={$_.Datacenter.Name}},

            @{N='Datastore Browser Path';E={$_.DatastoreBrowserPath}},

            @{N='Datastore Name';E={$_.Name}},

            @{N='Datastore Capacity';E={$_.CapacityGB}},

            @{N='Datastore Free Space';E={$_.FreeSpaceGB}}

    }

}


$report |  Export-Csv -Path .\Output.csv -Append -NoTypeInformation -UseCulture


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

View solution in original post

0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

Can you explain the purpose of this Where-clause?

Where ({$_.FreeSpaceGB -like "817*" -and $_.DatastoreBrowserPath -notcontains "*local-storage-1"})


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

0 Kudos
BBB36
Enthusiast
Enthusiast
Jump to solution

Hi LucD, yes. For the $_.FreeSpaceGB -like "817* I am trying to filter for datastores with a fee space of at least 8TB (which essentially means almost the same size as the capacity because I don't want datastores that have VM files to be included in the results. For the $_.DatastoreBrowserPath -notcontains "*local-storage-1, I want to exclude all local datastores, only Shared SAN or NAS should be in the results.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

See if this works for you

$Creds = Get-Credential -Message "Enter username and password" -ErrorAction SilentlyContinue

$vCenters = (Get-Content "C:\VC.txt")


$report = foreach ($vCenter in $vCenters) {

    Connect-VIServer $vCenter -Protocol https -Credential $Creds | Out-Null


    Get-Cluster -Server $vCenter -PipelineVariable cluster |

    ForEach-Object -Process {

        Get-Datastore -RelatedObject $cluster |

        where{$_.FreeSpaceGB -gt 8GB -and $_.DatastoreBrowserPath -notcontains "*local-storage-1"} |

        Select @{N='vCenter';E={$vCenter}},

            @{N='Cluster';E={$Cluster.Name}},

            @{N='Datacenter';E={$_.Datacenter.Name}},

            @{N='Datastore Browser Path';E={$_.DatastoreBrowserPath}},

            @{N='Datastore Name';E={$_.Name}},

            @{N='Datastore Capacity';E={$_.CapacityGB}},

            @{N='Datastore Free Space';E={$_.FreeSpaceGB}}

    }

}


$report |  Export-Csv -Path .\Output.csv -Append -NoTypeInformation -UseCulture


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

0 Kudos
BBB36
Enthusiast
Enthusiast
Jump to solution

When I run it as is, it's generating an empty output.csv file (no properties also). But when I comment out the where clause, it's generating some results, but appears with all properties and not the calculated ones. The resulting properties generated are: FileSystemVersion, DatacenterId, Datacenter, ParentFolderId, ParentFolder, DatastoreBrowserPath, FreeSpaceMB, CapacityMB, Accessible, Type, StorageIOControlEnabled    CongestionThresholdMillisecond, State, ExtensionData, CapacityGB, FreeSpaceGB, Name, Id, Uid.

Also, and I'm not sure if this makes a difference or not, the resulting datastore number format is in decimal form like this for example: 8190.758789. Does the where clause need to be something like where{$_.FreeSpaceGB -gt "8190*" or so? BTW I did try this but whenever I leave the where clause, the result output.csv file is empty.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

When you get an empty CSV file that probably means that none of the datastores conform to the conditions in the Where-clause.

Judging from your description, when you leave out the Where-clause, it sounds as if you dropped the pipeline symbol ('|') at the end of the line.

The FreeSpaceGB property is a decimal, so you can't compare it to a string as "8190*" like you propose.


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

0 Kudos
BBB36
Enthusiast
Enthusiast
Jump to solution

Yep. You are 100% correct. None of the datastores conformed to the conditions in the Where-clause because the local datastore names are actually local-datastore-1 and not local-storage-1 as indicated in the script so I changed that and it now works.

I also did mistakenly drop the  pipeline symbol ('|') at the end of the line. When I un-commented it out now the output file has results exactly as desired.

As for the FreeSpaceGB property, I instead used a where clause like this where{$_.FreeSpaceGB -like "8*" and I'm able to get all shared, non-local datastores with at least 8TB free.

Lastly, I also removed the datastore browser path calculated property because I had intended to use it to identify the vCenter but now that there's a separate property for the vCenter, I no longer need it.

Is there a way to also add to the where clause or anywhere else in the script to check that no VM files are in the datastores matching any of the results?

In any case, as always, thanks so much for your help. This is the script I'll be using in case anyone else finds it useful.

$Creds = Get-Credential -Message "Enter username and password" -ErrorAction SilentlyContinue

$vCenters = (Get-Content "C:\VC.txt")

$report = foreach ($vCenter in $vCenters) {

    Connect-VIServer $vCenter -Protocol https -Credential $Creds | Out-Null

    Get-Cluster -Server $vCenter -PipelineVariable cluster |

    ForEach-Object -Process {

        Get-Datastore -RelatedObject $cluster |

        where{$_.FreeSpaceGB -like "81*" -and $_.DatastoreBrowserPath -notcontains "*local-datastore-1"} |

        Select @{N='vCenter';E={$vCenter}},

            @{N='Cluster';E={$Cluster.Name}},

            @{N='Datacenter';E={$_.Datacenter.Name}},

            @{N='Datastore Name';E={$_.Name}},

            @{N='Datastore Capacity';E={$_.CapacityGB}},

            @{N='Datastore Free Space';E={$_.FreeSpaceGB}}

    }

}

$report |  Export-Csv -Path .\Output.csv -Append -NoTypeInformation -UseCulture

LucD
Leadership
Leadership
Jump to solution

Sorry, but that comparison will not work in all situations.

where{$_.FreeSpaceGB -like "8*"

PowerShell will convert the decimal into a string, and the where-clause checks if the 1st character of that string is 8.

That means that "8","80","800" ... will all match, which is probably not what you want.

The correct condition is (like I showed before)

where{$_.FreeSpaceGB -ge 8GB


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

0 Kudos
BBB36
Enthusiast
Enthusiast
Jump to solution

OK. I understand. You've helped tremendously. Thank you very much!

0 Kudos