如何合并 Get-CimInstance 和 Get-WMIObject 函数的输出

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

我有 2 个 powershell 脚本,我在下面提到过。我正在寻找一种结合这两个脚本的方法。但是我无法这样做,因为其中一个脚本使用的是 CIM 方法,而另一个脚本使用的是 WMI 方法。

我想要完成的是提供同一服务器的最后一次重启时间和可用的 space(用户必须输入服务器名称并在按下 Enter 时显示上次重启时间和免费 space 可用)。

脚本 1(CIM 方法):

$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"

脚本 2(WMI 方法):

$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"

将查询结果存储在一个变量中。然后用每个函数中您感兴趣的值创建一个 psobjectYou 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"