I have created below PowerShell script so far.
$myEsxis = Get-VMHost | Select Name,DatastoreIdList,ConnectionState -First 5
$myEsxiHosts = @()
foreach ($myEsxi in $myEsxis)
{ Try { $obj = New-Object psobject -Property @{
"ESXiName" = ($myEsxi).Name
"ClusterName" = (Get-Cluster -VMHost ($myEsxi).Name).Name
"ConnectionState" = ($myEsxi).ConnectionState
"PingState" = Test-Connection ($myEsxi).Name -ErrorAction 0 -Quiet -Count 1
"PhyNIC" = ((Get-EsxCli -VMHost ($myEsxi).Name).network.nic.list() | Select @{ Name = 'NIC'; Expression = {"$($_.Name)/$($_.Speed)/$($_.LinkStatus)"}} ) -split(";")
"NumVMs" = (Get-VM -Location ($myEsxi).Name).count
"ConnectedDatastores" = foreach ($obj in ($myEsxi).DatastoreIdList) {Get-Datastore | Where {($_.Id -eq $obj) -and ($_.Name -notlike '*local*')} | Select-Object -ExpandProperty Name}
"ESXiVersion" = (Get-VMHost -Name ($myEsxi).Name).ExtensionData.Config.Product.FullName
}
}
Catch { $obj = New-Object psobject -Property @{
"ESXiName" = ($myEsxi).Name
"ClusterName" = $_.Exception.Message
"ConnectionState" = $_.Exception.Message
"PingState" = Test-Connection ($myEsxi).Name -ErrorAction 0 -Quiet -Count 1
"NumVMs" = $_.Exception.Message
"PhyNIC" = $_.Exception.Message
"ConnectedDatastores" = $_.Exception.Message
"ESXiVersion" = $_.Exception.Message
}
}
$myEsxiHosts += $obj | Sort-Object -Property ESXiName | Select ESXiName,ClusterName,PingState,ConnectionState,NumVMs,@{Name='ConnectedDatastores';Expression={[string]::join(";", ($_.ConnectedDatastores))}},@{Name='PhysicalNIC';Expression={[string]::join(";", ($_.PhyNIC))}},ESXiVersion
}
$myEsxiHosts
I have below problems related to it:
- The column ConnectedDatastores gives continuous output data separated by a semicolon and I have tried
Split()
in various ways but didn't succeed. - The column PhysicalNIC also gives same type of continuous output data with @{ and that also I couldn't eliminate.
I need ConnectedDatastores column to display each item in new line.
I need same with the PhysicalNIC output but without @{