#requires -version 2.0 # Jeffery Hicks # http://jdhitsolutions.com/blog # follow on Twitter: http://twitter.com/JeffHicks # # Learn more Windows PowerShell with a copy of Windows PowerShell 2.0: TFM (SAPIEN Press 2010) # # "Those who forget to script are doomed to repeat their work." # **************************************************************** # * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * # * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * # * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * # * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * # **************************************************************** Function New-WMIObject { <# .Synopsis Create a custom WMI object from a hashtable of WMI classes and properties. .Description Connect to a computer and create a custom WMI object based on a hash table of WMI Win32 classes and property names. #> [cmdletbinding()] Param ( [Parameter(Position=0,ValueFromPipeline=$True, HelpMessage="Enter a computername.")] [string[]]$computername=$env:computername, [Parameter(Position=1,ValueFromPipeline=$False, HelpMessage="Enter a hashtable of WMI classes and properties.", Mandatory=$True)] [hashtable]$hashtable ) Begin { write-verbose "Starting $($myinvocation.mycommand)" }#begin Process { foreach ($computer in $computername) { #initialize property hash $properties=@{} write-verbose "Adding $($computer.toUpper()) to `$properties" $properties.Add("Computer",$($computer.toUpper())) foreach ($hash in $hashtable) { $classes=$hash.keys foreach ($class in $classes) { write-verbose "Getting $class information on $computer" $wmi=Get-WmiObject -Class $class -Property $hash.item($class) ` -computername $computer #create array properties if $wmi returned more than one item if ($wmi.count -gt 1) { write-verbose "Multiple WMI objects returned" #initialize a temporary array $array=@() foreach ($object in $wmi) { Write-Verbose "Adding $($object.__RELPATH)" $array+=$object | select $hash.item($class) }#end foreach $object #add the array as a property value #strip off win32_ from the class name $parsed=$class.Substring($class.indexof("_")+1) $properties.Add($parsed,$array) } else { #add each property foreach ($item in $hash.item($class)) { write-verbose "Adding $item = $($wmi.($item))" $properties.Add($item,$($wmi.($item))) } } #else } #foreach $class } #foreach $hash Write-Verbose "Creating custom object" New-Object PSObject -Property $properties } #foreach $computer } #process End { write-verbose "Ending $($myinvocation.mycommand)" } #end } #end function #examples #$a=@{win32_OperatingSystem="Caption","RegisteredUser","LastBootUpTime"; #win32_computersystem="Name","Manufacturer","Model"; #win32_bios="Version"; #win32_logicaldisk="DeviceId","Size","FreeSpace","DriveType"} #$r="godot7","serenity" | new-wmiobject -hash $a #$r #$r | select-object -property computer -expand logicaldisk | #where {$_.drivetype -eq 3} | format-table -GroupBy Computer -Property DeviceID,Size,FreeSpace