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

标签 windows powershell wmi

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

我想要完成的是提供同一台服务器的上次重启时间和可用空间(用户必须输入服务器名称,然后按 Enter 它会显示上次重启时间和可用空间)。

脚本 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"

关于windows - 如何合并 Get-CimInstance 和 Get-WMIObject 函数的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44257389/

相关文章:

windows - RegSvr32.exe的/n和/i参数有什么区别?

c# - 如何在 C# 中访问 WinRM

vbscript - 杀死所有具有特定名称的进程

c - c (libzmq) 中的 Zeromq pub/sub 示例带有工作过滤器(在 Windows 上)?

windows - 如何使用 Windows 命令行下载文件而无需输入代理密码?

windows - 如何在具有不同编码的异构环境中使用maven?

azure - 从提取的 hwid.csv 中获取序列号并使用它重命名 csv 文件

powershell - openfiles.exe的格式表输出

powershell - 在 Powershell 中应该使用什么异常类型来捕获由于无效字符引起的 XML 解析错误?

Python:获取U盘设备名称[windows]