Run bat file rebooting

BatchPatch Forums Home Forums BatchPatch Support Forum Run bat file rebooting

  • This topic has 15 replies, 2 voices, and was last updated 10 years ago by doug.
Viewing 16 posts - 1 through 16 (of 16 total)
  • Author
    Posts
  • #9081
    JohnFLi
    Participant

    When installing Windows updates, and before rebooting using batchpatch, is it possible to execute a bat file that is on the machine batchpatch is running on?

    The purpose is this;

    We have all our servers monitored by Nagios. I have bat files on my machine that turns off alerting for individual servers.

    In my dream world, I would like batchpatch to run a bat file prior to rebooting, and then have another bat file run 5 or so min after batch patch says the system is back up. (to turn monitoring back on for that server)

    #10841
    doug
    Moderator

    Hi John –

    Yes you can do this using the ‘Job Queue.’

    You would need to create a ‘Local Command 1’ and a ‘Local Command 2’ for a given row (or rows). Local Command 1 would execute the batch file that disables alerts for $computer, and Local Command 2 would execute the batch file that enables alerts for $computer. Note that inside the ‘Local Command’ window you are able to specify $computer as a variable to pass into your script, assuming your script is setup to receive the computer name as a parameter. When $computer is specified in a BatchPatch Local Command, BatchPatch will then substitute the computer name from the ‘Hosts’ column in place of $computer. This means that can use the same Local Command for every row instead of having to customize the command in each row with each row’s computer name.

    Once you have Local Commands 1 and 2 setup for your row(s), you would then create a Job Queue for the row(s) that looks something like:

    Execute Local Command 1

    Download/Install Windows Updates + Reboot

    Wait 10 minutes

    Execute Local Command 2

    I hope this helps.

    Here is a video demo of the Job Queue: https://www.youtube.com/watch?v=JwxhgprfZ5Y

    Here is a tutorial that uses $computer in a Local Command: Using BatchPatch to Execute a Custom Script to Retrieve the Count of CPU Sockets, Cores, and Logical Processors from Remote Computers

    Note, we have a handful of improvements to the job queue coming soon, and so the process that you are setting up here will be even easier once those updates have been published. However, in the meantime you can still do what you want pretty easily.

    -Doug

    #10842
    JohnFLi
    Participant

    OK, I will have to give that a try

    #10843
    JohnFLi
    Participant

    well, that did not work. The bat file only has 1 line of code, and when run manually, it takes less than a second to complete. It have been running for over 5 min. (Execute local command 1)

    Job Queue

    Execute local command 1

    Download and install updates + reboot if required

    Wait 5 minutes

    Execute local command 2

    Local Command 1

    powershell.exe c:tempg1vplncarc02-off.bat

    Local Command 2

    powershell.exe c:tempg1vplncarc02-on.bat

    Local Command Output Log

    Executing powershell.exe c:tempg1vplncarc02-off.bat

    All Messages

    Wed-15:53:28> Local Command: Executing powershell.exe c:tempg1vplncarc02-off.bat

    Wed-15:53:28> Job Queue: Initiating ‘Execute local command 1’

    Wed-15:53:28> Job Queue: Queued…

    #10844
    doug
    Moderator

    John – What are the contents of the .bat file? Can you post here? I should then be able to help you get this worked out.

    Generally speaking if a local or remote command is hanging indefinitely it’s because it’s waiting for user input of some kind. However, when BatchPatch executes these commands they are hidden and cannot accept user input. I’m not sure if your script is expecting user input of some kind, but if it is then that’s where your problem is. If not, then I’m not sure what’s going on and would then like to see the actual script contents.

    Thanks,

    -Doug

    #10845
    JohnFLi
    Participant

    the content of the bat file is:

    plink.exe g1vpnag01 -pw mnbJHG_7tT ./g1vplncarc02off.sh

    What this does, is it calls the program “plink” and plink then goes logs into the machine ‘g1vpnag01’ and runs the file ‘g1vplncarc02off.sh’

    here is one I tried just now

    G1VPLNCARC02

    Windows Update Messages

    1 update(s) found – 15:01:13

    Remote Agent Log

    G1VPLNCARC02 11/26/2014 15:01:08

    ::Begin online search – Server Selection: Default

    The search query “ImportantAndRecommended” returned 1 update(s):

    1> Security Update for Windows Server 2012 R2 (KB3011780) (1 MB) – Security Updates

    (Type-SoftwareUpdate, Downloaded-TRUE, Installed-FALSE, RebootRequired-MAYBE)

    ::End search

    Job Queue

    Execute local command 1

    Download and install updates + reboot if required

    Wait 5 minutes

    Execute local command 2

    Local Command 1

    powershell.exe c:tempg1vplncarc02-off.bat

    Local Command 2

    powershell.exe c:tempg1vplncarc02-on.bat

    Local Command Output Log

    Executing powershell.exe c:tempg1vplncarc02-off.bat

    All Messages

    Fri-08:07:12> Local Command: Executing powershell.exe c:tempg1vplncarc02-off.bat

    Fri-08:07:12> Job Queue: Initiating ‘Execute local command 1’

    Fri-08:07:12> Job Queue: Queued…

    #10846
    doug
    Moderator

    Thanks, John.

    So, when you run the script manually at the command line (without BatchPatch) it (plink) doesn’t prompt for any input such as a username or password? When BatchPatch runs a local command, it should in theory be identical to running a command at the command line with just one exception, which is that it runs hidden and doesn’t allow user interaction. That said, if your script doesn’t normally prompt for any input yet BatchPatch still hangs on the local command, indefinitely, then the only other thing I can think of that might cause that is if the script requires elevation to run the plink.exe. What happens if you run the entire BatchPatch instance with elevation (right-click on the BatchPatch.exe and choose to run as administrator). Does this solve the problem? Also note, you can test the local command directly without running the job queue by simply choosing ‘Actions > Execute Local Process/Command > Execute Local Command 1’

    Let me know how it goes.

    -Doug

    #10847
    JohnFLi
    Participant

    Correct, the bat file does NOT require any intervention. When I run the BatchPatch program, I run it with a domain administrator account (that account is also a local admin as well)

    For testing, I even did the “Right click, Run as Administrator” still didn’t do anything.

    Plink doesn’t require elevation to run.

    I also tried your suggestion ‘Actions > Execute Local Process/Command > Execute Local Command 1’ same result.

    Is there any other logging I can turn on?

    I also tried seeing if the program even starts and it does not show up in task manager. Meaning that it looks like it isn’t even trying to run the bat file.

    Instead of using powershell “powershell.exe c:tempg1vplncarc02-off.bat ” I tried “cscript c:tempg1vplncarc02-off.bat “

    It gives an error as expected, so at least it shows that it is trying with cscript.

    #10848
    JohnFLi
    Participant

    as a quick test i made a new bat file

    the content of the bat file was just pause

    and the result was the same

    #10849
    doug
    Moderator

    John – A couple points…

    1. A batch file (.cmd or .bat) generally should be executed with cmd.exe, not powershell.exe and not cscript.exe. In BatchPatch when you execute a .bat/.cmd via local command, just enter the path to the batch file. In theory, the powershell.exe ought to be able to run a .bat file without issues, but since you’re having problems, please try removing it as it’s not necessary and it’s slightly abnormal (cmd.exe would be more appropriate, but it’s also not necessary), and perhaps it is part of the problem. So, the syntax in your case for the local command should just be set to:

    c:tempg1vplncarc02-off.bat

    2. Since in your case the batch file is executing plink.exe, please open task manager and watch the processes list for plink.exe when the local command is executing. If plink.exe appears, then we’ll know that it’s being executed. If it doesn’t appear, then we’ll know it’s somehow stuck before it even executes.

    I just did several tests running batch files as described above using local command, and they all worked properly. However, of course none of them execute plink.exe, so it’s possible that the problem is somehow tied to that, but realistically it *should* work just fine.

    I know you said you tested a batch file containing “pause” but that’s the one thing that you should NOT test because it will “pause” indefinitely and produce exactly the behavior that we are trying to avoid. Instead, test something like “ipconfig” (without the quotes, of course) inside of the batch file. That would be a much better test and it should return the ip configuration of the machine to the Local Command Output Log column.

    Keep me posted.

    -Doug

    #10850
    JohnFLi
    Participant

    I did the ‘pause’ test becasue I wanted to catch it in task manager….it did not

    Also, I already did what you said in #2 and I did not see plink in the task manager either.

    I tried changing powershell.exe to cmd.exe….same result.

    Also tried just using “c:tempg1vplncarc02-off.bat”…..same result. (without quotes of course)

    The log when trying without powershell, or cmd is:

    Local Command Output Log

    Error: Cannot start process because a file name has not been provided.

    All Messages

    Fri-10:47:39> Local Command: Error: Cannot start process because a file name has not been provided.

    Fri-10:47:39> Local Command: Executing c:tempg1vplncarc02-off.bat

    Fri-10:47:39> Local Command 1: Queued…

    #10851
    doug
    Moderator

    What version of BatchPatch are you running? This is peculiar.

    With regard to the “Cannot start process because a file name has not been provided” message, it should (in theory) know to execute using the shell, but for some reason it’s not doing that on your machine. I wonder if that might be due to an environment variable setting, and I wonder if a missing environment variable is also the cause for the other issue we’re experiencing. I will look into that more, but for the moment if let’s scratch the plan I suggested before to not use cmd.exe. Instead let’s use the cmd.exe command syntax:

    cmd.exe /C c:tempg1vplncarc02-off.bat

    Please give that a try. If it doesn’t work, please try:

    C:WindowsSystem32cmd.exe /C c:tempg1vplncarc02-off.bat

    Let’s see what happens now. Again please monitor the task manager and see if plink.exe appears.

    Thanks,

    Doug

    #10852
    JohnFLi
    Participant

    version: 2014.11.7.16.6

    Job Queue

    Execute local command 1

    Download and install updates + reboot if required

    Wait 5 minutes

    Execute local command 2

    Local Command 1

    c:windowssystem32cmd.exe /C c:tempg1vplncarc02-off.bat

    Local Command Output Log

    C:Johns BackupBatch-Patch>plink.exe g1vpnag01 -pw mnbJHG_7tT ./g1vplncarc02off.sh

    All Messages

    Fri-11:40:33> Local Command: Exit Code: 1

    Fri-11:40:33> Local Command: Executing c:windowssystem32cmd.exe /C c:tempg1vplncarc02-off.bat

    Fri-11:40:33> Local Command 1: Queued…

    #10853
    doug
    Moderator

    That’s progress, at least. Not sure why it’s failing though. What happens if you simply put the plink.exe command directly into the local command 1 *instead* of using the .bat file:

    plink.exe g1vpnag01 -pw mnbJHG_7tT ./g1vplncarc02off.sh

    By the way, what happened when you tested a batch file that only contained “ipconfig” without the quotes? That worked, I assume, when you used the following syntax?:

    C:WindowsSystem32cmd.exe /C c:tempipconfig_test.bat

    -Doug

    #10854
    JohnFLi
    Participant

    well what do you know….

    c:tempplink.exe g1vpnag01 -pw mnbJHG_7tT ./g1vplncarc02off.sh

    It worked!!!

    (I had to put the path to plink)

    So with that, I modified local command 2 to turn the monitoring back on after 10 min.

    and all seems to be working as it should.

    #10855
    doug
    Moderator

    Great! I don’t know why it hadn’t occurred to me before, but I guess the issue with the .bat file was that the plink.exe needed the full c:tempplink.exe filepath inside the .bat. I’m really glad to hear that it’s working now!

    -Doug

Viewing 16 posts - 1 through 16 (of 16 total)
  • You must be logged in to reply to this topic.