调用特定的Powershell WMI对象属性

时间:2014-02-17 02:30:45

标签: powershell wmi

我正在尝试创建以特定方式编辑的日志文件。

我不确定如何显示我从WMI获取的对象的特定属性。我相信我需要包含一个where-object或foreach-object,但到目前为止我的google-foo还不够强大。

这是我到目前为止所做的:

$LogPath = "$HOME\Documents\logs"
$LogFilePath = "$HOME\Documents\Logs\log1.log"
$DiskInfo = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'"
$BiosInfo = Get-wmiobject win32_Bios
$AppInfo = Get-WmiObject -Class Win32_Product


Write-Output "Summary Information for computer $computername on $Date" |Out-file -append -FilePath $LogFilePath
Write-Output "=============================================================== " | Out-file -append -filepath $LogFilePath

Write-Output "Disk Drive Summary" |out-file -append $LogFilePath
Write-Output "=======================================================================" | out-file -append -filepath $LogFilePath
Write-Output "Model                                                 Size" | Out-file -append -filepath  $LogFilePath
write-Output "--------                                             ------" | Out-file -append -filepath     $LogFilePath     
Write-Output "$diskinfo.deviceid                                      $diskinfo.size" | out-file -append -filepath $LogFilePath


Write-Output "BIOS Version Summary" |out-file -append -filepath $LogFilePath
Write-Output "=======================================================================" | out-file -append -filepath $LogFilePath
Write-Output $biosinfo|out-file -append -filepath  $LogFilePath

1 个答案:

答案 0 :(得分:0)

请记住,这取决于您是否从Get-WmiObject命令返回多个WMI类实例。

如果您需要c:\驱动器的逻辑磁盘信息,则可以执行以下操作:

 $Disk = Get-WmiObject -Class Win32_LogicalDisk -Filter "Name = 'c:'";
 $Disk.FreeSpace; # In bytes
 $Disk.Size; # In bytes

如果您需要BIOS信息,请执行以下操作:

 $BIOS = Get-WmiObject -Class Win32_BIOS;
 $BIOS.SMBIOSBIOSVersion;
 $BIOS.SerialNumber;

在前两个示例中,我们只处理单个WMI类实例:1)单个“逻辑磁盘”实例,以及2)单个“BIOS”实例。如果您正在获取多个WMI实例(例如,多个打印机对象),则必须使用foreach循环遍历每个实例:

 $PrinterList = Get-WmiObject -Class Win32_Printer;
 foreach ($Printer in $PrinterList) {
      $Printer.Name;
 }

另外,我建议避免使用Win32_Product WMI类,因为它奇怪地在所有MSI(Windows Installer)软件包上调用修复。有关此问题的详细信息,请参阅此博客文章:http://myitforum.com/cs2/blogs/gramsey/archive/2011/01/25/win32-product-is-evil.aspx