Summary: Learn how to configure Windows PowerShell remoting, store credentials, and use remote commands.
Hey, Scripting Guy! I keep hearing about Windows PowerShell remoting. But I do not really know what Windows PowerShell remoting is. For example, I can use the Get-WMIObject to work with remote computers. Is that remoting? If so, what is the big deal? I could do that in Windows PowerShell 1.0. Is this just a bunch of typical marketing hype, or is there something that actually works here and I am missing it?
—GB
Hello GB,
Microsoft Scripting Guy Ed Wilson here. GB, I hate to tell you, but you are missing the boat. There is a common misconception about what Windows PowerShell remoting really is. There are several ways to run a command on a remote computer:
The first thing to do is to enable and configure Windows PowerShell remoting. To do this, use the Enable-PSRemoting cmdlet. Enable Windows PowerShell remoting on all machines that will communicate. If this only involves a couple computers, using the Enable-PSRemoting cmdlet works fine. But if you need to turn on Windows PowerShell remoting on an entire organizational unit, domain, or forest, it is better to use Group Policy. Unfortunately, there are myriad Group Policy settings from which to choose, and I like the convenience of the Enable-PSRemoting cmdlet. Therefore, I use a script that calls the Enable-PSRemoting cmdlet with Group Policy and assign it as a logon script. I discuss this technique in the Enable PowerShell Remoting to Enable Running Commands blog post.
After it is configured, I like to use the Invoke-Command cmdlet to ensure that Windows PowerShell remoting works properly. By running a simple command such as hostname.exe, I ensure that Windows PowerShell remoting works, and confirm the actual location that ran the command. Here is the syntax:
invoke-command -cn syddc01 -credential contoso\administrator -scriptblock {hostname}
After I have run that command, I know everything works. Now, I like to store my credentials in a variable to make it easier to run remote commands. To do this, I use the Get-Credential cmdlet. Here is the command I use:
$cred = Get-credential contoso\administrator
When the command runs, the following dialog box is displayed.
After I have a credential object stored in the $cred variable, I use it to invoke a remote command. These commands are shown here:
$cred = Get-Credential contoso\administrator
invoke-command -cn syddc01 -cred $cred -script {hostname}
Now, I want to use the Invoke-Command cmdlet to run the Get-Process cmdlet on a remote computer and use alternate credentials. The syntax for this command is shown here (this command uses the cn computername alias for Invoke-Command, the credentials stored in the $cred variable, and the gps alias for the Get-Process cmdlet):
Invoke-Command -cn syddc01 -Credential $cred -ScriptBlock {gps}
The command and associated output are shown in the following figure.
GB, that is all there is to getting Windows PowerShell remoting up and running, and storing credentials in a credential object to simplify running remote commands. Join me tomorrow when I will talk about creating remote Windows PowerShell sessions.
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
Hi Ed,
remoting support in powershell is one of the major advantages it offers for administration of remote machines. It nearly makes me forget about psexec.exe ( part of the Sysinternals suite )
There is one tradeoff: We can't work interactivly on remote machines via PS remoting!
This feature has been considered a security risk and so it is not available in powershell, which sometimes is the only reason to fall back on psexec ...
Klaus.
PowerShell remoting supports an interactive remoting (so called 1:1 remoting) with the Enter-PSSession cmdlet. The Enter-PSSession cmdlet starts an interactive session with a single remote computer. During the session, the commands that you type run on the remote computer, just as though you were typing directly on the remote computer.
Dear Aleksandar,
you are right ... and I am right :-)
It depends on the meaning of the word "interactive", I'm afraid!
Well, if you enter a pssession, you are in fact "on/in the remote system" and you can work interactive ... true!
But you won't see a GUI on the remote machine!
That's what I meant because I have to start interactive (=~ GUI) programs remotely and it makes no sense to do so, without the graphical interface ... even if some of the programs would begin automatically to do their work, I couldn't see any progress and can't pause/resume - interact them without their controls :-(
I cant seem to find anything about enabling remote access on remote machines. If I want to enable\configure powershell remote access on remote machines, how would I do that (if I couldnt use group policy, but the machines are all on the same domain, and users are local administrators on their machines)?
Can you point me to a blog in which you have perhaps talked about using Powershell to retrieve some information from a Unix server?