Executing PowerShell Scripts and Commands on Many Remote Computers

One of the things that BatchPatch does very well is remote script execution on many computers all at the same time. The ability to remotely execute a single script on numerous computers, simultaneously, with just a few clicks, is one of the awesome features of BatchPatch. No administrator wants to repeat a single process over and over on many computers. What about when the script you want to execute is written in PowerShell? How do you go about executing that single PowerShell script on all of your networked computers?

First, before you try to execute a single PowerShell script on numerous computer, you need to make sure that the script at least executes successfully on a single computer. One of the annoyances with PowerShell is that if you don’t have the same version of PowerShell on your computer, you might find that the syntax of certain portions of your scripts might only work on version 3 of PowerShell, which would obviously be a problem if some or many of computers are currently only on version 2 of PowerShell.

The first thing you’ll want to do is check to see which version of PowerShell is installed on your target systems. You can use the following PowerShell command to get the version information:

$PSVersionTable.PSVersion

If you want to use BatchPatch to check the version of PowerShell on all of your target systems, here’s what to do.

  1. Highlight the target hosts in BatchPatch, and then select ‘Actions > Execute remote process/command > Create/modify remote command 3 (logged output)
  2. In the command field we type or paste the following command exactly as it is written below:
    cmd.exe /c echo . | powershell.exe -ExecutionPolicy Bypass -command "$PSVersionTable.PSVersion"

    2015-09-14 15_29_01-new 1 - BatchPatch X2

  3. Note, if you think you might want to re-use the same command in BatchPatch more than once, then feel free to add the command to ‘Actions > Execute remote process/command > Create/modify remote commands (logged output), which will allow you to “permanently” hard-code any commands into the BatchPatch menu.
  4. Click ‘execute’ to run the command on the selected hosts. You can see the results below from my quick run. I have one computer with version 3 of PowerShell, while the other still has version 2.
    2015-09-14 15_32_20-Add New Post ‹ BatchPatch - The Ultimate Windows Update Tool — WordPress
  5. Now, let’s say you want to run a multi-line PowerShell script on your target machines. After verifying the PowerShell version that you have running on target computers and after making sure that your desired script works properly on at least one computer from each group (just to make sure that the syntax of your PowerShell script works on all versions of PowerShell being used in your environment, you now have two options for executing it from BatchPatch.

    One option is to use the BatchPatch deployment feature. Please review this tutorial: Executing Powershell Commands On Remote Computers With BatchPatch You may also review the Software Deployment page to learn about BatchPatch deployments, in general. In this case instead of deploying a software package or an update, we want to deploy a PowerShell script. So, we would highlight the desired target computers, and then we’d select ‘Actions > Deploy > Create/modify deployment.’ In the window that appears we would select the script to be deployed, and then we would check the “retrieve console output’ if our script outputs anything to the console that we want to see. And then we can simply choose “Execute now” to execute the deployment and have BatchPatch copy the script to the target systems and subsequently execute it on each system.
    2015-09-14 15_38_35-Deploy .msi .msp .msu .exe .reg .vbs .bat .cmd .ps1 etc

    A second option is to just run the script directly on target systems without doing a full deployment. In this case we would need to convert our script into the right format for it to be able to run in a single line. So, for example, let’s make up a very simple multi-line script to use for this example.

    $version = $PSVersionTable.PSVersion
    $version | format-list

    In order to execute this using the ‘Remote process/command’ feature of BatchPatch, we have to convert it into a single-line for execution. In PowerShell the semi-colon is used to separate multiple lines into a single line. So, in this case we would follow the same example that was given higher up in this tutorial, but note that this time we substitute in the above 2-line script.

  6. Highlight the target hosts in BatchPatch, and then select ‘Actions > Execute remote process/command > Create/modify remote command 3 (logged output)
  7. In the command field we type or paste the following command exactly as it is written below:
    cmd.exe /c echo . | powershell.exe -ExecutionPolicy Bypass -command "$version = $PSVersionTable.PSVersion; $version | format-list"

    2015-09-14 15_29_01-new 1 - BatchPatch X2

  8. In the results screenshot below you can see our 2-line script was successful, and now we see our version outputted in list format.
    2015-09-14 16_22_31-new 1 - BatchPatch X2
This entry was posted in Blog, General, Tutorials and tagged , . Bookmark the permalink. Both comments and trackbacks are currently closed.