I wrote this a while ago to bring back the contents of a particular wmi class using powershell. It was orginally only developed to have a quick look at some wmi class settings. However as I continue to find it useful I have decided to repost the latest version.
(I had posted this before back in july 2011, but I had not realised that the version on my old blog was incorrect.)
Usage Examples:
Get-WMIClassInstanceAsXML "root\cimv2" "Win32_ComputerSystem"
Get-WMIClassInstanceAsXML "root\cimv2" "Win32_OperatingSystem"
Get-WMIClassInstanceAsXML "root\cimv2" "Win32_LoggedOnUser"
Get-WMIClassInstanceAsXML "root\cimv2" "Win32_LogicalDisk"
Get-WMIClassInstanceAsXML "root\cimv2" "Win32_Printer" "ComputerA"
#http://jongurgul.com/blog/wmi-classes-powershell/ [Reflection.Assembly]::LoadWithPartialName("System.Web") | Out-Null; Function Get-WMIClassInstanceAsXML($Path="root\cimv2", $Class = "Win32_Processor", $Computer= $Env:COMPUTERNAME){ $StringBuilder = New-Object "System.Text.StringBuilder" $ManagementPath = New-Object System.Management.ManagementPath("\\$Computer\$Path`:$Class"); $ManagementScope = New-Object System.Management.ManagementScope($ManagementPath); $ObjectGetOptions = New-Object System.Management.ObjectGetOptions; $ObjectGetOptions.UseAmendedQualifiers = $True; $ManagementScope.Connect(); $ManagementClass = New-Object System.Management.ManagementClass($ManagementScope,$ManagementPath,$ObjectGetOptions); [Void]$StringBuilder.Append("<$Class>"); $ManagementClass.PSBase.GetInstances()|%{ $_.PSBase.Properties|%{ If($_.Value){[Void]$StringBuilder.Append("<$($_.Name)>$([System.Web.HttpUtility]::HtmlEncode($_.Value).Replace([Char]0x1F, ' '))</$($_.Name)>")} } } [Void]$StringBuilder.Append("</$Class>"); $StringBuilder.ToString(); }
Leave a Reply