Powershell Remoting Concepts, powershell remoting allows administrators to execute the commands on machines, you can configure and manage machines easily. Collecting data and changing the settings on one or more remote systems pretty much simple and easy, there are some cmdlets already have the remoting feature or parameter to execute a command on the remote machines.
Here are some of the commands (cmdlets) support remoting.
- Get-Service
- Send-MailMessage
- Set-Service
- Get-WinEvent
- Get-WmiObject
- Restart-Computer
- Test-Connection
- Clear-EventLog
- Get-EventLog
- Get-HotFix
- Get-Process
The commands in PowerShell which have a -ComputerName parameter allows you to specify the target machine and get what you want.
Powershell Invoke-command
This command is most useful and powerful, it has core Powershell remoting capabilities and we can run commands on remote computers. This allows us to run persistent commands as using New-PSSession, when you want to run the background jobs use the AsJob parameter. This Invoke-command can be used synchronously or asynchronously as a job.
If you’re going to execute the command on many computers, this command itself takes care of all resource management details, such as limiting the Invoke-Command number of concurrent remote connections.
Syntax:
Invoke-Command [-ComputerName] [-Session] [-ScriptBlock]<String[] <ScriptBlock> [<CommonParameters>]
Enabling Powershell Remoting.
Powershell remoting is enabled by default on windows server 2012 and later version, however, prior to the 2012 version we have to enable. Let’s see how to enable Powershell remoting on machines, remember there some additional steps that need to be performed on the workstation for enabling remoting features.
Login to a remote computer and open the Powershell window in administrator mode and run the below command.
Enable-PSRemoting -force
This command sets all the required things for remoting to enable like starts the WinRM service and sets it to automatic. If your machine is part of the domain and a private network group then you are good to start executing the commands on this machine from any other machine.
Examples:
Invoke-command -computername machine01 -scriptblock {get-service} #Above command brings all the services available on the remote computer name machine01. Invoke-command -computername machine03 -scriptblock {get-eventlog system}
The above command brings all the system event logs available on the remote computer name machine03.
Invoke-Command -ComputerName machine01, machine02 -ScriptBlock {Get-Process PowerShell} -HideComputerName
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
——- —— —– —– —– —— — ———–
456 11 35100 70988 100 3.68 1372 PowerShell
677 12 35106 50988 200 7.68 454 PowerShell
Persistent Remote connection
This Enter-PSSession command allows one to connect remote computer and can establish an interactive session, once you are connected remote computer you can run commands, retrieve data, and perform actions. This command opens an interactive session within your local Powershell window and you can also use alternative credentials from a nondomain system.
Enter-PSSession -computername machine01. Enter-PsSession –ComputerName machine02.techieseek.com –Credentials domain\serveradmin.
When you want to exit from an interactive session then you need to pass exit-pssession command.
Notes: Before we end up reading this article let me repeat a few important things.
The command invoke-command is used for running commands against remote machines, one at a time.
The command Enter-PSSession, is used for making a persistent connection between your local machine to the remote machine.
The command Exit-PSSession is used for exiting the above persistent remote connection, coming back to your local machine Powershell session.
Thank you for reading this article, if you have any questions please let us know.
Thank you for visiting my site, for any scripts in these articles you are testing please make sure you have tested this script in our lower environment before you run in production.
Leave a Reply Cancel reply