【发布时间】:2021-04-15 20:00:11
【问题描述】:
到目前为止,我已经在 PowerShell 脚本下创建了。
$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
我有以下相关问题:
- ConnectedDatastores 列给出了用分号分隔的连续输出数据,我尝试了
Split()以各种方式但没有成功。 - PhysicalNIC 列还使用 @{ 提供相同类型的连续输出数据,我也无法消除。
我需要 ConnectedDatastores 列以在新行中显示每个项目。
我需要与 PhysicalNIC 输出相同但没有 @{
【问题讨论】:
-
两列都返回
array而不是string,因此您会看到两列的结果都包含在{ result1, result2 , ... }中。我假设您想要的是将这些数组转换为多行字符串,对吗? -
感谢您的回复,但我没有想法。我该如何做到这一点?
标签: powershell