How to Run PowerShell Commands on Remote Computers

1008
How to Run PowerShell Commands on Remote Computers

Here we can see, “How to Run PowerShell Commands on Remote Computers”

The functionality of the remote command execution in PowerShell is PowerShell Remoting (appeared in PowerShell 2.0) and supported the online Services for Management protocol (WS-Management). With PowerShell Remoting, you’ll run commands on one or several remote computers. In addition, you’ll use the interactive session mode with remote computers, a short-lived or permanent connection. Earlier, we’ve covered the way to run the PowerShell script from Task Scheduler. During this article, we’ll glance at several samples of how to execute a PowerShell script remotely.

Configuring WinRM for PowerShell Remoting

To connect to a computer remotely via PowerShell, the WinRM (Windows Remote Management service) must be enabled and configured thereon (it is disabled by default). Communication between computers is performed over HTTP or HTTPS protocols, and every network traffic between computers is encrypted. You’ll use NTLM and Kerberos to authenticate on a foreign computer.

get-service winrm

As you’ll see, the WS-Management service is running.

If the WinRM service isn’t running, you want to enable it on the remote computer with the command:

Enable-PSRemoting

This command prepares the pc for remote management: starts the WinRM service, changes startup type to Automatic, and adds necessary exceptions to Windows Defender Firewall.

If the remote computer is during a workgroup (not joined to the Active Directory domain), and a Public network profile is applied to that (instead of Domain or Private), you would like to allow incoming WinRM traffic in Windows Firewall explicitly:

Set-NetFirewallRule -Name "WINRM-HTTP-In-TCP-PUBLIC" -RemoteAddress Any

To test the connection to a foreign server via WinRM, use the subsequent command:

Test-WSMan server1

If you get a response, then the remote computer is accessible through PowerShell Remoting.

Also See:  0x100000ea (THREAD STUCK IN DEVICE DRIVER M) – Fix for Windows XP, Vista, 7, 8, 8.1, 10

Hint. If you’re connecting to a foreign computer via PS Remoting by an IP address, you’ll receive an error:

Connecting to remote server 192.168.1.70 failed with the subsequent error message: The WinRM client cannot process the request. Default authentication could also be used with an IP address under the following conditions: the transport is HTTPS, the destination is within the TrustedHosts list, and explicit credentials are provided.

In this case, you would like to put in an HTTPS certificate for PowerShell Remoting on the remote computer (the long way) or add this host to the trusted ones on your management computer:

Set-Item wsman:\localhost\Client\TrustedHosts -value 192.168.1.70

Running Remote Commands with PowerShell Remoting

To interactively hook up with a foreign computer (with a hostname Server1) via PowerShell, run the subsequent command:

Enter-PSSession Server1

The PowerShell CLI view will change. At the start of the road, there’ll be the name of the remote computer to which you’re connected via WinRM. After the remote session is established, all commands entered within the PowerShell console are executed on the remote computer. PS Remoting works as follows: the commands entered on the local computer are transmitted to the remote computer and are executed there, then the result’s transmitted back. Since all commands are executed locally, there’s no got to worry about compatibility with the luxury version and modules.

To end the remote interactive session, run the command:

Exit-PSSession

Only the straightforward management tasks are typically performed on remote computers within the interactive mode. To run a posh command or run the PowerShell script remotely, use the Invoke-Command cmdlet.

Using Invoke-Command to Run PowerShell Scripts Remotely

The following command will create a foreign reference to the pc Server1 and run the block of commands laid out in the ScriptBlock parameter. Then, the remote session will automatically close.

Invoke-Command -ScriptBlock {Restart-Service spooler} -ComputerName server1

You can run the task within the background by running Invoke-Command with the -AsJob parameter. But during this case, the command won’t return the result to the luxury console. to urge the detailed background job information, use the Receive-Job cmdlet.

PowerShell allows you to run local PS1 scripts on remote computers. the thought is that you store all PowerShell instructions during a local.PS1 file on your computer. With PowerShell Remoting, you’ll transfer a PS1 file to a foreign computer and execute it there.

To do this, use the -FilePath parameter within the Invoke-Command cmdlet rather than -ScriptBlock. For instance, to run the c:\ps\tune.ps1 script on three remote servers, you’ll use the subsequent command:

Invoke-Command -FilePath c:\ps\tune.ps1 -ComputerName server1,server2,server3

The main advantage of running PowerShell scripts is that you don’t get to copy the PS1 script file to remote computers. Instead, you’ll use the local script and the PS script during a shared network folder, which will be accessed from the local computer.

If you would like to run PowerShell scripts with credentials aside from the present user, you would like to use the Credential parameter.

Also See:  The Xbox One Error Code E203

First, you would like to urge the credential and save them to a variable:

$cred = Get-Credential

Now you’ll run the PS script on remote computers under the saved credential permissions.

Invoke-Command -FilePath c:\ps\tune.ps1 -ComputerName server1,server2,server3 -Credential $cred

You can save the list of computers during a document and run PowerShell script remotely on all computers at once:

Invoke-command -comp (get-content c:\ps\servers.txt) -filepath c:\ps\tune.ps1

By default, the Invoke-Command cmdlet sends the PS1 script to 32 remote computers from the list at an equivalent time. If there are quite 32 computers, then PoSh checks the execution status of the script on the primary 32 computers. If the script is completed, the command is executed on a subsequent computer. With the ThrottleLimit parameter, you’ll increase this limit, but take care not to overload your network.

Using Persistent PowerShell Connections (Sessions)

Each time you run Invoke-Command, a replacement session is made with the remote computer. This takes time and system resources. In PowerShell, you’ll create one session and execute all commands and scripts in it.

Using the New-PSSession cmdlet, you’ll create persistent PowerShell sessions with remote computers.

For example, let’s create sessions with three computers and save them within the $PSSess variable:

Invoke-Command -FilePath c:\ps\tune.ps1 -ComputerName server1,server2,server3

$PSSess = New-PSSession -ComputerName server1, server2, server3

After establishing a session, you’ll run it to run commands and scripts. Because sessions are persistent, you’ll get data from them and use it in other commands and scripts.

For example, the subsequent command will get an inventory of processes on remote servers and store them within the $RemoteProcesses variable:

Invoke-Command -Session $PSSess {$RemoteProcesses = Get-Process}

Now you’ll use this variable in other commands within the same sessions. Within the following example, we use the Where-Object cmdlet to seek out processes that use quite 500MB of RAM):

Invoke-Command -Session $PSSess {$RemoteProcesses | where-object {$_.WorkingSet -GT 500000*1024}|select processname,@{l="Working Memory (MB)"; e={$_.workingset / 1mb}} |sort "Working Memory (MB)" -Descending}

The persistent remote PowerShell session will remain active until you shut the PowerShell console or forcefully close or delete the session using the Disconnect-PSSession or Remove-PSSession cmdlets, respectively.

User Questions:

1.What is invoke-command in PowerShell?

Description. The Invoke-Command cmdlet runs commands on an area or remote computer and returns all output from the commands, including errors. … you’ll also use Invoke-Command on an area computer to a script block as a command. PowerShell runs the script block immediately during a child scope of the present scope.

2.What is the IEX command?

iex is an alias for Invoke-Expression. Here the 2 backticks don’t make any difference but just obfuscates the command touch. iex executes a string as an expression, even from the pipe. Here Start-Process may be a cmdlet that starts processes.

3.Why is employed in PowerShell?

Windows PowerShell may be a Microsoft framework for automating tasks employing a command-line shell and an associated scripting language. However, when released in 2006, this powerful tool was replaced promptly because the default thanks to automating batch processes and making customized system management tools.

Also See:  How To Transfer Pictures From iPhone To Computer: The Best Way!

4.Running Powershell script on remote PC

Running Powershell script on remote PC from PowerShell

5.Invoke-Command on the remote computer to run a foreign program

Invoke-Command on remote computer to run a remote program from PowerShell