Creating A Computer Profile

by Mar 17, 2009

Often, information needed to comprehensively profile a computer comes from a number of sources. A great way to meld these different bits of information together is by creating a new result object with just the properties you need. Simply,, use any simple object like a number or an empty string, and append the needed properties with Select-Object:

$Info = 0 | select Name, OS,SP,Hotfixes, Software, lastboot, Services, Model
$info.SP = "Hello"

Next, you can use WMI and other technologies to automatically fill your new object. Find a function called Get-Computerinfo that will optionally accept a remote computer name or IP address. It will then retrieve the information using WMI and .NET and combine all information collected into a newly returned result object.

Be aware that this function can be time-consuming to execute. Enumerating installed software and Hotfixes can take up to a couple of minutes to succeed. This is why both calls are commented out below. Comment them in as needed:

function global:Get-ComputerInfo
param($server = '')

[system.Reflection.Assembly]::LoadWithPartialName("System.ServiceProcess") > $null
$Info = 0 | select Name, OS,SP,Hotfixes, Software, lastboot, Services, Model
$Info.Name = $server
$Info.OS = (Get-WmiObject -computerName $server -class Win32_OperatingSystem).version
$Info.SP = (Get-WmiObject -computerName $server -class Win32_OperatingSystem).ServicePackMajorVersion
$boot = (Get-WmiObject -computerName $server -class Win32_OperatingSystem).lastbootuptime
$Info.lastboot = [system.Management.ManagementDateTimeConverter]::ToDateTime($boot)
#$Info.Hotfixes = @(Get-WmiObject -computerName $server -class Win32_QuickFixEngineering |
# select HotfixID, InstalledBy, InstalledOn |where {$_.InstalledOn})
#$Info.Software = @(Get-WmiObject -computerName $server -class Win32_Product) |
# select Name, version
$Info.Services = [ServiceProcess.ServiceController]::GetServices($server)
$Info.model = (Get-WmiObject -computerName $server -class Win32_ComputerSystem).model