Question

I am trying to create a log file that is edited a specific way.

I am not sure how to display specific properties of the objects that I am getting from WMI. I believe I need to include a where-object or a foreach-object, but my google-foo has not been strong enough so far.

Here is what I have so far:

$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
Was it helpful?

Solution

Keep in mind that it will depend on whether or not you're getting multiple WMI class instances back from the Get-WmiObject command or not.

If you want logical disk information for the c:\ drive, then you can do the following:

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

If you want BIOS information, do the following:

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

In the two prior examples, we are only dealing with a single WMI class instance: 1) a single "logical disk" instance, and 2) a single "BIOS" instance. If you're getting multiple WMI instances back (eg. multiple printer objects), you will have to iterate over each instance using a foreach loop:

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

On a side note, I would recommend avoiding the use of the Win32_Product WMI class, as it oddly invokes a repair on all MSI (Windows Installer) software packages. For more information about this issue, see this blog post: http://myitforum.com/cs2/blogs/gramsey/archive/2011/01/25/win32-product-is-evil.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top