{"id":2361,"date":"2015-07-02T19:00:03","date_gmt":"2015-07-02T23:00:03","guid":{"rendered":"http:\/\/batchpatch.com\/?p=2361"},"modified":"2015-07-02T19:10:46","modified_gmt":"2015-07-02T23:10:46","slug":"executing-powershell-commands-on-remote-computers-with-batchpatch","status":"publish","type":"post","link":"https:\/\/batchpatch.com\/executing-powershell-commands-on-remote-computers-with-batchpatch","title":{"rendered":"Executing PowerShell Scripts and Commands (cmdlets) on Remote Computers with BatchPatch"},"content":{"rendered":"<p>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&#8217;d like to take a few minutes to go over the current options you have for executing PowerShell commands through BatchPatch on target computers.<\/p>\n<h2 class=\"brownishOrange\">Executing PowerShell Scripts:<\/h2>\n<p>If you&#8217;ve written a powershell script that you simply need to execute on a set of target computers, your easiest option is to use BatchPatch&#8217;s deployment feature.  BatchPatch will handle copying the file to target systems, executing the script, and then deleting the file.<\/p>\n<ol>\n<li>In BatchPatch, select <strong><em>Actions > Deploy software\/patch\/script\/regkey etc > Create\/modify deployment<\/em><\/strong><br \/>\n<a href=\"\/wp-content\/uploads\/2015\/07\/2015-07-02-17_48_15-Program-Manager.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/wp-content\/uploads\/2015\/07\/2015-07-02-17_48_15-Program-Manager.png\" alt=\"2015-07-02 17_48_15-Program Manager\" width=\"665\" height=\"722\" class=\"aligncenter size-full wp-image-2362\" srcset=\"https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/07\/2015-07-02-17_48_15-Program-Manager.png 665w, https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/07\/2015-07-02-17_48_15-Program-Manager-276x300.png 276w\" sizes=\"auto, (max-width: 665px) 100vw, 665px\" \/><\/a>\n<\/li>\n<li>In the Deployment form set a title, browse and for the .ps1 script file.  For ease of operation I&#8217;m going to save this Deployment using the double right arrow button <strong>>><\/strong>.  Once the Deployment has been saved, the Deployment form can be closed.<br \/>\n<a href=\"\/wp-content\/uploads\/2015\/07\/2015-07-02-17_53_16-Deploy-.msi-.msp-.msu-.exe-.reg-.vbs-.bat-.cmd-.ps1-etc.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/wp-content\/uploads\/2015\/07\/2015-07-02-17_53_16-Deploy-.msi-.msp-.msu-.exe-.reg-.vbs-.bat-.cmd-.ps1-etc.png\" alt=\"2015-07-02 17_53_16-Deploy .msi .msp .msu .exe .reg .vbs .bat .cmd .ps1 etc\" width=\"857\" height=\"450\" class=\"aligncenter size-full wp-image-2366\" srcset=\"https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/07\/2015-07-02-17_53_16-Deploy-.msi-.msp-.msu-.exe-.reg-.vbs-.bat-.cmd-.ps1-etc.png 857w, https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/07\/2015-07-02-17_53_16-Deploy-.msi-.msp-.msu-.exe-.reg-.vbs-.bat-.cmd-.ps1-etc-300x158.png 300w\" sizes=\"auto, (max-width: 857px) 100vw, 857px\" \/><\/a>\n<\/li>\n<li>Highlight the hosts you want to deploy the script to.  I titled my deployment &#8220;Execute PowerShell Script 1,&#8221; so I will now select <strong><em>Actions > Deploy software\/patch\/script\/regkey etc > Execute saved deployments > Execute PowerShell Script 1<\/em><\/strong>.  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.<br \/>\n<a href=\"\/wp-content\/uploads\/2015\/07\/2015-07-02-17_56_49-.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/wp-content\/uploads\/2015\/07\/2015-07-02-17_56_49--1024x681.png\" alt=\"2015-07-02 17_56_49-\" width=\"540\" height=\"359\" class=\"aligncenter size-large wp-image-2368\" srcset=\"https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/07\/2015-07-02-17_56_49--1024x681.png 1024w, https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/07\/2015-07-02-17_56_49--300x199.png 300w, https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/07\/2015-07-02-17_56_49-.png 1158w\" sizes=\"auto, (max-width: 540px) 100vw, 540px\" \/><\/a>\n<\/li>\n<li>When you click OK you&#8217;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.<br \/>\n<a href=\"\/wp-content\/uploads\/2015\/07\/2015-07-02-18_04_30-new-1-BatchPatch-X1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/wp-content\/uploads\/2015\/07\/2015-07-02-18_04_30-new-1-BatchPatch-X1.png\" alt=\"2015-07-02 18_04_30-new 1 - BatchPatch X1\" width=\"663\" height=\"417\" class=\"aligncenter size-full wp-image-2370\" srcset=\"https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/07\/2015-07-02-18_04_30-new-1-BatchPatch-X1.png 663w, https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/07\/2015-07-02-18_04_30-new-1-BatchPatch-X1-300x189.png 300w\" sizes=\"auto, (max-width: 663px) 100vw, 663px\" \/><\/a>\n<\/li>\n<li>When the script completes Exit Code: 0 (SUCCESS) is displayed in the &#8216;All Messages&#8217; column.  That&#8217;s all there is to it.  However, if your script was specifically written to output messages to the console, you could select the &#8220;Retrieve console output&#8221; 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.<\/li>\n<\/ol>\n<h2 class=\"brownishOrange\">Executing PowerShell Cmdlets:<\/h2>\n<p>If you only need to execute a particular powershell cmdlet that&#8217;s already available\/installed on the target computer, you can do that with a single command instead of deploying a whole script.<\/p>\n<ol>\n<li>Select <strong><em>Actions > Execute remote process\/command > Create\/modify remote commands (logged output)<\/em><\/strong><br \/>\n<a href=\"\/wp-content\/uploads\/2015\/07\/2015-07-02-18_14_28-Program-Manager.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/wp-content\/uploads\/2015\/07\/2015-07-02-18_14_28-Program-Manager.png\" alt=\"2015-07-02 18_14_28-Program Manager\" width=\"694\" height=\"726\" class=\"aligncenter size-full wp-image-2372\" srcset=\"https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/07\/2015-07-02-18_14_28-Program-Manager.png 694w, https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/07\/2015-07-02-18_14_28-Program-Manager-287x300.png 287w\" sizes=\"auto, (max-width: 694px) 100vw, 694px\" \/><\/a>\n<\/li>\n<li>In the &#8216;Remote Process&#8217; form, add a new row, give the cmdlet a title, and enter the actual cmdlet into the &#8216;command&#8217; field.  For the sake of this tutorial I&#8217;m just going to execute the &#8216;get-help&#8217; cmdlet.  <strong>Here&#8217;s the key part<\/strong>:  Unfortunately you can&#8217;t simply enter &#8216;get-help&#8217; 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&#8217;ll need to enter the following syntax, substituting the cmdlet that you want to run for &#8216;get-help,&#8217; of course:\n\n<div class=\"wp_syntax\"><table><tr><td class=\"code\"><pre class=\"powershell\" style=\"font-family:monospace;\">cmd.exe <span style=\"color: pink;\">\/<\/span>c <span style=\"color: #008080; font-weight: bold;\">echo<\/span> . <span style=\"color: pink;\">|<\/span> powershell.exe <span style=\"color: #008080; font-style: italic;\">-ExecutionPolicy<\/span> Bypass <span style=\"color: #008080; font-style: italic;\">-command<\/span> <span style=\"color: #800000;\">&quot;get-help&quot;<\/span><\/pre><\/td><\/tr><\/table><\/div>\n\n<p>Click OK after you&#8217;ve created the command.<br \/>\n<a href=\"\/wp-content\/uploads\/2015\/07\/2015-07-02-18_21_09-Program-Manager.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/wp-content\/uploads\/2015\/07\/2015-07-02-18_21_09-Program-Manager.png\" alt=\"2015-07-02 18_21_09-Program Manager\" width=\"662\" height=\"456\" class=\"aligncenter size-full wp-image-2374\" srcset=\"https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/07\/2015-07-02-18_21_09-Program-Manager.png 662w, https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/07\/2015-07-02-18_21_09-Program-Manager-300x207.png 300w\" sizes=\"auto, (max-width: 662px) 100vw, 662px\" \/><\/a>\n<\/li>\n<li>Now highlight the hosts that you would like to execute the cmdlet on, and then select <strong><em>Actions > Execute remote process\/command > Execute saved remote commands (logged output) > PowerShell Get-Help Cmdlet<\/em><\/strong>.  Of course you&#8217;ll substitute your own title for &#8216;PowerShell Get-Help Cmdlet.&#8217;<br \/>\n<a href=\"\/wp-content\/uploads\/2015\/07\/2015-07-02-18_28_18-.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/wp-content\/uploads\/2015\/07\/2015-07-02-18_28_18--1024x865.png\" alt=\"2015-07-02 18_28_18-\" width=\"540\" height=\"456\" class=\"aligncenter size-large wp-image-2376\" srcset=\"https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/07\/2015-07-02-18_28_18--1024x865.png 1024w, https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/07\/2015-07-02-18_28_18--300x253.png 300w, https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/07\/2015-07-02-18_28_18-.png 1153w\" sizes=\"auto, (max-width: 540px) 100vw, 540px\" \/><\/a>\n<\/li>\n<li>The command completes and we see the output from the cmdlet in the &#8216;Remote Command Output Log&#8217; column.<br \/>\n<a href=\"\/wp-content\/uploads\/2015\/07\/2015-07-02-18_54_06-Program-Manager.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/wp-content\/uploads\/2015\/07\/2015-07-02-18_54_06-Program-Manager.png\" alt=\"2015-07-02 18_54_06-Program Manager\" width=\"666\" height=\"733\" class=\"aligncenter size-full wp-image-2378\" srcset=\"https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/07\/2015-07-02-18_54_06-Program-Manager.png 666w, https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/07\/2015-07-02-18_54_06-Program-Manager-273x300.png 273w\" sizes=\"auto, (max-width: 666px) 100vw, 666px\" \/><\/a>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;d like to take a few minutes to go over the current options you have for executing PowerShell [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1,17,3],"tags":[145,142,143,144],"class_list":["post-2361","post","type-post","status-publish","format-standard","hentry","category-blog","category-general","category-tutorials","tag-cmdlet","tag-powershell","tag-remote-powershell","tag-script"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/batchpatch.com\/wp-json\/wp\/v2\/posts\/2361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/batchpatch.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/batchpatch.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/batchpatch.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/batchpatch.com\/wp-json\/wp\/v2\/comments?post=2361"}],"version-history":[{"count":9,"href":"https:\/\/batchpatch.com\/wp-json\/wp\/v2\/posts\/2361\/revisions"}],"predecessor-version":[{"id":2380,"href":"https:\/\/batchpatch.com\/wp-json\/wp\/v2\/posts\/2361\/revisions\/2380"}],"wp:attachment":[{"href":"https:\/\/batchpatch.com\/wp-json\/wp\/v2\/media?parent=2361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/batchpatch.com\/wp-json\/wp\/v2\/categories?post=2361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/batchpatch.com\/wp-json\/wp\/v2\/tags?post=2361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}