Invoke CimMethod - PowerShell


Overview

Invoke-CimMethod allows you to remotely invoke a method on a Common Information Model (CIM) object. This command provides access to the WMI (Windows Management Instrumentation) infrastructure, enabling you to interact with remote systems and retrieve or modify information. It’s primarily used for administration, monitoring, and troubleshooting tasks within Windows environments.

Syntax

Invoke-CimMethod [-ComputerName] <string[]> [-Namespace] <string> [-ClassName] <string> [-MethodName] <string> [-Arguments] <object[]> [-Credential] <PSCredential> [-Authentication] <AuthenticationLevel> [-UseSsl] [-ImpersonationLevel] <ImpersonationLevel> [-SessionOption] <SessionOption> [-ConnectionTimeout] <int> [-OperationTimeout] <int> [-SkipCACheck] [-DisableCertificateValidation] [-CimSession] <CimSession> [-ErrorAction] <ActionPreference>

Options/Flags

  • -ComputerName: Specifies the target computer where the method invocation will occur. Defaults to the local computer.
  • -Namespace: Defines the WMI namespace where the targeted CIM class is located. Defaults to “root\CIMV2”.
  • -ClassName: Specifies the name of the CIM class containing the method you want to invoke.
  • -MethodName: Identifies the method you want to invoke on the CIM class.
  • -Arguments: Provides the input parameters for the method. Pass parameters as an array of objects.
  • -Credential: Specifies the credentials to use for remote authentication. Defaults to the current user’s credentials.
  • -Authentication: Sets the level of authentication to use for remote connections. Defaults to “Default”.
  • -UseSsl: Establishes a secure SSL connection to the remote system.
  • -ImpersonationLevel: Specifies the level of user impersonation for remote connections. Defaults to “Impersonate”.
  • -SessionOption: Defines additional session options for the connection. Consult the New-CimSession documentation for details.
  • -ConnectionTimeout: Sets the maximum time to wait for a connection to the remote system.
  • -OperationTimeout: Specifies the maximum time to wait for the method invocation to complete.
  • -SkipCACheck: Bypasses certificate authority (CA) verification for SSL connections.
  • -DisableCertificateValidation: Skips SSL certificate validation.
  • -CimSession: Accepts an existing CimSession object for authenticated, persistent connections.
  • -ErrorAction: Controls how errors are handled during the command execution. Defaults to “Continue”.

Examples

Example 1: Invoke a method on a local computer

Invoke-CimMethod -ClassName Win32_OperatingSystem -MethodName GetVersion

Example 2: Invoke a method on a remote computer with specified credentials

$credentials = Get-Credential
Invoke-CimMethod -ComputerName remote-server -Credential $credentials -ClassName Win32_Service -MethodName StartService -Arguments ServiceName

Example 3: Invoke a method with input parameters

$params = @{
    InterfaceAlias = 'Ethernet1'
    IPAddress = '192.168.1.105'
}
Invoke-CimMethod -ClassName Win32_NetworkAdapter -MethodName EnableStatic -Arguments $params

Common Issues

  • Missing WMI provider: If WMI is not enabled or the required provider is missing on the target system, you may encounter errors.
  • Invalid method name: Ensure the specified -MethodName exists for the target -ClassName.
  • Access denied: Verify that the specified credentials have sufficient permissions to execute the method.

Integration

Invoke-CimMethod can be combined with other PowerShell commands for advanced tasks. For example, it can be integrated into scripts for:

  • Remote system monitoring
  • Automated configuration management
  • Troubleshooting hardware and software issues
  • Get-CimInstance: Retrieves a CIM instance from a remote system.
  • New-CimSession: Creates a long-lived CIM session for authenticated, persistent connections.
  • Set-CimInstance: Modifies a CIM instance on a remote system.