Windows PowerShell


Windows PowerShell Remote

Windows PowerShell 2.0 Remote

Normally Microsoft products are so easy to install that you don't need any instructions.  However, setting up PowerShell v 2.0's 'Remoting' is an exception; in fact, creating a remote session is so difficult for beginners that I have devoted a whole tutorial to achieve this single goal.

Remoting in Microsoft PowerShell v 2.0

Introduction to PowerShell Remote

The ability to create sessions to remote machines is PowerShell v 2.0's jewel in the crown.  Indeed, remoting is the number one reason to upgrade from PowerShell v 1.0.  In many ways remote PowerShell reminds me of Telnet. Sitting at our own keyboard, yet running the wonderful PowerShell commands against another machine transforms what we can achieve in terms of configuring settings and collecting data about your network empire. 

Now for the bad news, getting starting with remote PowerShell is hard.  Most of the articles about PowerShell's remote capabilities are written by experts; people who are not only comfortable with command line syntax, but also, in a previous life, have probably written a shell program from scratch.

Our PowerShell 'Remoting' Mission

The crucial concept for this mission to get remote PowerShell working is that we need an additional shell program called WinRm.  If you have used a command line program such as NetSh then the techniques for configuring WinRm will be vaguely familiar.

I got my copy of WinRm from Microsoft Connect, I recommend registering with this site because you never know when you are going to need some its vast library of software.  Also be sure to install the same version of WinRm and PowerShell v 2.0 on both test machines.  Incidentally, that in itself can be a major challenge if you have to uninstall a previous version, but in the mean time you have applied a service pack. 

My Mission is to get you started with remote PowerShell.  In order to focus on this task, I suggest that you, set aside any thoughts of safe procedures; on each test machine logon locally as THE administrator, then disable the firewalls on each test computer.  Furthermore, before you launch either PowerShell or the cmd DOS box, right click and select 'Run as administrator', that way if a command does not work we can't blame lack of security rights.  Once you gain success with remoting commands, then naturally revert to normal security and start turning on firewalls, and logging on with normal accounts.

It's only fair to point out that security experts are horrified by my suggestion to ride roughshod over security, they preach that you should seek to setup remoting in a secure environment from the outset.  My counter is that this multi-factor approach is just too hard for PowerShell beginners and security amateurs.

Guy Recommends: SolarWinds LANSurveyorSolarwinds LANSurveyor

LANSurveyor will produce a neat diagram of your network topology.  But that's just the start; LANSurveyor can create an inventory of the hardware and software of your machines and network devices.  Other neat features include dynamic update for when you add new devices to your network.  I also love the ability to export the diagrams to Microsoft Visio.

Finally, Guy bets that if you take a free trial of LANSurveyor then you will find a device on your network that you had forgotten about, or someone else installed without you realizing!

Download a Free Trial of LANSurveyor

Two Types of Remote PowerShell

It's time to point out that there are two varieties of Microsoft PowerShell remoting.  Firstly, the more sophisticated variation, which employs cmdlets that create a persistent pipe to the second machine.  These commands contain the aptly named noun PSSession, check what's available with this cmdlet:

get-Command -noun PSSession.

Secondly, there is also a more basic form of remote PowerShell which merely extends local commands by appending the -computerName parameter; the result is that your instructions run against another named machine on the network.  For example:

get-Process -computerName machine2

For a list of cmdlets that support this simple remote behaviour try:

get-command | where { $_.parameters.keys -contains "ComputerName"}

For the full list you need to add: -and $_.parameters.keys -notContains "Session"

Get Started with Basic PowerShell Remote

I strongly recommend that you gain confidence by starting with the basic form of remoting, my reasoning is that it does not require you to configure the tricky WinRm shell.  My thinking is that when you are learning anything new, not just PowerShell, there is nothing like success to encourage further exploration of the boundaries of what is possible.  Whereas, there is nothing like a string of failures to plant the seed of doubt that this is indeed a worthwhile project or product.  Thus once you have mastered a PowerShell technique on the local computer it's only natural try to run the same command on a remote machine. 

get-services # (Local Machine)
# Then
get-Services -computerName machine2
# (You should get a list of services from your second test computer.)

WinRm - The Pre-requisite for Remote PowerShell

Key Command
Even with all the components installed correctly, (PowerShell v 2.0 CTP3, .Net Framework and WinRm), remoting still won't work until you run this cmdlet from within PowerShell (not DOS):

Enable-PSRemoting

Note that unlike PSSession commands, this function only has one 'S' PSRemoting.  Actually, the underling cmdlet is Enable-PSSessionConfiguration.

The two commonest traps with WinRm are, trying to configure this command from inside PowerShell, when you should be at the cmd (DOS box) command line.  In particular the 'set' verb works with cmd but not with PowerShell.  The second trap is remembering to right click and 'Run as administrator'.  The error messages do give tiny clues to the underlying problem, but they don't give many ideas for a solution.  This is why I recommend investigating the WinRm syntax, I would start with
winrm help -?

Having noted the verbs, 'get', 'set' and 'enumerate', I would try
winrm help config

Two more useful commands
winrm enumerate winrm/config/listener
winrm get winrm/config/client

Any problems, remember, you should be in the DOS box and not PowerShell.   Also, you did right click and 'Run as administrator', didn't you?  Naturally you have to run the same WinRm setup techniques on both the host machine and the test 'victim' machine.

Configuring TrustedHosts drove me mad, I suggest you try this command, especially noting the famous * wildcard:
winrm set winrm/config/client/ @{TrustedHosts="*"}  

Guy Recommends: SolarWinds Engineer's Toolset v10Engineer's Toolset v10

The Engineer's Toolset v10 provides a comprehensive console of utilities for troubleshooting computer problems.  Guy says it helps me monitor what's occurring on the network, and the tools teaches me more about how the system literally operates.

There are so many good gadgets, it's like having free rein of a sweetshop. Thankfully the utilities are displayed logically: monitoring, discovery, diagnostic, and Cisco tools.  Download your copy of the Engineer's Toolset v 10

Basic PowerShell Remoting Commands

Once you have slaved away to setup PowerShell v 2.0's remoting, then naturally you want to test the connections to the second machine, for example:

New-PSSession -computername testMachine2
# Or simply:
Enter-PSSession -computername testMachine2

Tip: If you want to test remoting but don't have a second machine, then create a 'Remote Session' to your own own machine.  Explicitly, if your machine is called 'KingMachine', then try:
New-PSSession -computername KingMachine.

 Other useful commands include:

Exit-PSSession
Get-PSSession
# or
Get-PSSession | remove-PSSession # (Deletes the session)

For the full list try: Get-command -noun pssession.  Also get-command -verb invoke

Once you reach the 'Victim' / remote / second machine

With the remote pipeline in place, you are ready to run PowerShell commands as though you were at the keyboard of the second machine, indeed, this is the whole point of creating remote shells.  For example,

set-location c:\ | get-childitem

As my mission in this tutorial is solely to get you started, now it's up to you to devise PowerShell commands to interrogate and even configure your remote computer.

Tip: If any of your remote commands don't seem to work, test them on your local computer first.

  ˚

Summary of Windows PowerShell v 2.0 Remoting

Paradoxically, once you realize a project is going to be complicated, the task becomes easy if you break it down into a series of bite-sized chunks.  Therefore, install the same version of PowerShell on both machines, abandon security and get WinRm working, now you are ready to test PowerShell 2.0's wonderful remoting capabilities.

See more Microsoft PowerShell tutorials

PowerShell Home  • Introduction  • Dreams  • 3 Key Commands  • Cmdlet scripts  • Real life tasks

Remote PowerShell  • -Online  • WinRm and WSMan  • Invoke-Command  • Jobs  -asJob   • PowerShell

Please write in if you see errors of any kind.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Google

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

Home Copyright © 1999-2009 Computer Performance LTD All rights reserved

Please report a broken link, or an error.