This is small guide which allows you to create Remote Powershell Sessions (like SSH). It allows you to create connection to Host which have Powershell.
•Allow Powershell Remoting on the Remote Host
•Add Trusted Hosts on the Local Computer
•Create a new Remote Session
•Leave a Powershell Remote Session
•Close a Powershell Remote Session
•Send a command to a Remote Host
Allow Powershell Remoting on the Remote Host
Run Powershell on the Remote Host and run the following Cmdlet.
Enable-PSRemoting
This command starts the WinRM service if it’s not allready started and sets the startup type to automatic. Adds firewall exceptions for WS-Management communications and creates a listener to accept requests.
Add Trusted Hosts on the Local Computer
On the Local Computer run Powershell and run the following Cmdlet. This allows you to connect to any host. It also starts WinRM if its not already started.
Set-Item WSMan:\localhost\Client\TrustedHosts *
After that you may have to restart the WinRM service
Restart-Service winrm -Force
Create a new Powershell Remote Session
There are two ways to create a new PS Remote Session.
New-PSSession -ComputerName Computer01
With Get-PSSession you can list all active sessions. Now you can enter a active Session with Enter-PSSession and the ID
Enter-PSSession 2
A quicker way to do that, you can simply use Enter-PSSession to create a new Session and directly connect to this Session.
Enter-PSSession -ComputerName Computer01
Leave a Powershell Remote Session
To leave a Powershell Remote Session you can simply use the Exit-PSSession
Exit-PSSession
Close a Powershell Remote Session
To close a Powershell Remote Session you can list all active Sessions with Get-PSSessions command and close them with Remove-PSSession command.
Get-PSSession | Remove-PSSession
'|' is a pipe command which lets you continue your command with another command to run afterwards.
A bit like, run this code first then once done, run this next bit of code.
Send a command to a Remote Host
To run a command on a Remote Host you can use the '-ComputerName' parameter.
Get-Service -ComputerName Server01
Get-Service -ComputerName Server01 | Where-Object {$_.Name -eq "BITS"}
With this little snippet you can run commands on multiple Hosts
No comments:
Post a Comment