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:

  1. Open LON-DC1 and sign in as Adatum\Administrator with the password Pa55w.rd.
  2. Repeat step 1 for LON-CL1.

Exercise 1: Querying information by using WMI

Note: The Get-WmiObject cmdlet is only available in Windows PowerShell 5.1. In PowerShell 7, use Get-CimInstance instead (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:

  1. Query IP addresses.
  2. Query operating system version information.
  3. Query computer system hardware information.
  4. Query service information.

Task 1: Query IP addresses

  1. On LON-CL1, select Start, and then enter powersh.
  2. In the results list, right-click Windows PowerShell or activate its context menu, and then select Run as administrator.
  3. 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 Name
    

    Notice the Win32_NetworkAdapterConfiguration class.

  4. 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 IPAddress
    

    Remember 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

  1. 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 Name
    

    Notice the Win32_OperatingSystem class.

  2. 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
    
  3. Notice the Version, ServicePackMajorVersion, and BuildNumber properties.

  4. 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

  1. 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 Name 
    

    Notice the Win32_ComputerSystem class.

  2. 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-Member doesn’t display property values, but Format-List can.

  3. 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

  1. 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 Name
    

    Notice the Win32_Service class.

  2. 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 *
    
  3. 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
    
  4. 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:

  1. Query user accounts.
  2. Query BIOS information.
  3. Query network adapter configuration information.
  4. Query user group information.

Task 1: Query user accounts

  1. 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_UserAccount class.

  2. 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
    
  3. 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,Name
    

    Notice the returned list of all domain and local accounts.

Task 2: Query BIOS information

  1. 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_BIOS class.

  2. 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

  1. To display a list of all the local Win32_NetworkAdapterConfiguration instances, enter the following command in the Windows PowerShell console, and then press the Enter key:

    Get-CimInstance -Classname Win32_NetworkAdapterConfiguration
    
  2. To display a list of all the Win32_NetworkAdapterConfiguration instances 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

  1. 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_Group class.

  2. 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
    
  3. 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:

  1. Invoke a CIM method.
  2. Invoke a WMI method.

Task 1: Invoke a CIM method

  1. 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 Reboot
    

    Notice the response that includes ReturnValue=0 and PSComputerName=LON-DC1.

  2. Switch to the LON-DC1 virtual machine and observe it restarting.
  3. When the restart is complete, switch back to the LON-CL1 virtual machine.

Task 2: Invoke a WMI method

Note: Get-WmiObject and Invoke-WmiMethod are 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'}.

  1. 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.

  2. 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'
    
  3. 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.