As a software engineer, more often than not I find myself writing code in C#, and things tend to be pretty neat inside Visual Studio. However, when we need to deploy our new application to the servers where the application will be hosted, the number of Remote Desktop sessions we require can be overwhelming.
Here is where PowerShell Remoting comes to the rescue. Basically it allows a client machine to connect to another machine and then you can run PowerShell commands and scripts typing from the client. Think of Telnet on Unix machines.
Remoting is a feature introduced on PowerShell v2.0 and basically will let you run any PowerShell command or script (like deployment and configuration scripts) for all your farm servers from a single computer. And the sweet thing is that you will not need to start Remote Desktop connections to each server separately.
So to start working with PowerShell Remoting you need to follow these steps:
1. Log on the Host computer, the one you want to connect to, and start an elevated (run as Administrator) PowerShell session.
2. Configure the WinRM Service (Windows Remote Management) using the cmdlet:
Enable-PSRemoting
3. Configure the trusted hosts, this is, specify which other machines are allowed to connect to this one. For example if you want to allow remote connections from two machines named SERVER-2 and MYDESKTOP you will need to run the following:
set-item wsman:localhost\client\trustedhosts -value “SERVER-2,MYDESKTOP”
4. Once you have done this, you are ready to go. You can log into your computer and start a remote session to your recently configured server, suppose we are trying to start a session into SERVER-1 machine:
Enter-PSSession SERVER-1
5. Now you are ready to type PowerShell commands and they will run on SERVER-1 machine. Once you are finished, you exit the session:
Exit-PSSession
6. So far so good, but what when you have a full script that you want to run on the other machine? For instance, if you want to run myScript.ps1 you will need to use New-PSSession and Invoke-Command
$remSes = New-PSSession -ComputerName SERVER-1
$job = Invoke-Command -Session $remSes -FilePath myScript.ps1 –AsJob
This will run myScript.ps1 on SERVER-1 machine as a job from your very desktop. At first, this may seem like a lot of things to do just to be able to run scripts remotely on a single server.
But when you have several servers to manage and you have scripts that do exactly the same on all servers then PSRemoting will be handy, more if you use New-PSSession and Invoke-Command as part of a larger script that handles all your servers. Automating this type of tasks, in the long term will make your life easier and they will save you much more than a few hours, which you can use to create more PowerShell scripts to automate something else.
No comments:
Post a Comment