Creating A Computer Profile

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

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 = '127.0.0.1')

[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
$info
}

Posted Mar 17 2009, 08:00 AM by ps1

Comments

Aleksandar wrote re: Creating A Computer Profile
on 04-14-2009 2:12 PM

There is no need to target remote computer three times for the same WMI class. Also, we can use ConvertToDateTime() method. It's easier to discover and remember than long .NET class and its static method.

$OS = Get-WmiObject -computerName $server -class Win32_OperatingSystem

$Info.OS = $OS.version

$Info.SP = $OS.ServicePackMajorVersion

$Info.lastboot = $OS.ConvertToDateTime($OS.lastbootuptime)

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.