Querying information by using WMI and CIM
This lab should take approximately 45 minutes to complete.
Scenario
You have to query management information from several computers. You start by querying the information from your local computer and from one test computer in your environment.
Objectives
After completing this lab, you’ll be able to:
- Query information by using Windows Management Instrumentation (WMI) commands.
- Query information by using Common Information Model (CIM) commands.
- Invoke methods by using WMI and CIM commands.
Lab setup
Virtual machines: AZ-040T00A-LON-DC1 and AZ-040T00A-LON-CL1
Username: Adatum\Administrator
Password: Pa55w.rd
For this lab, you’ll use the available virtual machine environment. Before you begin the lab, complete the following steps:
- Open LON-DC1 and sign in as Adatum\Administrator with the password Pa55w.rd.
- Repeat step 1 for LON-CL1.
Exercise 1: Querying information by using WMI
Note: The
Get-WmiObjectcmdlet is only available in Windows PowerShell 5.1. In PowerShell 7, useGet-CimInstanceinstead (as shown in Exercise 2). Ensure you run this exercise in a Windows PowerShell console.
Scenario 1
In this exercise, you’ll discover repository classes and then use WMI commands to query them.
The main tasks for this exercise are as follows:
- Query IP addresses.
- Query operating system version information.
- Query computer system hardware information.
- Query service information.
Task 1: Query IP addresses
- On LON-CL1, select Start, and then enter powersh.
- In the results list, right-click Windows PowerShell or activate its context menu, and then select Run as administrator.
-
To find a repository class that lists the IP addresses being used by a computer, enter the following command in the Windows PowerShell console, and then press the Enter key:
Get-WmiObject -Namespace root\cimv2 -List | Where Name -like '*configuration*' | Sort NameNotice the
Win32_NetworkAdapterConfigurationclass. -
To retrieve all instances of the class depicting static IP addresses, enter the following command in the Windows PowerShell console, and then press the Enter key:
Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where DHCPEnabled -eq $False | Select IPAddressRemember that you can run the first command and pipe its output to Get-Member to review the properties that are available.
Task 2: Query operating system version information
-
To find a repository class that lists operating system information, enter the following command in the Windows PowerShell console, and then press the Enter key:
Get-WmiObject -Namespace root\cimv2 -List | Where Name -like '*operating*' | Sort NameNotice the
Win32_OperatingSystemclass. -
To display a list of properties for the class, enter the following command in the Windows PowerShell console, and then press the Enter key:
Get-WmiObject -Class Win32_OperatingSystem | Get-Member -
Notice the Version, ServicePackMajorVersion, and BuildNumber properties.
-
To display the specified information, enter the following command in the Windows PowerShell console, and then press the Enter key:
Get-WmiObject -Class Win32_OperatingSystem | Select Version,ServicePackMajorVersion,BuildNumber
Task 3: Query computer system hardware information
-
To find a repository class that displays computer system information, enter the following command in the Windows PowerShell console, and then press the Enter key:
Get-WmiObject -Namespace root\cimv2 -List | Where Name -like '*system*' | Sort NameNotice the
Win32_ComputerSystemclass. -
To display a list of instance properties and values, enter the following command in the Windows PowerShell console, and then press the Enter key:
Get-WmiObject -Class Win32_ComputerSystem | Format-List -Property *Remember that
Get-Memberdoesn’t display property values, butFormat-Listcan. -
To display the specified information, enter the following command in the Windows PowerShell console, and then press the Enter key:
Get-WmiObject -Class Win32_ComputerSystem | Select Manufacturer,Model,@{n='RAM';e={$PSItem.TotalPhysicalMemory}}
Task 4: Query service information
-
To find a repository class that contains information about services, enter the following command in the Windows PowerShell console, and then press the Enter key:
Get-WmiObject -Namespace root\cimv2 -List | Where Name –like '*service*' | Sort NameNotice the
Win32_Serviceclass. -
To display a list of instance properties and values, enter the following command in the Windows PowerShell console, and then press the Enter key:
Get-WmiObject -Class Win32_Service | FL * -
To display the specified information, enter the following command in the Windows PowerShell console, and press the Enter key:
Get-WmiObject –Class Win32_Service –Filter "Name LIKE 'S%'" | Select Name,State,StartName -
Leave the Windows PowerShell console open for the next exercise.
Exercise 2: Querying information by using CIM
Scenario 2
In this exercise, you’ll discover new repository classes and query them by using CIM commands.
The main tasks for this exercise are as follows:
- Query user accounts.
- Query BIOS information.
- Query network adapter configuration information.
- Query user group information.
Task 1: Query user accounts
-
To find a repository class that lists user accounts, enter the following command in the Windows PowerShell console, and then press the Enter key:
Get-CimClass -ClassName *user*Notice the
Win32_UserAccountclass. -
To display a list of class properties, enter the following command in the Windows PowerShell console, and then press the Enter key:
Get-CimInstance -Class Win32_UserAccount | Get-Member -
To display the specified information, enter the following command in the Windows PowerShell console, and then press the Enter key:
Get-CimInstance -Class Win32_UserAccount | Format-Table -Property Caption,Domain,SID,FullName,NameNotice the returned list of all domain and local accounts.
Task 2: Query BIOS information
-
To find a repository class that contains BIOS information, enter the following command in the Windows PowerShell console, and then press the Enter key:
Get-CimClass -ClassName *bios*Notice the
Win32_BIOSclass. -
To display the specified information, enter the following command in the Windows PowerShell console, and then press the Enter key:
Get-CimInstance -Class Win32_BIOS
Task 3: Query network adapter configuration information
-
To display a list of all the local
Win32_NetworkAdapterConfigurationinstances, enter the following command in the Windows PowerShell console, and then press the Enter key:Get-CimInstance -Classname Win32_NetworkAdapterConfiguration -
To display a list of all the
Win32_NetworkAdapterConfigurationinstances that exist on LON-DC1, enter the following command in the Windows PowerShell console, and then press the Enter key:Get-CimInstance -Classname Win32_NetworkAdapterConfiguration -ComputerName LON-DC1
Task 4: Query user group information
-
To find a repository class that lists user groups, enter the following command in the Windows PowerShell console, and then press the Enter key:
Get-CimClass -ClassName *group*Notice the
Win32_Groupclass. -
To display the specified information, enter the following command in the Windows PowerShell console, and then press the Enter key:
Get-CimInstance -ClassName Win32_Group -ComputerName LON-DC1 -
Leave the Windows PowerShell console open for the next exercise.
Exercise 3: Invoking methods
Scenario 3
In this exercise, you’ll use WMI and CIM commands to invoke methods of repository objects.
The main tasks for this exercise are as follows:
- Invoke a CIM method.
- Invoke a WMI method.
Task 1: Invoke a CIM method
-
To restart LON-DC1, enter the following command in the Windows PowerShell console, and then press the Enter key:
Invoke-CimMethod -ClassName Win32_OperatingSystem -ComputerName LON-DC1 -MethodName RebootNotice the response that includes ReturnValue=0 and PSComputerName=LON-DC1.
- Switch to the LON-DC1 virtual machine and observe it restarting.
- When the restart is complete, switch back to the LON-CL1 virtual machine.
Task 2: Invoke a WMI method
Note:
Get-WmiObjectandInvoke-WmiMethodare only available in Windows PowerShell 5.1. In PowerShell 7, use CIM cmdlets:Get-CimInstance -ClassName Win32_Service -Filter "Name='WinRM'" | Invoke-CimMethod -MethodName ChangeStartMode -Arguments @{StartMode='Automatic'}.
-
To review properties of the WinRM service, enter the following command in the Windows PowerShell console, and then press the Enter key:
Get-Service WinRM | FL *Note that the StartType is Manual.
-
To change the start mode of the specified service, enter the following command in the Windows PowerShell console, and then press the Enter key:
Get-WmiObject -Class Win32_Service -Filter "Name='WinRM'" | Invoke-WmiMethod -Name ChangeStartMode -ArgumentList 'Automatic' -
To verify that the StartType of the WinRM service has changed, enter the following command in the Windows PowerShell console, and then press the Enter key:
Get-Service WinRM | FL *Note that the StartType is Automatic.