在 Powershell 中格式化输出

Format output in Powershell

我的脚本中有一段运行良好的小代码。我只是对输出感到恼火..

我的输出如下所示:

11.11.111.123

Model                                                                                                                                                                                  
-----                                                                                                                                                                                  
HP ZBook Studio G5                                                                                                                               

csname         : XXXXXXX
LastBootUpTime : 22/Apr/2022 08:10:57

但我想要这样:

IP Address:     11.11.111.123
Model:          HP ZBook Studio G5     
csname:         xxxxx
LastBootUpTime: 22/Apr/2022 08:10:57

这是脚本:

Get-WmiObject Win32_NetworkAdapterConfiguration -Computername $pcName |
      Where { $_.IPAddress } |
      Select -Expand IPAddress | 
      Where { $_ -like '10.11*' -or $_ -like '10.12*'}
   
Get-WmiObject -Class Win32_ComputerSystem -Computername $pcName | Select Model
   
Get-WmiObject win32_operatingsystem -Computername $pcName -ea stop | select csname, @{LABEL='LastBootUpTime';EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}} | format-list

由于输出是由 3 个不同的 类 产生的,解决方法是创建一个 new object 来合并它们:

$IPs = Get-CimInstance Win32_NetworkAdapterConfiguration -ComputerName $pcName |
    Where-Object { $_.IPAddress -like '10.11*' -or $_.IPAddress -like '10.12*' }
$Model = (Get-CimInstance -Class Win32_ComputerSystem -ComputerName $pcName).Model
$OS = Get-CimInstance win32_operatingsystem -EA Stop -ComputerName $pcName

[pscustomobject]@{
    'IP Address'   = $IPs.IpAddress -join ', '
    Model          = $Model 
    csname         = $OS.CSName
    LastBootUpTime = $OS.LastBootUpTime.ToString()
}