PowerShell Beginner’s Guide – PowerShell Remoting

PowerShell Beginner’s Guide – PowerShell Remoting

https://ift.tt/A20myXh

In this we will be going through PowerShell remoting (PSRemoting).

PowerShell remoting is a feature that enables administrators to manage multiple computers remotely using PowerShell commands. It allows us to execute commands, run scripts, and manage configurations on one or more remote computers from a single console.

With PSremoting, we can run administrative tasks, automate processes, and manage Windows and Linux without needing to access each machine through RDP or direct console.

PowerShell Remoting User Rights

The two local groups allows users can connect to a machines remotely using PSRemoting by default:

  • Administrators
  • Remote Management Users

If users don’t require admin rights on the machine and but they will need to remote on, they should just be added to remote management users. It always best to setup least privileges for security.

Enabling PowerShell Remoting

First we need to enable PSRemoting, if it is not enabled or the WinRM ports are blocked between the two machines. We will receive and error like the below.

PS Remoting Error

To enable PSRemoting we need to run

Enable-PSRemoting
Enable PowerShell Remoting

Once enabled we can now connect using

Enter-PSSession ComputerName
PowerShell Remote Connection

Now that we are connect we can run commands as if we had the PowerShell console open directly on the remote computer.

Running Remote Command

To exit and return back to local PowerShell console, we just need to run

Exit-PSSession
Exiting PowerShell Session

Invoke-Command

Using Enter-PSSession is good for running commands against one machine but if we want to run against multiple machines we can use Invoke-command.

When running we specify the command that will be run in the sriptblock inbetween the two curly brackets {}.

Invoke-Command -ComputerName computer1,computer2 -ScriptBlock { Command}

In the below example I am running against three machines and getting the computersystem WIM class.

PowerShell Remoting Multiple Machines

PowerShell Sessions

Another method to run multiple commands against machines is to create a PowerShell session and then re-using this initial connection.

In the below example I am connect to the three machines, checking the printer spooler service and stopping the service in the third command.

PowerShell Sessions

This has been a quick few examples of setting up and using PSRemoting. Using PSRemoting makes administration a lot easier on remote machines. .

Some security teams don’t want remoting enabled (it is enabled by default on Windows Servers OS since 2012) as they see its as a security risk, while there are risks the benefits out way the risks and there are many ways to harden and reduce security risks associated with PowerShell rather than disabling PSRemoting.

vmware,Azure,microsoft

via TheSleepyAdmins https://ift.tt/E5dj4y2

April 7, 2024 at 12:19PM
TheSleepyAdmin