Remoting is one of the wonderful features offered by powershell V2.This article will explain how to setup remoting and run commands on remote system using remoting feature.
Introduction:
First of all, what is remoting? Executing a command against a remote system is what called remoting? No, this is not a full fledged remoting. PowerShell remoting enables you to run a command *in* remote computer and bring the output to your console. For example, if you type some command like “hostname” or “ipconfig” in remoting, it will get executed in target computer and brings the output to your computer console. You might want to ask, why this remoting is needed when cmdlets offering -computername parameter(for example Get-WMIObject). The answer is simple, not every cmdlet support remoting and authentication happens per cmdlet basis. To overcome these limitations powershell remoting was introduced where you will get authenticated with target computer once and from there on you can execute the command/script/powershell cmdlet/batch file locally to the remote computer.
Configuration:
You need at least Windows powershell V2 to make remoting work. Use Get-Host cmdlet to know your current powershell version.
Enable remoting:
Remoting feature is not enabled by default. But it’s not that difficult to enable it. Launch a powershell window with elevated rights and run below command.
[PS]C:>Enable-Remoting
This enabling has to be done on all computers in your domain to trigger commands/script executing remotely. If you see any errors while enabling remoting use -force option.
This completes the configuration of remoting in computers.
Now it’s time to perform actions in target system by sitting at your desktop. First let’s go through the list of cmdlets available as part of remoting.
Above cmdlets are self explanatory. Executing a command against a target computer is a 3 step process.
- Establish a session
- Execute any command/script/cmdlet using the session
- Delete the session
1. Establishing a session:
A new session needs to be established with target computer to execute commands against it. The below examples helps you in creating a session with computer name called myremotepc.
[PS]C:>New-PsSession -ComputerName myremotepc
This command will succeed if your current login has administrator rights on target computer. If not, then you have an option to provide credentials at the time of establishing new connection like shown below
[PS]C:>New-PsSession -ComputerName myremotepc -credential $prompt
This completes the session establishment:
2. Executing commands:
Now it’s the time for us to execute some commands. Let’s start with a simple example. Have you ever used Get-Date cmdlet to get the time of remote computer? I bet you never. This cmdlet don’t have option to do this. But with remoting we can make it possible.
[PS]C:>$mysession = New-PSSession -ComputerName myremotepc
[PS]C:>Invoke-Command { Get-Date } -Session $mysession
In the above example, first we established a new session with remote computer and saved the session details to a new variable called $mysession. Using invoke-Command cmdlet we executed Get-Date cmdlet against remote computer using the session that we created before. This way you can execute any command/script/cmdlet using Invoke-command cmdlet by passing the command inside the curly brackets.
3. Deleting the session:
It is always recommended to delete the session once you are done with using it. This is a good security practice. This way we can ensure that no other who uses your computer later can execute commands on your name. Remove-PsSession is the cmdlet which helps us here.
[PS]C:>Remove-PsSession -session $mysession
An alternate method for executing commands is directly entering the remote computer powershell window. Enter-Pssession cmdlet does it for you.
Conclusion:
Windows PowerShell remoting really make the life of system administrators so easy in Windows 7/windows 2008 environment. The level of automations that sysadmins can do will be increased to greater extents with powershell.
Comments on this entry are closed.
Fantastic ! I hope you will not stop posting new articles. I was pleased to read this article
i have problems installing windows7 on my PC, maybe i need a bios update,**
:-” I am very thankful to this topic because it really gives up to date information **~
Hey I just wanted to say that I really liked reading your blog. You have good views, Keep up the good informative info