BatchPatch Forums Home › Forums › BatchPatch Support Forum › Executing Powershell Scripts through BatchPatch
- This topic has 3 replies, 2 voices, and was last updated 10 years, 10 months ago by doug.
-
AuthorPosts
-
January 23, 2014 at 11:17 pm #8628stuartParticipant
Hi Doug, hoping you can help me. Firstly great job on the latest release. The changes with the multiple schedule function etc is great. Will make it a lot easier to multiple schedules. Awesome to see that you actioned requests/suggestions so rapidly.
One thing that I am trying to do is deploy .ps1 powershell scripts remotely, specifically in this case to migrate a Hyper-V cluster. Now the scrips that I have work fine and I can execute them from a powershell prompt and they work. The way I have been doing it manually remotely is a script with the command below. This invokes the execution of the script ‘clusterfailover1-2.ps1’ which is located in the same folder.
remotescriptrun.ps1
Invoke-Command -ComputerName %myserver% -FilePath .clusterfailover1-2.ps1
The problem I have is I can’t get BatchPatch to execute ‘remotescriptrun.ps1’. I have tried ensuring the ‘copy entire directory’ is selected (obviously using the deploy script function). And also manually changing the ‘command to execute’ field to something like powershell.exe -file “C:Users%MYUSERNAMEDocumentsBatchPatchScriptsSQLremotescriptrun.ps1” but can’t get to execute.
Just wondering if you had any suggestions. Ultimately all I need to do is run the remotescriptrun.ps1 from the system I have BP installed on (obviously this executes a second script in the same folder) but I just can’t get it to work. I tried additional parameters with the powershell command to force executionpolicy etc but to no avail.
I understand this could potentially be venturing a little outside of BP’s functionality but any assistance you could provide would be greatly appreciated.
Thanks in advance.
January 24, 2014 at 4:26 pm #9660dougModeratorHi Stuart –
If I’m understanding correctly, you just need to execute a powershell script locally, on the computer that you are running BatchPatch on, and you want to pass the remote computer name to that script as a parameter. I do this all the time, and I see no reason why you shouldn’t be able to.
The first problem that I think you’re having is that you’re trying to use the Deployment action to deploy a script that you want to run locally. The problem here is that the script you want to run needs to run on your local BatchPatch computer, but the Deployment action would copy that script to the remote system and then execute it remotely on that target system, which is not what you want/need, in your example.
This is exactly how I do it with success. Hopefully you can use this as a template for your script:
Use
Actions > Local process/command > Create/modify local command
The Local process/command action will run the script locally, on the computer that is running BatchPatch, NOT remotely on the target system, and since the script you are running already handles the remote execution portion, you don’t need BatchPatch do that.
Then enter something like this (note that when you use $computer as a parameter, it will substitute the actual target computer name that you have in the Host column for that row. The $computer parameter is optional, but it’s nice because if you setup your script to take the computer name as a parameter, then it’s easy to use BatchPatch to execute the script on many machines by simply creating one row per machine, and then run the identical script in the Local process/command action of each row, using the $computer parameter:
powershell.exe c:myScriptsFoldermyPowerShellScriptsscriptName.ps1 $computer
When you’re ready to execute the script, you would then simply click on
Actions > Local process/command > Execute local command
I hope this helps. Let me know how it goes.
-Doug
January 29, 2014 at 4:22 am #9665stuartParticipantHi Doug, that helps a lot! Didn’t think to use the local command as assumed it would execute the command on the local machine. Which it does but not the way I thought! 🙂 I’ve been able to successfully execute a couple of test commands but have created some more automation for this week so ill see how that goes.
Thanks again for the prompt response and assistance.
January 29, 2014 at 3:20 pm #9666dougModeratorGreat news. I’m glad you got it working.
-Doug
-
AuthorPosts
- You must be logged in to reply to this topic.