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
1
  • 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. Commented Apr 22, 2022 at 12:57

1 Answer 1

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()
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.