【问题标题】:Splitting selected data and formatting correctly in PowerShell在 PowerShell 中正确拆分所选数据和格式
【发布时间】: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

我有以下相关问题:

  1. ConnectedDatastores 列给出了用分号分隔的连续输出数据,我尝试了Split() 以各种方式但没有成功。
  2. PhysicalNIC 列还使用 @{ 提供相同类型的连续输出数据,我也无法消除。

我需要 ConnectedDatastores 列以在新行中显示每个项目。
我需要与 PhysicalNIC 输出相同但没有 @{

【问题讨论】:

  • 两列都返回array 而不是string,因此您会看到两列的结果都包含在{ result1, result2 , ... } 中。我假设您想要的是将这些数组转换为多行字符串,对吗?
  • 感谢您的回复,但我没有想法。我该如何做到这一点?

标签: powershell


【解决方案1】:

试试这个,它应该可以工作:

$myEsxis = Get-VMHost | Select Name,DatastoreIdList,ConnectionState -First 5

# Using Generic List for efficiency
$myEsxiHosts = [system.collections.generic.list[pscustomobject]]::new()

# Wrapping the oneliners inside scriptblocks for readability
$phyNIC={
        ((Get-EsxCli -VMHost $myEsxi.Name).Network.NIC.List() |
        Select @{
            Name = 'NIC'
            Expression = {"$($_.Name)/$($_.Speed)/$($_.LinkStatus)"}
        }).NIC
}

$dataStores={
    foreach ($obj in $myEsxi.DatastoreIdList)
    {
        (Get-Datastore | Where {($_.Id -eq $obj) -and ($_.Name -notlike '*local*')}).Name
    }
}

foreach ($myEsxi in $myEsxis)
{
    Try
    {
        $myEsxiHosts.Add(
            [pscustomobject]@{
                ESXiName = $myEsxi.Name
                ClusterName = (Get-Cluster -VMHost $myEsxi.Name).Name
                ConnectionState = $myEsxi.ConnectionState
                PingState = Test-Connection $myEsxi.Name -ErrorAction 0 -Quiet -Count 1
                PhyNIC = & $phyNIC | Out-String # Executing the scriptblock with '&' and piping the result to Out-String to get our multiline string
                NumVMs = (Get-VM -Location $myEsxi.Name).count
                ConnectedDatastores = & $dataStores | Out-String
                ESXiVersion = (Get-VMHost -Name $myEsxi.Name).ExtensionData.Config.Product.FullName
        })
    }
    Catch
    {
        $myEsxiHosts.Add(
            [pscustomobject]@{
                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 | Sort-Object -Property ESXiName 

编辑:我可能是错的,但大多数时候当我看到这个问题时,它与数据的导出方式(CSV、Xlsx 等)有关。拥有数组属性的问题是,当我们想要导出对象时,您将获得如下所示的类型,而不是显示所述属性的内容。有几种解决方法,即-join,如果你想要一个包含所有值的字符串,Out-String,如果你想让你的数组显示为多行字符串。

网上有很多比我能给你更好的解释,即:

数组属性:

数组转换为多行字符串:

编辑:

如果对象被转换为 HTML,而不是使用Out-String,您需要使用-join '<br>',然后在对象转换为 HTML 之后,您需要将'<br>' 替换为'<br>'。这在这里得到了很好的解释:Break lines in powershell array and then convert it to html?

作为一个测试示例,你可以试试这个:

(@(
[PSCustomObject]@{
    Name = 'Value'
    Arr = 1,2,3,4,5,6|Out-String
}

[PSCustomObject]@{
    Name = 'Value2'
    Arr = 1,2,3,4,5,6|Out-String
}

[PSCustomObject]@{
    Name = 'Value'
    Arr = 1,2,3,4,5,6 -join "<br>"
}

[PSCustomObject]@{
    Name = 'Value2'
    Arr = 1,2,3,4,5,6 -join "<br>"
})|ConvertTo-Html) -replace '&lt;br&gt;','<br>' > ./test.html

看起来像这样:

【讨论】:

  • 可能值得为答案添加一些解释,或者一些关于您在代码中所做的内联 cmets
  • @MathiasR.Jessen 同意,添加了解释以及一些内联 cmets。
  • 这对我来说是很多新知识。我试过你的脚本。但是,我的第一个要求的第二部分仍未满足。 PhyNIC 和 ConnectedDatastore 显示连续数据,没有换行符。如果我想为每列添加列大小,我应该在哪里以及如何添加?或者有没有更好的办法在每个输出对象上添加换行符?
  • @SumanChhetri 我需要看看它们是什么样子才能给出正确的答案,首先运行你的 $myEsxis 行,然后运行两个变量 $phyNIC$dataStores 并执行它们&amp;。如果您可以附上它们在您的 PS 主机中的外观截图,我会尽力帮助您。
  • 不幸的是,该编辑对我没有帮助。但是,最后我通过 CSS white-space: break-spaces; 解决了它,但无论如何,您为我提供了不同的视角。我会将您的脚本标记为答案。非常感谢。
【解决方案2】:

@santiago-squarzon 帮助我从报告中删除了 @{;
但是,我能够使用 CSS white-space: break-spaces; 打破界限。

【讨论】:

    猜你喜欢
    • 2017-07-14
    • 1970-01-01
    • 2017-04-10
    • 2022-10-04
    • 1970-01-01
    • 2019-04-29
    • 2019-04-13
    • 1970-01-01
    • 2021-12-20
    相关资源
    最近更新 更多