Summary: Learn how to use Windows PowerShell to create a remote management session.
Microsoft Scripting Guy, Ed Wilson, is here. Today I am happy to provide you with an excerpt from my new book, Windows PowerShell 3.0 Step by Step, published by Microsoft Press.
For a simple configuration on a single remote machine, entering a remote Windows PowerShell session is the answer. To enter a remote Windows PowerShell session, use the Enter-PSSession cmdlet to create an interactive remote Windows PowerShell session on a target machine. If you do not supply credentials, the remote session impersonates your current sign-in info.
The following output illustrates connecting to a remote computer named dc1. When the connection is established, the Windows PowerShell prompt changes to include the name of the remote system. The Set-Location (sl is an alias) cmdlet changes the working directory on the remote system to c:\. Next, the Get-WmiObject cmdlet retrieves the BIOS information on the remote system. The Exit command exits the remote session, and the Windows PowerShell prompt returns to the default.
PS C:\> Enter-PSSession -ComputerName dc1
[dc1]: PS C:\Users\Administrator\Documents> sl c:\
[dc1]: PS C:\> gwmi win32_bios
SMBIOSBIOSVersion : A01
Manufacturer : Dell Computer Corporation
Name : Default System BIOS
SerialNumber : 9HQ1S21
Version : DELL – 6
[dc1]: PS C:\> exit
PS C:\>
The good thing is that when using the Windows PowerShell transcript tool via Start-Transcript, the transcript tool captures output from the remote Windows PowerShell session and output from the local session. Indeed, all commands typed appear in the transcript. The following commands illustrate beginning a transcript, entering a remote Windows PowerShell session, typing a command, exiting the session, and stopping the transcript.
PS C:\> Start-Transcript
Transcript started, output file is C:\Users\administrator.IAMMRED\Documents\PowerShe
ll_transcript.20120701124414.txt
PS C:\> Enter-PSSession -ComputerName dc1
[dc1]: PS C:\Users\Administrator\Documents> gwmi win32_bios
SMBIOSBIOSVersion : A01
Manufacturer : Dell Computer Corporation
Name : Default System BIOS
SerialNumber : 9HQ1S21
Version : DELL – 6
[dc1]: PS C:\Users\Administrator\Documents> exit
PS C:\> Stop-Transcript
Transcript stopped, output file is C:\Users\administrator.IAMMRED\Documents\PowerShe
ll_transcript.20120701124414.txt
PS C:\>
The following image contains a copy of the transcript from the previous session.
If you anticipate making multiple connections to a remote system, use the New-PSSession cmdlet to create a remote Windows PowerShell session. New-PSSession permits you to store the remote session in a variable, and it provides you with the ability to enter and leave the remote session as often as required, without the additional overhead of creating and destroying remote sessions.
In the commands that follow, a new Windows PowerShell session is created via the New-PSSession cmdlet. The newly created session is stored in the $dc1 variable. Next, the Enter-PSSession cmdlet is used to enter the remote session by using the stored session. A command retrieves the remote host name, and the remote session is exited via the Exit command. Next, the session is re-entered, and the last process is retrieved. The session is exited once again. Finally, the Get-PSSession cmdlet retrieves Windows PowerShell sessions on the system, and all sessions are removed via the Remove-PSSession cmdlet.
PS C:\> $dc1 = New-PSSession -ComputerName dc1 -Credential iammred\administrator
PS C:\> Enter-PSSession $dc1
[dc1]: PS C:\Users\Administrator\Documents> hostname
dc1
[dc1]: PS C:\Users\Administrator\Documents> exit
PS C:\> Enter-PSSession $dc1
[dc1]: PS C:\Users\Administrator\Documents> gps | select -Last 1
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
——- —— —– —– —– —— — ———–
292 9 39536 50412 158 1.97 2332 wsmprovhost
[dc1]: PS C:\Users\Administrator\Documents> exit
PS C:\> Get-PSSession
Id Name ComputerName State ConfigurationName Availability
— —- ———— —– —————– ————
8 Session8 dc1 Opened Microsoft.PowerShell Available
PS C:\> Get-PSSession | Remove-PSSession
PS C:\>
Join me tomorrow when I will talk about understanding the While statement.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
0 comments