Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Check All VMFS Datastores connected on all VMware ESXi Hosts
# Start Load VMware Snapin (if not already loaded)
if (!(Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {
if (!(Add-PSSnapin -PassThru VMware.VimAutomation.Core)) {
# Error out if loading fails
Write-Error "ERROR: Cannot load the VMware Snapin. Is the PowerCLI installed?"
Exit
}
}
# End Load VMware Snapin (if not already loaded)
# Start Global Definitions
$VIServer = "yourvCenter.lan.local"
$DataCenterName = "yourDataCenter"
# End Global Definitions
# Start vCenter Connection
Write-Output "Starting to Process vCenter Connection to " $VIServer " ..."
$OpenConnection = $global:DefaultVIServers | where { $_.Name -eq $VIServer }
if($OpenConnection.IsConnected) {
Write-Output "vCenter is Already Connected..."
$VIConnection = $OpenConnection
} else {
Write-Output "Connecting vCenter..."
$VIConnection = Connect-VIServer -Server $VIServer
}
if (-not $VIConnection.IsConnected) {
Write-Error "Error: vCenter Connection Failed"
Exit
}
# End vCenter Connection
# Start Check All Datastores on all Hosts
$MyErrors = ""
$myDataCenter = Get-Datacenter -Name $DataCenterName
$AllStores = $myDataCenter | Get-Datastore | where {$_.Accessible -eq "True"} | Sort Name
$AllHosts = $myDataCenter | Get-VMhost | where {$_.ConnectionState -eq "Connected"} | Sort Name
foreach ($myHost in $AllHosts) {
foreach ($myStore in $AllStores) {
try {
Write-Output "Check $myStore on $myHost"
$DS = $myHost | Get-Datastore -Name $($myStore.Name) -ErrorAction Stop
}
catch {
Write-Output " ...Not Found"
$MyErrors += "Datastore $myStore not found on $myHost `n"
}
}
}
# End Check All Datastores on all Hosts
Write-Output "" $MyErrors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.