Invoke WSManAction - PowerShell


Overview

Invoke-WSManAction allows remote execution of Windows Management Instrumentation (WMI) method invocations, command execution, script invocation, and file transfer operations on remote computers using the Web Services for Management (WS-Management) protocol.

Syntax

Invoke-WSManAction -Action <string> -ComputerName <string> [-Credential <PSCredential>]
[-OperationTimeout <int>] [-Port <int>] [-SkipCACheck] [-SkipCNCheck] [-SkipRevocationCheck]
[-ThrottleLimit <int>] [-SessionOption <string>]

Options/Flags

  • -Action: Specifies the action to perform. Valid values are Invoke, Execute, Get, Put, and Delete.
  • -ComputerName: Specifies the computer name or IP address of the remote host.
  • -Credential: Specifies the credentials to use for authentication to the remote host.
  • -OperationTimeout: Sets the time (in milliseconds) to wait for the operation to complete.
  • -Port: Specifies the port number to use for WS-Management communication.
  • -SkipCACheck: Instructs the command to skip certificate authority (CA) chain validation.
  • -SkipCNCheck: Instructs the command to skip checking the Common Name (CN) in the subject name of the server’s certificate.
  • -SkipRevocationCheck: Instructs the command to skip revocation checking for the server’s certificate.
  • -ThrottleLimit: Limits the number of concurrent WS-Management connections to the remote host.
  • -SessionOption: Specifies additional session options to be used when connecting to the remote host.

Examples

Invoke a WMI Method

Invoke-WSManAction -Action Invoke -ComputerName remotehost -Credential $Credential -ResourceURI winrm/wmi/root/cimv2/win32_OperatingSystem -Operation get-wmiobject -ArgumentList "Win32_OperatingSystem"

Execute a Command

Invoke-WSManAction -Action Execute -ComputerName remotehost -Command "ipconfig /all"

Transfer a File

Invoke-WSManAction -Action Put -ComputerName remotehost -ResourceURI "C:\remote" -SourcePath "C:\local"

Common Issues

  • Credential Errors: Ensure that the specified credentials have sufficient permissions on the remote host.
  • Certificate Issues: Check for expired, revoked, or untrusted certificates. Consider using the -SkipXXXCheck parameters to bypass certificate validation.
  • WS-Management Service Not Running: Verify that the WS-Management service is running on the remote host.

Integration

  • Use Invoke-WSManAction with Get-Credential for secure authentication to remote hosts.
  • Combine with Invoke-Command to execute commands on multiple remote hosts in a loop.
  • Use Format-WSManActionData to format the output of Invoke-WSManAction for display or further processing.
  • Get-WSManInstance
  • Set-WSManInstance
  • New-WSManSession