Reply to Message

View discussion in a popup

Replying to:
BBB36
Enthusiast
Enthusiast

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