1

I have a small code in my script that is working well. I'm just annoyed with the output..

My output looks like this:

11.11.111.123

Model                                                                                                                                                                                  
-----                                                                                                                                                                                  
HP ZBook Studio G5                                                                                                                               

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

But I want it like this:

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

This is the script:

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
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
Toon
  • 29
  • 8
  • 2
    As an aside: The CIM cmdlets (e.g., `Get-CimInstance`) superseded the WMI cmdlets (e.g., `Get-WmiObject`) in PowerShell v3 (released in September 2012). Therefore, the WMI cmdlets should be avoided, not least because PowerShell (Core) v6+, where all future effort will go, doesn't even _have_ them anymore. Note that WMI still _underlies_ the CIM cmdlets, however. For more information, see [this answer](https://stackoverflow.com/a/54508009/45375). – mklement0 Apr 22 '22 at 12:57

1 Answers1

2

Since the output is produced by 3 different classes the way around it is create a new object to merge them:

$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()
}
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37