Windows Management InstrumentationWMI) is one of the tools that can change your daily life. Although it already exists in the early 1990s s, its promotion speed is very slow due to the complexity of WMI. Windows PowerShell breaks this obstacle and makes WMI easier to use, which also changes the IT industry.
Before focusing on how to use PowerShell to simplify WMI, let's take a look at what WMI is. This is the simplest term. You can think of WMI as a database full of consistent and reliable data formats for queries.
Wikipedia explains that WMI aims to "define a non-proprietary setting for an independent environment standard that allows management applications to share management information ." This is a rather abstract explanation, and WMI may have begun to make an "environment independent" attempt. Now the situation has changed. Let's start to think about what WMI will look like these days. When you hear about WMI today, it is usually used in Microsoft's WMI implementation environment through the built-in Supply Program. This is also the focus of this article.
WMI consists of three basic elements:
- Provider -- approve access to management objects and provide valid WMI APIs
- Classes -- WMI statements for objects with properties and Methods
- Namespace -- logical grouping of Classes
So how does PowerShell make WMI access easier?
First, let's take a look at the tools PowerShell provides for WMI. In general, there are five PowerShell command sets, which makes WMI easy to use. Here I will list them all, but I will only focus on one of the Get-WMIObject ):
- Get-WmiObject -- return an object based on the namespace and provided category
- Invoke-WmiMethod -- call a WMI program to execute a static program)
- Register-WmiEvent -- used to subscribe to WMI events
- Remove-WmiObject -- it is clear to delete an existing WMI class instance that does not actually Delete the class itself, but the instance of this class in the memory)
- Set-WmiInstance -- use it with caution when creating or updating an existing WMI class because it will actually be written to the WMI Library)
Now let's solve the biggest problem in WMI and find out what it is and what data it can provide.
You can use the following code to write a program:
$Root = "\\.\ROOT:__namespace"$WMIProv = New-Object System.Management.ManagementClass ($Root)$WMIProv.GetInstances() | Select Name
(However, this is more complex than other tasks. Fortunately, you don't have to do this frequently .)
The method for listing the classes provided through a specific namespace is Root \ CIM2 by default, which includes all Microsoft Win32 classes ):
# On local machineGet-WmiObject –Namespace Root\SecurityCenter –List# On Remote machineGet-WmiObject –Namespace Root\SecurityCenter –List –Computer core# To filter you can use wildcardsGet-WmiObject –Namespace Root\SecurityCenter –List *firewall*# To list the classes for HyperV on remote serverGet-WmiObject –Namespace Root\Virtualization –List –Computer core
This is tough, but you can cheat with free tools such as WMI browser or Microsoft PowerShell Scriptomatic .)
Now it's time to take a look at the unusual features of Get-WMIObject. It is the most useful of the five command sets. With it in the toolbox, you have almost all the systems related to Microsoft that you can think. There are more than 600 Win32 classes used to display information such as CPU, memory, disk, process, network, BIOS, and USB. Excited? Wait and you will know how simple it is.
Obtain information about the operating system:
Get-WmiObject –class win32_OperatingSystem
Obtains information about a computer system.
Get-WmiObject –class win32_ComputerSystem
Obtain disk information:
Get-WmiObject –class Win32_LogicalDisk
Obtain Network Information:
Get-WmiObject –class Win32_NetworkAdapterConfiguration
Just try-it's that simple.
Let's take a look at an example of getting IP information Using WMI. The following valid scripts replace ipconfig and its common awful output.
function Get-IP{[Cmdletbinding()]Param([alias('dnsHostName')][Parameter(ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true) [string]$ComputerName = $Env:COMPUTERNAME)process{$NICs = Get-WmiObjectWin32_NetworkAdapterConfiguration -Filter"IPEnabled='$True'" -ComputerName $ComputerNameforeach ($Nic in $NICs){$myobj = @{Name = $Nic.DescriptionMacAddress = $Nic.MACAddressIP4 = $Nic.IPAddress | where{$_-match"\d+\.\d+\.\d+\.\d+"}IP6 = $Nic.IPAddress | where{$_-match "\:\:"}IP4Subnet = $Nic.IPSubnet | where{$_-match"\d+\.\d+\.\d+\.\d+"}DefaultGWY = $Nic.DefaultIPGateway |Select -First 1DNSServer = $Nic.DNSServerSearchOrderWINSPrimary = $Nic.WINSPrimaryServerWINSSecondary = $Nic.WINSSecondaryServer}$obj = New-Object PSObject -Property $myobj$obj.PSTypeNames.Clear()$obj.PSTypeNames.Add('BSonPosh.IPInfo')$obj}}}
You can find more information about WMI on the Microsoft website, as well as a list of WMI terms and Win32 classes.