다음을 통해 공유


Quick Guide to PowerShell v2 Remoting with PSSession

There are excellent, detailed references to all the powerful remoting features in PowerShell v2, including:

Both of these are far more detailed and involved than is necessary to get up and running with PowerShell PSSession commands. And that's what I really like to use, since I'm predominantly an interactive PowerShell user. So, here's the basics to use PowerShell from your desktop to connect to a server (or other workstation) and run commands on that server or workstation. (note: this assumes a domain environment)

First, make sure PowerShell Remoting is enabled on the target server or workstation. For this one command, you'll need to open a Remote Desktop session on the target server or workstation, and open a PowerShell window As Administrator. 

    PSH> Enable-PSRemoting -force

That's it. The Enable-PSRemoting command needs to be run once only on a computer to enable it to receive commands.

Now, the basic steps for interactive PSSessions are:

  • Create a session (New-PSSession)
  • Use the session (Enter-PSSession)
  • Leave the session (Exit-PSSession or simply Exit)
  • Close the session (Remove-PSSession)

To create a new session on machine "srv1":

    PSH> $srv1 = New-PSSession srv1

Then, to actually use the session:

    PSH> Enter-PSSession $srv1
    [SRV1]: PS C:\Users\Charlie\Documents>
**
**And that's it - you now have an open PowerShell command window on the remote computer, srv1. Any commands you enter there will be processed on the remote computer. When you want to return to the local computer, simply type exit.
**
**To run a script or command against a remote computer, you can also use Invoke-Command. So, using our session from above, I can even run a CMD command:

PSH> Invoke-Command -session $srv1 -script {cmd /c dir c:\ }
 Volume in drive C has no label.
 Volume Serial Number is 8053-7E1F

 Directory of c:\

17/12/2010  09:03 AM    <DIR>          ExchangeSetupLogs
03/12/2010  07:29 AM               559 HPDIU.log
08/11/2010  02:31 PM    <DIR>          inetpub
13/07/2009  07:20 PM    <DIR>          PerfLogs
08/12/2010  07:21 AM    <DIR>          Program Files
26/12/2010  02:56 PM    <DIR>          Program Files (x86)
02/12/2010  02:25 PM             1,233 redircopy.txt
02/12/2010  02:21 PM    <DIR>          StorageReports
13/02/2011  05:49 PM    <DIR>          temp
12/02/2011  07:25 AM    <DIR>          Users
03/12/2010  05:05 PM    <DIR>          VHDs
13/02/2011  03:25 PM    <DIR>          Windows
02/12/2010  02:39 PM    <DIR>          WSUS
               2 File(s)          1,792 bytes
              11 Dir(s)  112,803,667,968 bytes free

PSH>
There are lots of other ways you can use PowerShell v2 Remoting, but this should get you started.


See Also