Windows PowerShell Remoting

Host applications can send and receive data to and from remote computers in several ways. They can use a PowerShell object to run cmdlets that use WS-Management–based Windows PowerShell remoting to create a temporary or persistent connection, or they can run cmdlets that use Windows Management Instrumentation (WMI) to connect to the remote computer. To use WS-Management–based remoting, you must configure the local and remote computers for remoting. If you use WMI, you do not have to configure the remote computer for remoting.

Cmdlets That Use WMI

Several cmdlets have a ComputerName parameter that enables you to create a temporary connection to a remote computer by using WMI. Because these cmdlets do not use WS-Management–based remoting, you can use the ComputerName parameter of these cmdlets to specify any computer that is running Windows PowerShell. The computer does not have to be configured for WS-Management–based remoting, and the computer does not have to meet the system requirements for WS-Management–based remoting.

Host applications can run the following cmdlets without creating a custom host or a remote runspace. These cmdlets can be added to the pipeline of a PowerShell object in the same way that other cmdlets are added to the pipeline. All of these cmdlets have a ComputerName parameter.

  • Clear-EventLog

  • Limit-EventLog

  • Get-Counter

  • New-EventLog

  • Get-EventLog

  • Remove-EventLog

  • Get-HotFix

  • Restart-Computer

  • Get-Process

  • Show-EventLog

  • Get-Service

  • Show-Service

  • Get-WinEvent

  • Stop-Computer

  • Get-WmiObject

  • Write-EventLog

Windows PowerShell Remote Commands

Host applications can use a PowerShell object to run commands that use WS-Management–based Windows PowerShell remoting to create a temporary or persistent connection to the remote computer. This includes establishing an interactive session with a single remote computer.

noteNote:
To use WS-Management–based Windows PowerShell remoting, the local and remote computers must be configured for remoting, and the host application must run in elevated mode. For more information about the configuration details, see about_Remote_Requirements in the Microsoft TechNet library.

Establishing a Remote Connection to a Single Computer

Host applications use a WSManConnectionInfo object to specify the information needed to establish the remote connection, such as which computer to connect to. Then a runspace is created that uses the WSManConnectionInfo object to establish the connection. After a remote connection is established, a PowerShell object can be used to run commands or scripts on the computer. It is important to remember that a remote connection can be established to only one computer, and that computer can be a remote computer or the local computer.

For examples of the code that is needed to establish the remote connection, see RemoteRunspace01 Sample and RemoteRunspacePool01 Sample.

Invoking Invoke-Command on a Remote Computer

Although the remote connection connects to a single computer, the host application can run the Invoke-Command cmdlet to run commands on other computers. This means that remote connection specifies the computer where the Invoke-Command cmdlet is run, and the cmdlet specifies the computers where other commands are invoked.

You can specify these other computers by setting the ComputerName parameter of the cmdlet, and you can specify the commands to be run by setting the ScriptBlock parameter. For example, the host application could create a remote connection to Server01, and then it could run the following command on server01 to run the Get-Culture cmdlet on the Server02 and Server03 computers.

invoke-command -computername Server02, Server03 -scriptblock {get-culture}

The ComputerName parameter is designed for running a single command or several unrelated commands on one or more computers. When you use the ComputerName parameter, Windows PowerShell establishes a temporary connection just for that command. It then closes the connection when the command is complete. Any variables or functions that are defined in the command are lost.

Invoking Enter-PSSession to Establish Persistent Connections

Host applications can run the EnterPsSession-Command cmdlet on a local or remote computer to establish persistent connections to other computers. The following list describes the elements that the host application must implement and how those elements are used.

  1. The host application creates a local runspace that is used by the PowerShell object to run the commands.

  2. The host application creates a custom host class that implements the IHostSupportsInteractiveSession interface. For an example of of implementing this interface, see How to Add Support for Interactive Remoting.

  3. When the Enter-PsSession cmdlet is invoked in the local runspace, the Windows PowerShell runtime calls the PushRunspace method of the IHostSupportsInteractiveSession interface to establish the remote connection.

  4. The host application creates a remote runspace directly or runs the New-PsSession cmdlet. This is the runspace that is used by the Enter-PsSession cmdlet.

  5. From here onwards, the host application uses this remote runspace to execute commands (by using a PowerShell object).

  6. When the Exit-PsSession cmdlet is run, the Windows PowerShell runtime calls the PopRunspace method of the IHostSupportsInteractiveSession interface to break the remote connection.

See Also



Show:
© 2014 Microsoft