Get WSManInstance - PowerShell


Overview

The Get-WSManInstance command retrieves an instance of a WS-Management resource. It allows you to interact with remote computers and manage various aspects of the operating system and applications. This command is particularly useful for remote administration, automation, and management of distributed systems.

Syntax

Get-WSManInstance [-ComputerName] <string[]> [-ResourceUri] <string> [-Authentication] <WSManAuthentication> [-Credential] <PSCredential> [-Filter] <string> [-Option] <WSManOption> [-Property] <string[]> [-ErrorAction] <ActionPreference> [-WarningAction] <ActionPreference> [-InformationAction] <ActionPreference> [-DebugAction] <ActionPreference> [-Verbose] [-ErrorVariable] <string> [-WarningVariable] <string> [-InformationVariable] <string> [-DebugVariable] <string> [-OutVariable] <string> [-OutBuffer] <uint16> [-ThrottleLimit] <uint32>

Options/Flags

  • -ComputerName: Specifies the remote computer name or IP address.
  • -ResourceUri: Sets the URI of the resource to be retrieved.
  • -Authentication: Configures the authentication mechanism. Default is Negotiate.
  • -Credential: Provides a PSCredential object for authentication.
  • -Filter: Filters the results based on a WQL query.
  • -Option: Specifies additional WS-Management options.
  • -Property: Selects specific properties to be retrieved.
  • -ErrorAction, -WarningAction, -InformationAction, -DebugAction: Controls the behavior when errors, warnings, informational messages, or debug messages occur.
  • -Verbose: Enables verbose output.
  • -ErrorVariable, -WarningVariable, -InformationVariable, -DebugVariable: Stores error, warning, informational, or debug messages in specified variables.
  • -OutVariable: Stores the output in a specified variable.
  • -OutBuffer: Sets the number of objects to buffer in memory.
  • -ThrottleLimit: Limits the number of concurrent connections to the remote computer.

Examples

Example 1: Get a specific property of a WS-Management resource

Get-WSManInstance -ComputerName remote-computer -ResourceUri "http://schemas.microsoft.com/wbem/wscim/1/wmi/root/cimv2/Win32_Service" -Property "Name"

Example 2: Filter results using a WQL query

Get-WSManInstance -ComputerName remote-computer -ResourceUri "http://schemas.microsoft.com/wbem/wscim/1/wmi/root/cimv2/Win32_Service" -Filter "State='Running'"

Example 3: Use alternate authentication method

$creds = Get-Credential
Get-WSManInstance -ComputerName remote-computer -Authentication Basic -Credential $creds -ResourceUri "..."

Common Issues

  • Access Denied: Ensure you have sufficient permissions on the remote computer and the WS-Management service is running.
  • Invalid URI: Verify the ResourceUri parameter is properly formatted and points to a valid WS-Management resource.
  • Connection Refused: Check firewall settings and network connectivity to the remote computer.

Integration

Get-WSManInstance can be used in conjunction with other PowerShell commands to automate complex management tasks.

Example: Restart a service on a remote computer

$service = Get-WSManInstance -ComputerName remote-computer -ResourceUri "http://schemas.microsoft.com/wbem/wscim/1/wmi/root/cimv2/Win32_Service" -Filter "Name='ServiceName'"
$service.StopService()
$service.StartService()