Executing PowerShell Scripts and Commands (cmdlets) on Remote Computers with BatchPatch

Executing PowerShell commands and scripts remotely can be a bit tricky. BatchPatch currently has direct support for deploying PowerShell scripts to target computers, and we will be adding more PowerShell functionality in the not-too-distant future. However, today I’d like to take a few minutes to go over the current options you have for executing PowerShell commands through BatchPatch on target computers.

Executing PowerShell Scripts:

If you’ve written a powershell script that you simply need to execute on a set of target computers, your easiest option is to use BatchPatch’s deployment feature. BatchPatch will handle copying the file to target systems, executing the script, and then deleting the file.

  1. In BatchPatch, select Actions > Deploy software/patch/script/regkey etc > Create/modify deployment
    2015-07-02 17_48_15-Program Manager
  2. In the Deployment form set a title, browse and for the .ps1 script file. For ease of operation I’m going to save this Deployment using the double right arrow button >>. Once the Deployment has been saved, the Deployment form can be closed.
    2015-07-02 17_53_16-Deploy .msi .msp .msu .exe .reg .vbs .bat .cmd .ps1 etc
  3. Highlight the hosts you want to deploy the script to. I titled my deployment “Execute PowerShell Script 1,” so I will now select Actions > Deploy software/patch/script/regkey etc > Execute saved deployments > Execute PowerShell Script 1. You can see in the screenshot below that when the mouse hovers over the menu item, the configuration of the actual deployment is displayed in a tooltip.
    2015-07-02 17_56_49-
  4. When you click OK you’ll be presented with a confirmation dialog which also displays the configuration of the deployment that is going to be executed. Click OK to continue.
    2015-07-02 18_04_30-new 1 - BatchPatch X1
  5. When the script completes Exit Code: 0 (SUCCESS) is displayed in the ‘All Messages’ column. That’s all there is to it. However, if your script was specifically written to output messages to the console, you could select the “Retrieve console output” checkbox option in the Deployment form when configuring the deployment. However, this setting can cause a deployment to not execute in some cases, so proceed accordingly.

Executing PowerShell Cmdlets:

If you only need to execute a particular powershell cmdlet that’s already available/installed on the target computer, you can do that with a single command instead of deploying a whole script.

  1. Select Actions > Execute remote process/command > Create/modify remote commands (logged output)
    2015-07-02 18_14_28-Program Manager
  2. In the ‘Remote Process’ form, add a new row, give the cmdlet a title, and enter the actual cmdlet into the ‘command’ field. For the sake of this tutorial I’m just going to execute the ‘get-help’ cmdlet. Here’s the key part: Unfortunately you can’t simply enter ‘get-help’ like you would at the command line. In a future build of BatchPatch we will be adding direct support for PowerShell cmdlets so that will be able to execute cmdlets more intuitively, but for the time being you’ll need to enter the following syntax, substituting the cmdlet that you want to run for ‘get-help,’ of course:
    cmd.exe /c echo . | powershell.exe -ExecutionPolicy Bypass -command "get-help"

    Click OK after you’ve created the command.
    2015-07-02 18_21_09-Program Manager

  3. Now highlight the hosts that you would like to execute the cmdlet on, and then select Actions > Execute remote process/command > Execute saved remote commands (logged output) > PowerShell Get-Help Cmdlet. Of course you’ll substitute your own title for ‘PowerShell Get-Help Cmdlet.’
    2015-07-02 18_28_18-
  4. The command completes and we see the output from the cmdlet in the ‘Remote Command Output Log’ column.
    2015-07-02 18_54_06-Program Manager
This entry was posted in Blog, General, Tutorials and tagged , , , . Bookmark the permalink. Both comments and trackbacks are currently closed.