在PowerShell中正确拆分所选数据和格式

在PowerShell中正确拆分所选数据和格式,powershell,Powershell,到目前为止,我已经创建了下面的PowerShell脚本 $myEsxis = Get-VMHost | Select Name,DatastoreIdList,ConnectionState -First 5 $myEsxiHosts = @() foreach ($myEsxi in $myEsxis) { Try { $obj = New-Object psobject -Property @{ "ESXiName" = ($myEsxi).Na

到目前为止,我已经创建了下面的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输出,但如果没有@{

    尝试一下,它应该可以工作:

    $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
    ;如果希望数组为d,请使用
    Out string
    显示为多行字符串

    在互联网上有比我能给你的更好的解释,即:

    数组属性:

    已转换为多行字符串的数组:

    编辑: 如果对象转换为HTML,而不是使用
    输出字符串
    ,则需要使用
    -join'
    '
    ,然后在对象转换为HTML后,需要替换
    '
    '
    'br'

    作为测试示例,您可以尝试以下方法:

    (@(
    [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
    
    (@(
    [PSCustomObject]@{
    名称='值'
    Arr=1,2,3,4,5,6 |输出字符串
    }
    [PSCustomObject]@{
    名称='Value2'
    Arr=1,2,3,4,5,6 |输出字符串
    }
    [PSCustomObject]@{
    名称='值'
    Arr=1,2,3,4,5,6-连接“
    ” } [PSCustomObject]@{ 名称='Value2' Arr=1,2,3,4,5,6-连接“
    ” })|转换为Html)-替换“br”,“
    ”>。/test.Html
    看起来是这样的:


    试试这个,它应该可以:

    $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
    ;如果希望数组为d,请使用
    Out string
    显示为多行字符串

    在互联网上有比我能给你的更好的解释,即:

    数组属性:

    已转换为多行字符串的数组:

    编辑: 如果对象转换为HTML,而不是使用
    输出字符串
    ,则需要使用
    -join'
    '
    ,然后在对象转换为HTML后,需要替换
    '
    '
    'br'

    作为测试示例,您可以尝试以下方法:

    (@(
    [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
    
    (@(
    [PSCustomObject]@{
    名称='值'
    Arr=1,2,3,4,5,6 |输出字符串
    }
    [PSCustomObject]@{
    名称='Value2'
    Arr=1,2,3,4,5,6 |输出字符串
    }
    [PSCustomObject]@{
    名称='值'
    Arr=1,2,3,4,5,6-连接“
    ” } [PSCustomObject]@{ 名称='Value2' Arr=1,2,3,4,5,6-连接“
    ” })|转换为Html)-替换“br”,“
    ”>。/test.Html
    看起来是这样的:


    @santiago squarzon帮助我从报告中删除
    @
    {


    然而,我能够用CSS打破这条线:空格;

    @santiago squarzon帮助我从报告中删除
    @
    {


    但是,我可以使用CSS
    空格:break spaces;

    断行,这两列都返回一个
    数组,而不是
    字符串
    ,因此您可以看到两列的结果都包装在
    {result1,result2,…}
    。我假设您想要的是将这些数组转换为多行字符串,对吗?谢谢您的回答,但我没有主意。如何实现这一点?这两列都返回一个
    数组
    ,而不是
    字符串
    ,因此您看到两列的结果都包装在
    {result1,result2,…}
    。我想你想要的是将这些数组转换成多行字符串,对吗?谢谢你的回答,但我没有主意。我该如何实现这一点?也许值得为答案添加一些解释,或者添加一些关于你在code@MathiasR.Jessen同意,添加了解释以及一些inline注释。这对我来说是很多新知识。我尝试了你的脚本。但是,我的第一个要求的第二部分仍然没有满足。PhyNIC和ConnectedDatastores显示连续数据,没有换行符。如果我想为每个列添加列大小,我应该在哪里以及如何添加?或者是否有更好的方法在每个ou上添加换行符tput object?不幸的是,这个编辑对我没有帮助。但是,最后我用CSS解决了这个问题
    空白:空格;
    但是无论如何,你给了我一个不同的视角。我将把你的脚本标记为答案。非常感谢。@sumanchetri很高兴它对你有用。也许值得为答案添加一些解释,或者一些其他的关于您在应用程序中所做工作的内联注释code@MathiasR.Jessen同意,添加了解释和一些内联注释。这对我来说是很多新知识。我尝试了你的脚本。但是,我的第一个要求的第二部分仍然没有满足。PhyNIC和ConnectedDatastores显示连续数据,没有换行。如果我愿意的话若要为每个列添加列大小,我应该在何处以及如何添加?或者是否有更好的方法在每个输出对象上添加换行符?不幸的是,该编辑没有成功