First, let’s understand what a WMI object is:
WMI exists as a basic database on Windows systems. We can connect to the WMI service request to query the information contained in it. WMI includes information on all aspects of the system, including:
• Machine information: manufacturer, model, serial number, etc.
• BIOS information
• OS information
• CPU information: type, manufacturer, speed, version
• Total server memory
• Disk information: capacity, format, etc.
• Network information: MAC, IP, etc.
• other
It can be seen how rich WMI content is, including almost all aspects of the computer.
View WMI members with PowerShell
List WMI objects in PowerShell with the following command:
get-wmiObject -list -namespace “root\CIMV2″ <enter>
You need to view the members of a certain class in detail and use the following command (for example, class "win32_process"):
get-wmiobject -class win32_process -namespace "root\cimv2" | get-member
You can see that some members are properties, while others are methods.
Then, there are two problems:
Why use -namespace "root\cimv2"?
cimv2 is a namespace of WMI, with different WMI object members under each namespace. cimv2 is its default setting. You can modify it according to the following steps:
Control Panel -> Administrative Tools -> Computer Management -> Services and Applications -> Right-click "WMI Control" -> Properties -> Advanced
The parameter "-namespace" is not necessary, but there are two benefits to using it. One is to ensure that we can accurately view the WMI objects under the specified namespace, because sometimes the default namespace is not what we want to view; the other is that if the namespace is not specified, the computer that has been set may deny our access request.
Note: Because I use the English version of the system, individual names in the above steps may be inaccurate.
In the interface shown in the figure above, you can modify the default path.
Another question is, what is the use of looking at the type of member?
If a member is a method, then we can call it. If a member is a property, we can view its value. However, it should be noted that different attribute members have different data structures, some are "", some are "System.UInt32", and some are "[ ]". When using it, you should pay attention to the data format, otherwise an error will be reported.
If we need to manage computers in the network, we need to specify the computer name:
get-wmiObject -list -namespace "root\CIMV2" -computername Computer name <enter>
Okay, let’s take a look at the specific information now.
example:
View BIOS information
get-wmiobject -class win32_bios -namespace "root\cimv2" <enter>
Running results:
View service information
get-wmiobject -class win32_service -namespace "root\cimv2" | format-list * <enter> View machine information
get-wmiobject -class win32_computersystem | format-list * <enter>
In most cases in a network management environment, we may want to view different computer information, so we need to use this parameter of computer, for example:
Query the network information of the local computer
$name="."
$items = get-wmiObject -class win32_NetworkAdapterConfiguration '
-namespace "root\CIMV2" -ComputerName $name | where{$_.IPEnabled -eq “True”}
foreach($obj in $items) {
Write-Host "DHCP Enabled:" $
Write-Host "IP Address:" $
Write-Host "Subnet Mask:" $
Write-Host "Gateway:" $
Write-Host "MAC Address:" $
}
If the query object is another machine, just assign other values to the variable "$name".
For example:
$name=read-host "Enter Computer Name"
write-host "Computer:"$name
$items = get-wmiObject -class win32_NetworkAdapterConfiguration '
-namespace "root\CIMV2" -ComputerName $name | where{$_.IPEnabled -eq “True”}
foreach($obj in $items) {
Write-Host "DHCP Enabled:" $
Write-Host "IP Address:" $
Write-Host "Subnet Mask:" $
Write-Host "Gateway:" $
Write-Host "MAC Address:" $
}
With more query commands, you can easily write a script for querying computer information. At the end of this tutorial, I will provide as many common scripts as possible. This is the end of this section.