How do I use the if statement with WMIC - batch-file

this is my first post. I am using a batch command to check if an instance of my program is already running based on its command line argument. I am able to see if it is running by using the following code:
wmic Path win32_process Where "CommandLine Like '%Account A%' AND Caption Like '%Leads Manager.exe%'"
If the process exists, I will get a full break down of the process information, but if the process isn't running i get No Instance(s) Available.
I want to be able to run an If statement, so if the result shows No Instance(s) Available. I want to run a command without displaying the process information and if it does exists, I want to echo Account A is already running
Hopefully someone with more experience can point me in the right direction

Here's a batch-file example, which may help you out:
#Echo Off
For %%A In (A B C D) Do WMIC Process Where^
"Name='Leads Manager.exe' And Not CommandLine Like '%%Account %A%%%'"^
Get Name 2>Nul|Find "Name">Nul && (
Start "" "C:\Leads Manager.exe" -- "C:\Account %%A.ini")
This example uses the outer For loop to choose the Account letters, it is therfore checking to see if Leads Manager.exe is running with a commandline containing the string Account A, Account B, Account C or Account D, and if not it should run Leads Manager specifying the path to the appropriate account. (Please note that your .exe and .ini file paths both contain spaces, so I have doublequoted them, but left the rest of the command as you wrote it in your comment. You may want to consider checking that what you provided was correct and adjust it as necessary before testing, or replace that line entirely with a simple Echo Is Not Running) || Echo Is Running) statement, or perhaps run another batch file instead).

Related

Batch Scripting percent tilde string and starting a process syntax understanding

I need help with understanding the following Batch script structure:
This is named Profile_something_schedule.bat
call somePath\lib.cmd :someLabel reqPath
call somePath\lib.cmd :someLabel reqKey
%reqPath% "%~someFileName" /vv_pwd=%reqKey% /bProfile_something_schedule /min
I have a lot of difficulty understanding why this script works.
I do not know why the 3rd line is valid. The behavior produced is that the someFileName is run. I understand it as starting the file as a process. Then why isn't the start command needed? I don't see any batch documentation saying you can simply run a file by writing its pathed filename.
I do not understand the syntax of "%~someFileName". From online searching about it almost every source shows you the batch call parameter table, saying things like %~1 expands %1..., %~f1 expands %1 some other way, etc. All of them involve some kind of number from 0 to 9 to correspond to the parameter position. However, I cannot find any specification of %~someString being legal. There is no parameter positional information from the someFileName string, it is a filename.extension string. Still, it is quite likely this line is trying to run this format.
What does "/vv_pwd=%reqKey% /bProfile_something_schedule" mean? In the lib.cmd that was called previously, there were variables reqPath and reqKey and I am quite certain it is trying to pass the value of reqPath and reqKey from the lib.cmd into the variables here and then I guess it is trying to use the reqKey value as a parameter, which is a password required to run the file.
Inspecting the file, it contains some script of some paid software specific format, it only has variable name v_pwd inside but not vv_pwd. I do not know what the /bProfile_... is for. The part without the /b is exactly this batch file's name. But together with the /b I don't know what it means. The /v and /b look like some kind of options to me but I cannot see any specification explaining as there is no command beginning line 3 just some path. I guess the /min option refers to starting window minimized which is an option for the command start, yet there is no option of /v. The /B in start means to start application without creating window, which is quite unnecessary to have /min if you are not going to create a window in the first place. And it doesn't make sense to use /B directly followed by some string of Profile_something_Schedule.
FYI, the lib.cmd starts with call %*, which I would consider as trying to call all passed parameters and assuming those parameters are actually batch files that can be called.
Another thought I have is that the 2nd line call connects with the 3rd line so the 3rd line may not need a command. But I can't make sense of it. The someFileName is not of the Batch extension so I doubt it can be called as the call doc says it is for batch programs. If I want to run non-batch programs I need to use start right?
Would greatly appreciate your help!
The variable pathext contains a semicolon-separated list of executable filenames that may be appended as an extension to myexecutable. if the first string on a batch line is not a cmd internal command (like set, for etc.) then cmd tries to find myexecutable + each of the extensions in pathext in turn, first in the current directory, and then in each directory in the path (another semicolon-separated list of directories) and runs the first name found, or fails if none are found. That first string may also have an Associated extension, which then runs the application with which the extension is associated (like .txt runs notepad by default)
Neither do I, and I can't see that even knowing what the actual strings being executed by %reqPath% are would assist. See for /? from the prompt for more documentation on other ~ operators - or search SO for thousands of uses.
vv_pwd=%reqKey% : %reqKey% is replaced by the value of the variable reqKey evidently returned by the previous line. / is used in Windows to mean "here's a switch parameter for the executable", so evidently /vv_pwd=[the contents of reqKey], /bProfile_something_schedule and /min mean something to the executable %reqPath%. Quite what is anyone's guess.
The fact that lib.cmd's first line is call %* would mean that lib.cmd contains a library of routines. Since each call you have shown is of the form :string1 string2 then the resultant command executed would be call :string1 string2. call :string1 will call the routine contained within "lib.cmd" with the label string1: supplying string2 (and presumably optionally string2 string3... as parameters. Evidently, string2 is the name of the variable into which lib.cmd places the required data.
Without the :, string1 would be any executable that cmd can locate using the method in (1). It does not have to be a batch, but commonly is a batch.

Logic to identify errors logged

How can I check for a particular text logged to a file in Win 10 ? There is a SAS daemon that fails occasionally and at which point logs a message to a log file (C:\SAS\logs\daemon_error.log). The message is logged (with timestamp) only when the error occurs. I want to write a program (and schedule to run every 15 mins) which would check the occurrence of any error-message since the last error recognized. For e.g. if the last error was logged on July 15th the file would have looked like :
07-15-2018 13:21:42 : Incorrect parameters
Now if some error gets logged again today, it would look like below, at which point the script should trigger and identify it and send an email to a list of intended recipients. I presume there needs to be a mechanism to store the last timestamp when the error was recognized so that it only identify errors logged after the last one.
07-15-2018 13:21:42 : Incorrect parameters
09-10-2018 04:32:09 : Temp directory full
How can I accomplish this thru a batch file or perhaps any windows event(s). I do not have much knowledge on Win systems as I primarily worked on Unix. Can anyone please help ?
Thanks
When I understood you correctly, you want to check for timestamp only to know, if there are new errors since the last check, but don't necessarily need to know, which errors. For that, I suggest using the "Archive attribute" (Windows sets this attribute at each write access to the file)
#echo off
:loop
timeout 900
dir /aa "C:\SAS\logs\daemon_error.log" >nul 2>&1 || goto :loop
echo The file was changed!
attrib -a "C:\SAS\logs\daemon_error.log"
goto :loop
The dir command will not find the file when the Archive attribute is not set (/aa) (or the file doesn't exist) and || goto :loop will then jump to the :loop label.
>nul 2>&1 will just discard any output of the dir command - we don't need it, just the errorlevel)
If the file was found (the Archive attribute is set, so we know, there was a write access), the code continues. Replace the echo command with the command to write an email (there are several questions/answers about that on SO).
attrib -a unsets the Archive attribute.
Note: Instead of the loop, I would set up a scheduled task, but keep with the loop for troubleshooting until you're sure, it works as intended.

CMD file will not run my command in the else block

I'm writing a CMD script to execute a Java program. The program requires at least one argument so I created an if else block to check that argument one %1 is not blank. When I run the script without providing argument one I get the expected you must provide an argument to run. But when I do provide the argument the program does not execute. Additionally if I don't have #echo off set, the entire script prints out as text in the window when I do provide the proper argument.
Here's the full script:
set APP_HOME=C:\Temp\Hammer
rem Set APP_HOME to the place where you installed Hammer
if ["%1"] == [""] (
echo you must provide a python script to run
) else (
java -cp %APP_HOME%\lib\jython.jar;%APP_HOME%\lib\hammer.jar;%CLASSPATH% bridenstine.hammer.main.Main %1 %2
)
I think this is a problem with the script and not the program itself because when I run the line that's in the else block by itself without the script,
java -cp C:\Temp\Hammer\lib\jython.jar;C:\Temp\Hammer\lib\hammer.jar;%CLASSPATH% bridenstine.hammer.main.Main argument1
The program executes normally. I've been looking at example scripts and cross referencing this site for CMD files and what I have seems to be valid. Do I have a syntax error?
Update:
I'm running the script like so,
cd C:\Temp\Hammer
bin\ProgramScript.cmd argument1
Update 2:
Someone pointed out that when they run this script they get an error message saying Java is not recognized as an internal or external command (the expected message when Java is not set on their PATH) But they made a good point that this means the script is in fact getting inside the else block. I then pointed out the following,
After I run the script with a valid argument it prints out the entire script as text on the command prompt. I am then able to mark the line from inside the else statement (that was printed), copy it, paste it, and it runs the program fine. So the Java command seems to be valid. But you make a good point that the script is obviously getting inside the else block...something is still going wrong here and it doesn't seem to be the program.
Note:
If relevant I am using Windows 8.1 and I am using the standard command prompt, not one that has administrative privileges (the results remained the same regardless of using a command prompt with administrative privileges).
I suggest to use
set APP_HOME=C:\Temp\Hammer
rem Set APP_HOME to the place where you installed Hammer.
if "%~1"=="" (
echo You must provide a Python script to run.
pause
) else (
java.exe -cp "%APP_HOME%\lib\jython.jar;%APP_HOME%\lib\hammer.jar;%CLASSPATH%" bridenstine.hammer.main.Main %*
)
It is always better to enclose an argument string in double quotes if it contains environment variables like CLASSPATH which might have a string value containing 1 or more spaces.
%* is expanded by all arguments passed to the batch file as argument, i.e. %1 %2 ...
It is best to always specify an application like java with full path and file extension as otherwise Windows has to search for a file with a file extension as defined in environment variable PATHEXT in current working directory and all directories defined in environment variable PATH. At least the file extension should be in the batch file if the program files directory of the application varies.
I can only offer a suggestion; I've not tried this.
I would try escaping each ; with a caret ^ thus:
java -cp %APP_HOME%\lib\jython.jar^;%APP_HOME%\lib\hammer.jar^;%CLASSPATH% bridenstine.hammer.main.Main %1 %2
(But I'll admit it's clutching at straws...)
You are missing you she-bang at the top of the script
#!/bin/bash

Kicking off a script and moving on to start another

I'm trying to remotely deploy some software and I have a few batch scripts to help this process along. The basics of my process is to have a text list that scriptA goes through, scriptA copies the necessary files to the target PCs (retrieved from the list) and launches ScriptB. The theory would then be that it would move on to the next PC in the list as soon as ScriptB launches and doesn't wait for ScriptB to finish before moving on. The basics of what I have are below:
ScriptA:
Set /P List= Please enter the list name
Set /P Name= Please enter your username
FOR /F %%A IN (%LIST%) DO (
MKDIR \\%%A\C$\Temp\Install
XCOPY "\\share\necessary files" "\\%%A\C$\Temp\Install"
PSEXEC \\%%A -u DOMAIN\%NAME% -e "\\%%A\C$\Temp\Install\scriptB.bat"
)
ScriptB just runs the installer and cleans up after itself.
Like I said, the goal would be that once scriptA has finished copying over files and gets things kicked off, it would move on to the next PC on the list. I've confirmed that everything works as is right now (so please ignore any random things that wouldn't work in the pseudocode above) it just takes forever because it waits for the installation to finish before moving on.
EDIT:
I realized I had the wrong files shown as being copied. What was originally listed as 'install.bat' should have been scriptB. I apologize, it has been very hard to try to get my question across as I'm having a very hard time describing the problem.
Per Request, here is how I was using start:
ScriptA:
START PSECEC \\%%A -u DOMAIN\%NAME% -e "\\%%A\C$\Temp\Install\ScriptB.bat"
It worked to start ScriptB in a new window, but what I'm really trying to have as the final product is:
scriptA gets prompts for the list of PC names
scriptA pulls the first name from the list and copies over the install files from a network share, including scriptB and puts them in \temp\install
scriptA kicks off scriptB on the PC it just copied files to.
While scriptB is running, scriptA moves on and copies the files to the next PC on the list and kicks of the next copy of scriptB
repeat until the whole list of PCs is completed.
I hope this helps to clarify things, and again, I apologize for the confusion. I have all of the steps listed above working as I would hope, the only thing that isn't working is that I can't get scriptA to kick off scriptB without pausing scriptA to wait for scriptB to finish and what I would like to have happen is that it moves on without waiting (so that I could start this script at the end of the day and it would continue installing at night until it finished the list of PCs).
Thanks again for any help!
You can use the START command to launch a command without waiting for it to end before moving on. Note that this will execute the specified command in a newly created command prompt, not in the same command prompt process.
Be aware though that if the path to your executable/batch file contains spaces, you must specify an empty string argument as the first argument. Example:
START "" "C:\Path with spaces\My other script.bat"
This is due to the fact that if the first arguments is surrounded by quotes, it is considered as being the title of the new command prompt that is launched to execute your new process. Specifying an empty argument (or the title you'd like to use for that matter) will work around this situation.
The further we are away from the actual problem, the less able we are to help. The more general the question, the more vague the response.
We're pointlessly mapping from "ScriptB" to "Install.bat" and you've claimed to have added the start command but not shown us where you've used it.
Using your original script, changing
PSEXEC \\%%A -u DOMAIN\%NAME% -e "\\%%A\C$\Temp\Install.bat"
to
start "installing on %%A" PSEXEC \\%%A -u DOMAIN\%NAME% -e "\\%%A\C$\Temp\Install.bat"
should solve the problem - but no guarantees.

Batch - Execute a command IF program output critera is met

Sorry about the lousy title, I don't really know how to fit my question into a few words.
This may probably seem trivial to you guys, but I'm trying to automate a task that goes something like this:
Check if a certain executable exists
If it exists, run the executable, which has two possible outcomes:
Update found.
Update not found.
If outcome A, run a certain command. If outcome B, run another command.
The executable gives an output in the command line window, but I'm stranded on how to make the IF command read the output and use it.
English isn't my first language and my vocabulary regarding this is rather limited, so just let me know if something isn't clear.
if exist path\filename.ext anycommand checks, if a file exists and if yes, executes anycommand
anycommand >temp.txt writes the output of a command to a textfile.
set /p variable=<temp.txt writes the content of the textfile to a variable (well, at least the first line)
if "%variable%"=="Update not found" othercommand runs othercommand, if %variable% is equal to "Update not found"
Something along these lines might be what you're looking for: (This is an example, not the exact program)
set file_path=C:\Users\Atyai\Desktop\ExampleFile.bat
if exist %file_path% echo Update Found. && ::Then put your command right here
if not exist %file_path% echo Update Not Found. && ::Put your other command here
What this does, is when the file exists, it echoes "Update found" then I left some room for your second command. In the case that the file does not exist, it echoes "Update Not Found."
The file path I gave you (first line) needs to be replaced with your own.
This might provide some help:
http://ss64.com/nt/if.html

Resources