Sharat Padmanabhan
Sharat Padmanabhan

Reputation: 33

How to combine output from Get-CimInstance and Get-WMIObject functions

I have got 2 powershell scripts, which I have mentioned below. I am looking for a way to combine both of these scripts. However I am unable to do so as one of the script is using CIM method and the other one is using the WMI method.

What I am trying to accomplish, is to provide the last reboot time and free space available of the same server (User has to enter the server name and on pressing Enter it shows the last reboot time and free space available).

Script 1 (CIM Method) :

$Server = Read-Host -Prompt 'Input your server  name'
Get-CimInstance -ClassName win32_operatingsystem -ComputerName $Server | select csname, lastbootuptime
Read-Host -Prompt "Press Enter to exit"

Script 2 (WMI Method) :

$Server = Read-Host -Prompt 'Input your server  name'
Get-WMIObject Win32_Logicaldisk -ComputerName $Server | Select PSComputername,DeviceID, @{Name="Total_Size_GB";Expression={$_.Size/1GB -as [int]}}, @{Name="Free_Space_GB";Expression={[math]::Round($_.Freespace/1GB,2)}}
Read-Host -Prompt "Press Enter to exit"

Upvotes: 3

Views: 2158

Answers (1)

G42
G42

Reputation: 10019

Store the results of your queries in a variable. Then create a psobject with the values you are interested in from each function. You can find out more about New-Object psobject here.

$Server = Read-Host -Prompt 'Input your server  name'

$myCimResp = Get-CimInstance -ClassName win32_operatingsystem -ComputerName $Server | select csname, lastbootuptime
$myWmiResp = Get-WMIObject Win32_Logicaldisk -ComputerName $Server | Select PSComputername,DeviceID, @{Name="Total_Size_GB";Expression={$_.Size/1GB -as [int]}}, @{Name="Free_Space_GB";Expression={[math]::Round($_.Freespace/1GB,2)}}

$theThingIActuallyWant = New-Object psobject -Property @{
    LastRebootTime = $myCimResp.lastbootuptime
    FreeSpace      = $myWmiResp.Free_Space_GB
}

# A couple of ways to print; delete 1, keep 1
Write-Output $theThingIActuallyWant      # Print to screen with implicit rules
$theThingIActuallyWant | Format-Table    # Print to screen with explicit table formatting


Read-Host -Prompt "Press Enter to exit"

Upvotes: 1

Related Questions