Message from discussion
Multiple wmi in one table
Date: Thu, 30 Oct 2008 14:41:31 +0000 (UTC)
Message-ID: <ef37c5c81c68cb089334b55b19@nn.bloomberg.com>
From: Brandon Shell [MVP] <a_bshell.m...@hotmail.com>
Subject: Re: Multiple wmi in one table
References: <3C4A74B9-E748-4ABE-9A8E-1EE58F9C224B@microsoft.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset=iso-8859-1; format=flowed
X-Newsreader: JetBrains Omea Reader 1098.1
Newsgroups: microsoft.public.windows.powershell
NNTP-Posting-Host: dns004.bloomberg.com 199.172.169.20
Lines: 1
Path: g2news1.google.com!news1.google.com!news2.google.com!news.glorb.com!news2!solnet.ch!solnet.ch!newsfeed.freenet.de!newsfeed00.sul.t-online.de!t-online.de!TK2MSFTFEEDS02.phx.gbl!TK2MSFTNGP01.phx.gbl!TK2MSFTNGP05.phx.gbl
Here is an example script that does what your looking for (sorta)
Param($server,$file,[switch]$verbose)
if($verbose){$verbosepreference = "continue"}else{$erroractionpreference
= "SilentlyContinue"}
function GetCompInfo{
Param($srv)
Write-Verbose " + Pinging $Srv"
$query = "Select statuscode FROM Win32_PingStatus WHERE Address='$srv'"
$pingresult = Get-WmiObject -query $query
if($pingresult.statuscode -eq 0)
{
# Get Info
Write-Verbose " - Ping Success"
Write-Verbose " - Getting WMI Info for Win32_ComputerSystem"
$CompInfo = Get-WmiObject Win32_ComputerSystem -comp $srv
Write-Verbose " - Getting WMI Info for Win32_OperatingSystem"
$OSInfo = Get-WmiObject Win32_OperatingSystem -comp $srv
Write-Verbose " - Getting WMI Info for Win32_BIOS"
$BiosInfo = Get-WmiObject Win32_BIOS -comp $srv
Write-Verbose " - Getting CPU Info for Win32_Processor"
$CPUInfo = Get-WmiObject Win32_Processor -comp $srv
# Creat Object
Write-Verbose " + Creating Custom Object"
$myobj = "" | Select-Object Name,Domain,Model,MachineSN,CPUs,CPUSpeed,RAM,OS,WindowsSN,Disk
$myobj.Name = $CompInfo.DNSHostName
$myobj.Domain = $CompInfo.Domain
$myobj.Model = $CompInfo.Model
$myobj.MachineSN = $BiosInfo.SerialNumber
$myobj.CPUs = $CPUInfo.Count
$myobj.CPUSpeed = $CPUInfo | %{$_.MaxClockSpeed}
$myobj.RAM = "{0:n2}GB" -f ($CompInfo.TotalPhysicalMemory/1gb)
$myobj.OS = $OSInfo.Caption
$myobj.WindowsSN = $OSInfo.SerialNumber
Write-Verbose " + Getting Drive Info"
$myobj.Disk = GetDriveInfo $srv
Write-Verbose " - Returning Custom Object"
$myobj
}
else
{
Write-Verbose " - Ping Failed!"
Write-Host "Error: $srv not Pingable" -fore RED
}
}
function GetDriveInfo{
Param($srv)
Write-Verbose " - Getting Drive Info"
$logicalDisk = Get-WmiObject Win32_LogicalDisk -Filter "DriveType=3"
-ComputerName $srv
foreach($disk in $logicalDisk)
{
$diskObj = "" | Select-Object Disk,Size,FreeSpace
$diskObj.Disk = $disk.DeviceID
$diskObj.Size = "{0:n2}GB" -f (($disk | Measure-Object -Property
Size -Sum).sum/1gb)
$diskObj.FreeSpace = "{0:n2}GB" -f (($disk | Measure-Object -Property
FreeSpace -Sum).sum/1gb)
if($msg)
{
$text = ";`"{0},Size:{1},Free:{2}`"" -f $diskObj.Disk,$diskObj.size,$diskObj.Freespace
Write-Verbose " - Adding $text"
$msg += $text
}
else
{
$text = "`"{0},Size:{1},Free:{2}`"" -f $diskObj.Disk,$diskObj.size,$diskObj.Freespace
Write-Verbose " - Adding $text"
$msg = $text
}
}
Write-Verbose " - $msg"
$msg
}
function Ping-Server {
Param([string]$server)
$pingresult = Get-WmiObject win32_pingstatus -f "address='$Server'"
if($pingresult.statuscode -eq 0) {$true} else {$false}
}
if($file -and (test-Path $file))
{
foreach($comp in (get-Content $file))
{
Write-Verbose " + Processing Computer [$Comp]"
Write-Host " + Processing Computer [$Comp]"
GetCompInfo $comp
}
}
if($input)
{
foreach($comp in $input)
{
Write-Verbose " + Processing Computer [$Comp]"
Write-Host " + Processing Computer [$Comp]"
GetCompInfo $comp
}
}
if($server){Write-Verbose " + Processing Computer [$server]";GetCompInfo
$server}
Brandon Shell
---------------
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject
T> Hi
T>
T> Im trying to get multiple pieces of information from more than one
T> namespace.
T>
T> e.g. win32_bios serialnumber and win32_computersystem name, model
T> ect.
T>
T> I can get meaningful tables with titles ect when I use only one but
T> not two.
T>
T> $serial = gwmi win32_bios
T> gwmi win32_computersystem|ft name , model, $serial.serialnumber
T> This only gives me titles for the win32_computersystem class
T>
T> Thanks
T>
T> Paul
T>