Skip to main content

Use .NET to Perform Advanced WMI Tasks

Problem

You want to work with advanced features of WMI, but PowerShell’s access (through the [Wmi], [WmiClass], and [WmiSearcher] accelerators) does not directly support them.

Solution

To interact with advanced features through their .NET interface, use the PsBase property of the resulting objects.

Advanced instance features

To get WMI instances related to a given instance, call the GetRelated() method:

$instance = [Wmi] 'Win32_Service.Name="winmgmt"' $instance.PsBase.GetRelated()

To enable security privileges for a command that requires them (such as changing the system time), set the EnablePrivileges property to $true:

$system = GetWmiObject Win32_OperatingSystem $system.PsBase.Scope.Options.EnablePrivileges = $true $system.SetDateTime($class.ConvertFromDateTime("01/01/2007"))

Advanced class features

To retrieve the WMI properties and qualifiers of a class, access the PsBase. Properties property: $class = [WmiClass] "Win32_Service" $class.PsBase.Properties

Advanced query feature

To configure connection options, such as Packet Privacy and Authentication, set the options on the Scope property:

$credential = GetCredential $query = [WmiSearcher] "SELECT * FROM IISWebServerSetting" $query.Scope.Path = "\\REMOTE_COMPUTER\Root\MicrosoftIISV2" $query.Scope.Options.Username = $credential.Username $query.Scope.Options.Password = $credential.GetNetworkCredential().Password $query.Scope.Options.Authentication = "PacketPrivacy" $query.get() | SelectObject AnonymousUserName

Discussion

The [Wmi], [WmiClass], and [WmiSearcher] type shortcuts return instances of .NET System.Management.ManagementObject, System.Management.ManagementClass, and System.Management.ManagementObjectSearcher classes, respectively.

As might be expected, the .NET Framework provides comprehensive support for WMI queries, with PowerShell providing an easiertouse interface to that support. If you need to step outside the support offered directly by PowerShell, these classes in the .NET Framework provide an advanced outlet. Perform advanced WMI tasks with .NET efficiently on a United Kingdom Dedicated Server, providing dedicated resources, reliable performance, and full administrative control for monitoring, managing, and automating server systems.

Help Category