I’ve been helping out on some WMI and PowerShell issues in the forums at ScriptingAnswers.com. As I was working on a problem I ended up taking a slight detour to address an issue that has always bugged me. When I run a command like this:
|
1 |
<span style="color: #0000ff;">get-wmiobject</span> <span style="color: #000080;">-query</span> <span style="color: #8b0000;">"Select Name,Description,Disabled from Win32_UserAccount"</span> |
PowerShell wants to return system classes as well.
__GENUS : 2
__CLASS : Win32_UserAccount
__SUPERCLASS :
__DYNASTY :
__RELPATH :
__PROPERTY_COUNT : 3
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
Description :
Disabled : False
Name : Jeff
The work around has been to pipe the original expression to Select-Object and re-selecting the properties I want. This seems like an unnecessary step. Now, depending on the class I could simple return all WMI objects and then use Select-Object. But I should be able to take advantage of early filtering and use Get-WmiObject as it was intended. So I wrote a function called Select-WMI that will take any WMI-looking object and return the non system properties.
|
1 2 3 4 5 6 |
<span style="color: #00008b;">Function</span> <span style="color: #8a2be2;">Select-WMI</span> <span style="color: #000000;">{</span> <span style="color: #006400;"><# .Synopsis Return non system properties from a WMI Object. .Description When using -query and Get-WmiObject, you get system properties included, </span> |
|
1 |
<span style="color: #006400;">like __CLASS and __SERVER. This function will strip off these properties</span> |
|
1 |
<span style="color: #006400;">and only return class properties. Use -Populated when you want to get </span> |
|
1 |
<span style="color: #006400;">all class properties but only if they have a value defined. This is </span> |
|
1 2 3 4 5 6 7 8 |
<span style="color: #006400;">similar to piping a WMI object to Select * except in this function you'll only get properties with a value. .Parameter InputObject A WMI object either from a Get-WMIObject expression. .Parameter Populated Only return properties that have a defined value. .Example PS C:\> Get-WMIObject -query "Select Model,Name,Manufacturer from </span> |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<span style="color: #006400;">win32_computersystem" | Select-WMI Manufacturer Model Name ------------ ----- ---- TOSHIBA Qosmio X505 SERENITY .Example PS C:\> Get-WMIObject win32_operatingsystem | Select-WMI BootDevice : \Device\HarddiskVolume1 BuildNumber : 7600 BuildType : Multiprocessor Free Caption : Microsoft Windows 7 Ultimate CodeSet : 1252 CountryCode : 1 CreationClassName : Win32_OperatingSystem CSCreationClassName : Win32_ComputerSystem CSDVersion : CSName : SERENITY CurrentTimeZone : -240 DataExecutionPrevention_32BitApplications : True DataExecutionPrevention_Available : True DataExecutionPrevention_Drivers : True DataExecutionPrevention_SupportPolicy : 2 Debug : False Description : Distributed : False EncryptionLevel : 256 ForegroundApplicationBoost : 2 FreePhysicalMemory : 2346688 FreeSpaceInPagingFiles : 4182504 FreeVirtualMemory : 6091932 InstallDate : 20100120131825.000000-300 LargeSystemCache : LastBootUpTime : 20100511175627.595198-240 LocalDateTime : 20100511180234.565000-240 Locale : 0409 Manufacturer : Microsoft Corporation MaxNumberOfProcesses : 4294967295 MaxProcessMemorySize : 8589934464 MUILanguages : {en-US} Name : Microsoft Windows 7 Ultimate </span> |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<span style="color: #006400;"> |C:\Windows|\Device\Harddi… NumberOfLicensedUsers : NumberOfProcesses : 80 NumberOfUsers : 5 OperatingSystemSKU : 1 Organization : JDH Information Technology… OSArchitecture : 64-bit OSLanguage : 1033 OSProductSuite : 256 OSType : 18 OtherTypeDescription : PAEEnabled : PlusProductID : PlusVersionNumber : Primary : True ProductType : 1 RegisteredUser : Jeff SerialNumber : 00426-065-0389393-86732 ServicePackMajorVersion : 0 ServicePackMinorVersion : 0 SizeStoredInPagingFiles : 4182504 Status : OK SuiteMask : 272 SystemDevice : \Device\HarddiskVolume2 SystemDirectory : C:\Windows\system32 SystemDrive : C: TotalSwapSpaceSize : TotalVirtualMemorySize : 8363108 TotalVisibleMemorySize : 4182504 Version : 6.1.7600 WindowsDirectory : C:\Windows .Example PS C:\> Get-WMIObject win32_bios -computername SERVER01 | </span> |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
<span style="color: #006400;">Select-WMI -Populated BiosCharacteristics : {4, 7, 8, 9...} BIOSVersion : {TOSQCI - 6040000, Ver 1.00PARTTBL} Caption : Ver 1.00PARTTBL Description : Ver 1.00PARTTBL Manufacturer : TOSHIBA Name : Ver 1.00PARTTBL PrimaryBIOS : True ReleaseDate : 20100223000000.000000+000 SerialNumber : Z9131790W SMBIOSBIOSVersion : V2.20 SMBIOSMajorVersion : 2 SMBIOSMinorVersion : 6 SMBIOSPresent : True SoftwareElementID : Ver 1.00PARTTBL SoftwareElementState : 3 Status : OK TargetOperatingSystem : 0 Version : TOSQCI - 6040000 .Example PS C:\> Select-wmi (gwmi win32_useraccount) | select -first 1 AccountType : 512 Caption : SERENITY\Administrator Description : Built-in account for administering the computer/… Disabled : False Domain : SERENITY FullName : InstallDate : LocalAccount : True Lockout : False Name : Administrator PasswordChangeable : True PasswordExpires : False PasswordRequired : True SID : S-1-5-21-2858895768-3673612314-3109562570-500 SIDType : 1 Status : OK .Inputs Any WMI or [system.management.managementobject] object .Outputs A WMI or [system.management.managementobject] object .Link Get-WMIObject Select-Object .Notes NAME: Select-WMI VERSION: 1.1 AUTHOR: Jeffery Hicks LASTEDIT: 5/11/2010 #></span> <span style="color: #a9a9a9;">[</span><span style="color: #add8e6;">CmdletBinding</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span><span style="color: #a9a9a9;">]</span> <span style="color: #00008b;">Param</span><span style="color: #000000;">(</span> <span style="color: #a9a9a9;">[</span><span style="color: #add8e6;">Parameter</span><span style="color: #000000;">(</span><span style="color: #000000;">Position</span><span style="color: #a9a9a9;">=</span><span style="color: #800080;">0</span><span style="color: #a9a9a9;">,</span><span style="color: #000000;">ValueFromPipeline</span><span style="color: #a9a9a9;">=</span><span style="color: #ff4500;">$True</span><span style="color: #000000;">)</span><span style="color: #a9a9a9;">]</span> <span style="color: #008080;">[object[]]</span><span style="color: #ff4500;">$InputObject</span><span style="color: #a9a9a9;">,</span> <span style="color: #008080;">[switch]</span><span style="color: #ff4500;">$Populated</span> <span style="color: #000000;">)</span> <span style="color: #00008b;">Begin</span> <span style="color: #000000;">{</span> <span style="color: #0000ff;">write-verbose</span> <span style="color: #8b0000;">"Starting $($myinvocation.MyCommand)"</span> <span style="color: #00008b;">if</span> <span style="color: #000000;">(</span><span style="color: #ff4500;">$populated</span><span style="color: #000000;">)</span> <span style="color: #000000;">{</span> <span style="color: #0000ff;">write-verbose</span> <span style="color: #8b0000;">"Populated specified"</span> <span style="color: #000000;">}</span> <span style="color: #000000;">}</span> <span style="color: #006400;">#Begin</span> <span style="color: #00008b;">Process</span> <span style="color: #000000;">{</span> <span style="color: #0000ff;">write-verbose</span> <span style="color: #8b0000;">"Analyzing object"</span> <span style="color: #00008b;">foreach</span> <span style="color: #000000;">(</span><span style="color: #ff4500;">$item</span> <span style="color: #00008b;">in</span> <span style="color: #ff4500;">$inputObject</span><span style="color: #000000;">)</span> <span style="color: #000000;">{</span> <span style="color: #006400;">#only process objects that look like WMI objects</span> <span style="color: #00008b;">if</span> <span style="color: #000000;">(</span><span style="color: #ff4500;">$item</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">__CLASS</span><span style="color: #000000;">)</span> <span style="color: #000000;">{</span> <span style="color: #0000ff;">write-verbose</span> <span style="color: #8b0000;">"Found $($item.psbase.properties.count) properties"</span> <span style="color: #006400;">#create an empty array to hold property names</span> <span style="color: #ff4500;">$properties</span><span style="color: #a9a9a9;">=</span><span style="color: #000000;">@(</span><span style="color: #000000;">)</span> <span style="color: #ff4500;">$populatedProperties</span><span style="color: #a9a9a9;">=</span><span style="color: #000000;">@(</span><span style="color: #000000;">)</span> <span style="color: #00008b;">foreach</span> <span style="color: #000000;">(</span><span style="color: #ff4500;">$property</span> <span style="color: #00008b;">in</span> <span style="color: #ff4500;">$item</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">properties</span><span style="color: #000000;">)</span> <span style="color: #000000;">{</span> <span style="color: #00008b;">if</span> <span style="color: #000000;">(</span><span style="color: #ff4500;">$Populated</span> <span style="color: #a9a9a9;">-AND</span> <span style="color: #ff4500;">$item</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">(</span><span style="color: #ff4500;">$property</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">name</span><span style="color: #000000;">)</span> <span style="color: #a9a9a9;">-ne</span> <span style="color: #ff4500;">$NULL</span><span style="color: #000000;">)</span> <span style="color: #000000;">{</span> <span style="color: #006400;">#only add property name if it has a value</span> <span style="color: #0000ff;">write-verbose</span> <span style="color: #8b0000;">"Value confirmed. Adding $($property.name)"</span> <span style="color: #006400;">#the following line will display the property value as a string</span> <span style="color: #006400;">#so you might get funny looking output</span> <span style="color: #006400;">#write-verbose ($item.($property.name) | out-string)</span> <span style="color: #ff4500;">$populatedProperties</span><span style="color: #a9a9a9;">+=</span><span style="color: #ff4500;">$property</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">name</span> <span style="color: #000000;">}</span> <span style="color: #00008b;">else</span> <span style="color: #000000;">{</span> <span style="color: #0000ff;">write-verbose</span> <span style="color: #8b0000;">"Adding $($property.name)"</span> <span style="color: #ff4500;">$properties</span><span style="color: #a9a9a9;">+=</span><span style="color: #ff4500;">$property</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">name</span> <span style="color: #000000;">}</span> <span style="color: #000000;">}</span> <span style="color: #0000ff;">write-verbose</span> <span style="color: #8b0000;">"Returning WMI Object"</span> <span style="color: #00008b;">if</span> <span style="color: #000000;">(</span><span style="color: #ff4500;">$populated</span><span style="color: #000000;">)</span> <span style="color: #000000;">{</span> <span style="color: #0000ff;">write-verbose</span> <span style="color: #8b0000;">"Returning $($populatedProperties.count) populated properties"</span> <span style="color: #ff4500;">$item</span> <span style="color: #a9a9a9;">|</span> <span style="color: #0000ff;">select</span> <span style="color: #ff4500;">$populatedProperties</span> <span style="color: #000000;">}</span> <span style="color: #00008b;">else</span> <span style="color: #000000;">{</span> <span style="color: #ff4500;">$item</span> <span style="color: #a9a9a9;">|</span> <span style="color: #0000ff;">select</span> <span style="color: #ff4500;">$properties</span> <span style="color: #000000;">}</span> <span style="color: #000000;">}</span> <span style="color: #00008b;">else</span> <span style="color: #000000;">{</span> <span style="color: #0000ff;">Write-warning</span> <span style="color: #8b0000;">"You did not pass a valid WMI Object"</span> <span style="color: #000000;">}</span> <span style="color: #000000;">}</span> <span style="color: #006400;">#ForEach</span> <span style="color: #000000;">}</span> <span style="color: #006400;">#Process</span> <span style="color: #00008b;">End</span> <span style="color: #000000;">{</span> <span style="color: #0000ff;">write-verbose</span> <span style="color: #8b0000;">"Ending $($myinvocation.MyCommand)"</span> <span style="color: #000000;">}</span> <span style="color: #006400;">#end</span> <span style="color: #000000;">}</span> <span style="color: #006400;">#end function</span> |
The function has complete help and examples. I assumed you would run it as part of a pipeline. Now I can more easily get the information I’m after.
PS C:\Scripts> get-wmiobject -query “Select Name,Description,Disabled from Win32_UserAccount” | Select-WMI
Description Disabled Name
———– ——– —-
Built-in account for ad… False Administrator
False bob
Built-in account for gu… True Guest
False Jeff
False sql
VMware User False __vmware_user__
The function will also work to bypass PowerShell’s default formatting for WMI objects, making it easier to see all the properties.
PS C:\Scripts> gwmi win32_bios | select-wmi
BiosCharacteristics : {4, 7, 8, 9…}
BIOSVersion : {TOSQCI – 6040000, Ver 1.00PARTTBL}
BuildNumber :
Caption : Ver 1.00PARTTBL
CodeSet :
CurrentLanguage :
Description : Ver 1.00PARTTBL
IdentificationCode :
InstallableLanguages :
InstallDate :
LanguageEdition :
ListOfLanguages :
Manufacturer : TOSHIBA
Name : Ver 1.00PARTTBL
OtherTargetOS :
PrimaryBIOS : True
ReleaseDate : 20100223000000.000000+000
SerialNumber : Z9131790W
SMBIOSBIOSVersion : V2.20
SMBIOSMajorVersion : 2
SMBIOSMinorVersion : 6
SMBIOSPresent : True
SoftwareElementID : Ver 1.00PARTTBL
SoftwareElementState : 3
Status : OK
TargetOperatingSystem : 0
Version : TOSQCI – 6040000
But I took this one step further. There are times when all I want are properties that have a value. So Select-WMI has a –Populated parameter so that only populated properties are displayed.
PS C:\Scripts> gwmi win32_bios | select-wmi -Populated
BiosCharacteristics : {4, 7, 8, 9…}
BIOSVersion : {TOSQCI – 6040000, Ver 1.00PARTTBL}
Caption : Ver 1.00PARTTBL
Description : Ver 1.00PARTTBL
Manufacturer : TOSHIBA
Name : Ver 1.00PARTTBL
PrimaryBIOS : True
ReleaseDate : 20100223000000.000000+000
SerialNumber : Z9131790W
SMBIOSBIOSVersion : V2.20
SMBIOSMajorVersion : 2
SMBIOSMinorVersion : 6
SMBIOSPresent : True
SoftwareElementID : Ver 1.00PARTTBL
SoftwareElementState : 3
Status : OK
TargetOperatingSystem : 0
Version : TOSQCI – 6040000
Do you think that’s helpful? You can download the script here.
