Jump to content

Batch file that 'knows if it's running automatically (Startup) or manually (by user)?


Recommended Posts

Posted

I'm wondering if there is a way I can have a script 'know' if it's being run as a scheduled task and/or startup script, or if it's being run manually by a user. Could be useful for adding timeouts, pauses, etc, but of course you don't want that if it's running automatically (thus, unattended, with no way to kill it. This way, I'd be able to program tasks without having to make multiple copies of the script with slight variations depending on how it was being executed.

 

I figure maybe the best way is to check the credentials the process is running under? It runs as SYSTEM if it's a startup script, I think?

Posted (edited)

One method would be to write the script to check if it was passed a particular parameter.

If you configure the scheduled task to pass a parameter (e.g. 'scheduled') the script would know if it was called from the scheduler.

 

In batch, you'd do something like this:

if ("%1"=="scheduled") echo I am running from a scheduled task.

 

Similar deal with VBS or Powershell.

Edited by jinnantonnixx
  • Thanks 1
Posted (edited)

You could use an arguement when running the batch file like say...

 

"C:\Cleanup.bat" Auto

 

The batch file will have something like.

 

if "%1"=="auto" goto auto

:manual
SOMETHING HERE
goto end

:auto
SOMETHING HERE
goto end

:end

 

 

Ah... never mind, already answered I see. ;)

Edited by DJ-1701
  • Thanks 1
Posted (edited)

Drat! Should have known it wouldn't be so easy..

 

REM Testing /q

IF (%1 NEQ ^/q) (
ECHO %DATE% %TIME% : Q parameter not passed [Manual launch] >> C:\Temp\Log.txt
) ELSE (
ECHO %DATE% %TIME% : Q parameter passed [Automatic launch] >> C:\Temp\Log.txt
)
PAUSE

 

Testing by double-clicking the batch file (so no Q), then running ParameterTest.bat /q (so passing Q)

 

Log.txt looks like:

10/01/2017 15:39:57.15 : Q parameter not passed [Manual launch] 
10/01/2017 15:40:03.60 : Q parameter not passed [Manual launch] 

 

But when looking at the commands from the 'automatic' cmd prompt (where variables have been enumerated) gives me:

IF (/q NEQ /q) (ECHO 10/01/20....

so shouldn't that give me the second response?

Edited by Garacesh
Posted (edited)

For your parameter test, try:

IF $%1 EQU $ goto noparam

IF $%1 NEQ $ goto param

goto :eof

:noparam
echo No parameter
goto :eof

:param
echo The parameter is %1
goto :eof

 

Batch is rubbish - it's a comedy scripting language. I always prefixed 'variables' with a character so I could later handle the absence of a character by comparing with the prefix. '$' is as good a prefix as any.

 

If no parameter is supplied, line 1 will expand to IF $ EQU $ .... which is an equality, so we know there was no parameter.

Edited by jinnantonnixx
  • Thanks 1
Posted

... False alarm(ish)!

I shouldn't be bracketing off my IF comparison.. Still thinking in Powershell.

Still had to use the dollar though. Without the dollar, /q works, but without /q I get

/q was unexpected at this time.
IF  NEQ ^/q (

 

Working script:

REM Testing /q

IF $%1 NEQ $^/q (
ECHO %DATE% %TIME% : Q parameter not passed [Manual launch] >> C:\Temp\Log.txt
) ELSE (
ECHO %DATE% %TIME% : Q parameter passed [Automatic launch] >> C:\Temp\Log.txt
)
pause

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



×
×
  • Create New...