Jump to content

Run autoit program from batch file without waiting


Recommended Posts

Hello,

we have the following situation in our company. Our users start VPN program. Within the VPN program it can be said to execute a batchfile after VPN is established. In our case the batch command starts our logonscript:

powershell.exe "&'\\server\sysvol.logonscript.ps1

Now we have the problem that quite often we run into a race condition. Firewall needs a few more seconds to open ports and the logon script comes too early and cannot connect our networkdrives. Now one could think, ok, just make a short wait-loop into the batchfile. But this is not wanted because users should not see the batch window open and waiting (they could close it for example...) 

Ok, I thought pretty easy would be to make a short autoit program with some parameters which could be launched from the batch file.

I made something like this:

rundelayed.exe /wait 30 /path powershell.exe "&'\\server\sysvol.logonscript.ps1'"

 

My program runs well but now I have the same problem with the batch file staying open until my program closes. Which then is the exact same situation as being native in the batch file.

I tried different start parameters in the batch file, like they are described:

 START "title" [/D path] [options] "command" [parameters]

 

And they work perfectly when I just want to start notepad.exe as it is mostly described in examples. But in my case it doesnt work, because the batch file returns it couldn't find the program /wait which is actually a parameter for my program. But I cannot encapsulate the whole command because this is not supported by the start command (quotes within quotes).

 

So currently I have no idea how to solve this in an elegant way. Some ideas would be highly appreciated. I already thought about an additional ini file where my autoit program can read the necessary paramters (how long to wait and what to start), but wanted to ask here whether someone has a better idea.

 

Link to post
Share on other sites
Posted (edited)

rundelayed.exe /wait 30 /path powershell.exe "&'\\server\sysvol.logonscript.ps1'"

 

My autoit program does this already as you can see at the parameter above. Problem is, that the batch start command does not continue when you run a program with parameters (apart from the file to open).

Example of batchfile(s):

This works

start /d "C:\folder" /b "c:\windows\notepad.exe" " c:\folder\document.txt" 

This doesn't work

start /d "C:\folder" /b "c:\folder\rundelayed.exe" /wait 30 /path powershell.exe "&'\\server\sysvol.logonscript.ps1'"

You get an error where it says /wait is an unknown program. Problem is that the batch start command cannot handle my program when it uses additional parameters.

 

Edited by AutoManfred
Link to post
Share on other sites
Posted (edited)

???

My program uses sleep. I take the param and multiply it with 1000 and this gives me my

sleep(30000)

This leads exactly to the problem i've described. The batch file stays open until the sleep time is over (when I use not the start command within the batch file)

As soon as I use the start command in batch (which is exactly for such purposes) then I cannot handle the additional paramter my program has.

FYI rundelayed.exe is my autoit program which I wrote for the problem of the not closing batch file. It is not some program from the internet or so.

Edited by AutoManfred
Link to post
Share on other sites

I am having a hard time to follow you.  It would have been much easier if you had post a runable example of your problem.  

Let me show you my example :

#include <Constants.au3>

Sleep(5000)
MsgBox ($MB_SYSTEMMODAL, $CmdLine[0], "Test " & $CmdLine[1] & " " & $CmdLine[2])

when I use this command in DOS console :

start /d "." temp6.exe /wait 30

It is NOT waiting the sleep time, it is returning immediately.  And in MsgBox I get the 2 parameters correctly.

Link to post
Share on other sites
  • Moderators

@AutoManfred I just have to ask, why are you using AutoIt to run a Batch file that runs a PowerShell script? All three of these can be turned into an executable, so what is the overarching need to use three scripting languages?

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to post
Share on other sites
Posted (edited)
12 hours ago, Nine said:

I am having a hard time to follow you.  It would have been much easier if you had post a runable example of your problem.  

Let me show you my example :

#include <Constants.au3>

Sleep(5000)
MsgBox ($MB_SYSTEMMODAL, $CmdLine[0], "Test " & $CmdLine[1] & " " & $CmdLine[2])

when I use this command in DOS console :

start /d "." temp6.exe /wait 30

It is NOT waiting the sleep time, it is returning immediately.  And in MsgBox I get the 2 parameters correctly.

Sorry, if I puzzled you Nine. I appreciate even more that you try to help me. Your post led me into the right direction. I understand now why it doesn't work at least

image.gif.f890f0348d284ab3de2d00205e24a3fb.gifimage.gif.7900e0e21ca6f319d58653b99ff4f1b0.gifYour batch command works:

start /d "." temp6.exe /wait 30

but as soon as you put the temp6.exe into quotes it doesn't work anymore. It then says '30 couldn't be found. Ensure the path is correct' (I have it in German, so my translation is probably not perfect). It seems the start command thinks it has to be a filename then.

Now I have to find a better way to use the start command within the batch file with less arguments. Maybe in this case it is better to create an additional ini file where I can store the necessary parameters? Then I could use the batch start command only with the name of my program, as you did it in your example.

 

9 hours ago, JLogan3o13 said:

@AutoManfred I just have to ask, why are you using AutoIt to run a Batch file that runs a PowerShell script? All three of these can be turned into an executable, so what is the overarching need to use three scripting languages?

Of course, this probably appears not logical at the first glance. Ok, let me explain.

We have our normal login script (powershell based), which we use when people are in the office. I must not change it in any way.

To run it when people are at home and using VPN (Cisco Anyconnect) we can run a batch file after the VPN is established. This is a feature of the vpn client and it can run only batch files, nothing else. My colleagues which are responsible for this client made the following little batch script:

PowerShell.exe -Command "&'\\company-ds\sysvol\company-ds.company.com\scripts\login-clients.ps1'"

This works generally but sometimes a race condition occurs, because the virus scanner hasn't open the ports fast enough and the network drives will not be connected then.

Now one could think, ok, just make a 10 sec loop into your batch file. But this is not wanted because then the user would see the open batch window until the loop is over.

So I cannot change the powershell script and I cannot create the waiting loop in the batch file. This led me to the idea I could make a little delayed start program with Autoit.

I hope this explains my situation and you understand now, why I have to use several script languages at once.

Again, thank you all that you try to help me!

 

Edited by AutoManfred
Link to post
Share on other sites
Posted (edited)

UPDATE:

Ok, I made a new autoit program which now collects the params 'wait' and 'command' from an additional ini file.

Now I can start this program from the batch file with just this syntax:

start rundelay.exe

This closes the batch file immediatelly and after the wait time the powershell command + powershell script is executed.

I have to discuss it with my colleagues but I think this could be a good solution 🙂

 

EDIT:

To complete my post here is the little autoit script I've made now:

#include <Misc.au3>

Opt("TrayIconHide", 1)

#Region Vars
$sAppName   =   StringTrimRight(@ScriptName, 4)
$sINI       =   @ScriptDir & "\" & $sAppName & ".ini"
$iWait      =   IniRead($sINI, "PARAMETERS", "Wait","")
$sCommand   =   IniRead($sINI, "PARAMETERS", "Command","")
#EndRegion

If _Singleton("$sAppName", 1) = 0 Then Exit

$iWait *= 1000
Sleep($iWait)

Run($sCommand, "", @SW_HIDE)

 

And the associated ini file:

[PARAMETERS]
Command=PowerShell.exe -Command "&'\\company-ds\sysvol\company-ds.company.com\scripts\login-clients.ps1'"
Wait=20

 

Edited by AutoManfred
Posting the scripts I have made
Link to post
Share on other sites
On 4/8/2021 at 8:40 AM, AutoManfred said:

This doesn't work

start /d "C:\folder" /b "c:\folder\rundelayed.exe" /wait 30 /path powershell.exe "&'\\server\sysvol.logonscript.ps1'"

You get an error where it says /wait is an unknown program. Problem is that the batch start command cannot handle my program when it uses additional parameters.

Solution:

start "" /d "C:\folder" /b "c:\folder\rundelayed.exe" /wait 30 /path powershell.exe "&'\\server\sysvol.logonscript.ps1'"

Note the "" after the start.  Apparently start takes the first quoted arg as the title for the window.

Code hard, but don’t hard code...

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...