May 9th, 2008

Remoting with PowerShell QuickStart

PowerShell Team
PowerShell Team

PowerShell V2 introduces a new capability which allows you to remotely manage machines in your organization. I will give a basic overview of PowerShell remoting here and follow it up with some adavanced topics later. Are you ready for the fun..

A remote interaction involves 2 endpoints – Client and a Server. The same computer or system can act both as a client and as a server.

Configuration

To enable an endpoint for PowerShell remoting you need to do the following:

Step 1: Install PowerShell CTP2 of PowerShell V2

Step2: Install CTP of WinRM

Step 3: Configure WinRM for PowerShell remoting. This can be done from a PowerShell Console using the following steps

(a)    Open PowerShell console in elevated prompt

(b)   Run $pshome\configure-wsman.ps1 script.

The above script will prepare your machine for remoting. This script will enable an endpoint both to act as a client and as well as a server.

PowerShell depends on WinRM for transport of data between endpoints. WinRM implements WS-Management a SOAP-based protocol for the management of servers etc. The good thing about this protocol is it is based on HTTP. So all the packets are going on Port 80 (by default) and you don’t need to open any other port for PowerShell remoting.

Using the Power

The beauty of PowerShell remoting is that all the cmdlets/scripts you have from V1 work as is everywhere (as long as PowerShell is installed on the server). So you develop your cmdlet/scripts once and you can remotely execute them with PowerShell as is without making any changes. The only dependency being the cmdlet/script you want to execute should be accessible on the remote box.

Let me show you some examples:

PS C:\> #my current machine

PS C:\> $env:computername

KRISCV-JHOOM

PS C:\> icm kriscv-lh { $env:computername }

KRISCV-LH

PS C:\>

The above example gives a glimpse of powershell remoting. Here I ran “$env:computername” locally and then on a remote machine from my local machine. I showed a new command “icm” here. “icm” is an alias for invoke-command cmdlet. This cmdlet takes the following pattern:

 

Invoke-command <ExecutionContext>  { <script block to run in the context>}

 

In my above “kriscv-lh” is the execution context. In this case it is a destination computer name.  So, essentially I have asked invoke-command to run the script “{$env:computername}” on the remote machine. This is the cmdlet you should use for remoting in CTP2 of Powershell V2. This cmdlet internally creates a connection with the machine “kriscv-lh”, runs the command on the machine, gets the output from the remote machine to the local machine, displays the output and then closes the connection.

 

You can pretty much do anything on the remote machine as you would on the local machine. Administrator of the remote machine however has the complete control of restricting you.

 

The following example shows you a way of finding free disk space on the remote machine:

 

PS C:\> $env:computername

KRISCV-JHOOM

PS C:\> icm kriscv-lh {gwmi win32_logicaldisk | select deviceid,freespace}

 

deviceid                 freespace                ComputerName             RunspaceId

——–                          ———                ————                           ———-

A:                                                               kriscv-lh                         8ce689c2-87a2-4e38-83…

C:                       44054937600                 kriscv-lh                          8ce689c2-87a2-4e38-83…

D:                                                              kriscv-lh                          8ce689c2-87a2-4e38-83…

 

Estentially whatever you have learned with V1 of PowerShell can be used with PowerShell remoting.  Lets convert the above example to show the freespace in GB instead of bytes:

 

PS C:\> icm kriscv-lh {gwmi win32_logicaldisk | select deviceid,freespace} | select deviceid,@{Name=

“freespace(GB)”;Expression={$_.freespace/1gb}},computername

 

deviceid                                             freespace(GB)           ComputerName

——–                                                     ————-                 ————

A:                                                                               0               kriscv-lh

C:                                                   41.0060882568359              kriscv-lh

D:                                                                               0               kriscv-lh

 

Notice what I have done here. The command in bold above is run on the remote machine kriscv-lh and the rest of the pipeline is run on the local box ie.,”select-object” cmdlet is run on the local machine. PowerShell remoting ensures objects are written onto the pipeline and hence you can leverage the complete power of PowerShell by working directly with an object.

 

You can apply the same concept to multiple machines. The following examples gets the free disk space from multiple machines:

 

PS C:\> icm kriscv-lh,kriscv-jhoom {gwmi win32_logicaldisk | select deviceid,freespace} | select dev

iceid,@{Name=”freespace(GB)”;Expression={$_.freespace/1gb}},computername

 

deviceid                                             freespace(GB)       ComputerName

——–                                                      ————-                  ————

C:                                                182.064617156982       kriscv-jhoom

D:                                                136.152328491211      kriscv-jhoom

E:                                                7.60776519775391       kriscv-jhoom

F:                                                1.76084136962891       kriscv-jhoom

G:                                                                           0        kriscv-jhoom

A:                                                                           0        kriscv-lh

C:                                               41.0063934326172       kriscv-lh

D:                                                                           0        kriscv-lh

 

Notice I am running the command on 2 machines and running select-object cmdlet on the local box to filter the data.

 

There are so many things I want to talk about this CTP which I will do in the coming weeks. For the time being install the CTP, try out our new features and most importantly, if possible, give us your feedback.

 

Have a great weekend!!

 

Thanks

Krishna Vutukuri[MSFT]

Windows PowerShell Development

This posting is provided “AS IS” with no warranties.

Category
PowerShell
Topics
Remoting

Author

PowerShell Team
PowerShell Team

PowerShell is a task-based command-line shell and scripting language built on .NET. PowerShell helps system administrators and power-users rapidly automate tasks that manage operating systems (Linux, macOS, and Windows) and processes.

0 comments

Discussion are closed.