Garacesh Posted January 10, 2017 Posted January 10, 2017 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?
jinnantonnixx Posted January 10, 2017 Posted January 10, 2017 (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 January 10, 2017 by jinnantonnixx 1
Garacesh Posted January 10, 2017 Author Posted January 10, 2017 That's.. considerably more logical than the way I was thinking of doing it..
jinnantonnixx Posted January 10, 2017 Posted January 10, 2017 That's.. considerably more logical than the way I was thinking of doing it.. Sometimes it's hard to think of the easy way....
DJ-1701 Posted January 10, 2017 Posted January 10, 2017 (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 January 10, 2017 by DJ-1701 1
Garacesh Posted January 10, 2017 Author Posted January 10, 2017 (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 January 10, 2017 by Garacesh
jinnantonnixx Posted January 10, 2017 Posted January 10, 2017 (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 January 10, 2017 by jinnantonnixx 1
Garacesh Posted January 10, 2017 Author Posted January 10, 2017 ... 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now