Prof. Powershell
Remote Possibilities
With no second computer for lab testing these remote connection management tips, what's a geek to do? Of course -- establish a 'remote' connection.
- By Jeffery Hicks
- 07/07/2009
In the last few columns I’ve been discussing remote computer management with PowerShell 2.0. This time, let's see this in action. Remember you will need PowerShell 2.0 installed on your machine and any remote machine you want to manage. You'll also need to configure WinRM with the Enable-PSRemoting cmdlet. If you don't have a second machine handy at the moment, not to worry. You can establish a "remote" connection to your computer to try this out.
First, use the New-PSSession cmdlet, specifying a computername:
PS C:\> $session=new-pssession CHAOS
PS C:\> $session
Id Name ComputerName State ConfigurationName Availability
-- ---- ------------ ----- ----------------- ------------
1 Session1 chaos Opened Microsoft.PowerShell Available
You can also create sessions to multiple computers at the same time.
Now we can execute any PowerShell expression on the remote computer using Invoke-Command:
PS C:\> invoke-command -Session $session -ScriptBlock {get-service | where {$_.status -eq "running"}}
Status Name DisplayName PSComputerName
------ ---- ----------- --------------
Running AeLookupSvc Application Experience chaos
Running AppMgmt Application Management chaos
Running AudioEndpointBu... Windows Audio Endpoint Builder chaos
Running AudioSrv Windows Audio chaos
...
All you need to do is specify the session object and a script block. The results are returned to your session immediately.
You can also create an interactive remote session with Enter-PSSession:
PS C:\> Enter-PSSession CHAOS
[chaos]: PS C:\Users\Jeff\Documents> $env:computername
CHAOS
I now have a remote session to CHAOS. Notice the prompt change? I can run any PowerShell command I enter and it will execute on the remote computer:
[chaos]: PS C:\Users\Jeff\Documents> gwmi win32_logicaldisk -filter "deviceid='c:'"
DeviceID : C:
DriveType : 3
ProviderName :
FreeSpace : 6014697472
Size : 80024170496
VolumeName :
You can also enter an interactive session by specifying a stored PSSession like the one I created earlier:
PS C:\> Enter-PSSession -Session $session
[chaos]: PS C:\Users\Jeff\Documents>
You could easily create PSSessions for several remote computers and jump between them using Enter-PSSession. Use Exit-PSSession to return to your local system.
Your PSSessions will automatically be removed when you close PowerShell, or you can use Remove-PSSession to delete one or more sessions.
As always, because I can’t go into great detail so be sure to look at complete help and examples for these cmdlets. Also be sure to read About_PSSessions.
Note: This information is based on pre-release software and is subject to change.
About the Author
Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.