WMI Process Call Create will not Run Batch script properately

13,979

First problem, when you wmic process call create you should use prefix your command with cmd /c.

Next, you're right. wmic doesn't display the resulting output of the remotely created process on your local console. You'll either need to use psexec which was designed for this sort of thing, or hack a workaround by piping the command output to a log file then reading the log file. Something like the following script.

I'm not really clear, if this is going to be a scheduled task, why you're concerned with results being available to stdout. I suspect you intend to redirect the output to a log of some sort. So I put that in here as well.

@echo off
setlocal

set "user=domainadmin"
set "pass=password"

for /f %%I in ('wmic os get localdatetime') do set "timestamp=%%I"
set "today=%timestamp:~0,8%"

set logfile=c:\users\me\Desktop\logs\%today%.log

if not exist "%logfile%" mkdir "%logfile%\.." 2>NUL
>>"%logfile%" echo %time%

for %%I in (NODE1 NODE2) do (
    (ping -n 1 %%I >NUL && (
        net use \\%%I /user:%user% %pass% >NUL 2>NUL
        wmic /node:%%I /user:%user% /password:%pass% process call create "cmd /c c:\clustercheck.bat >c:\cc.log"
        type \\%%I\c$\cc.log && del \\%%I\c$\cc.log
        net use \\%%I /delete >NUL 2>NUL
    ) || echo %%I unresponsive
)>>"%logfile%"

forfiles /p "%logfile%\.." /M *.log  /d -30 /c "cmd /c del @path"

This should create Desktop\logs if it doesn't exist, then create or append to Desktop\logs\YYYYMMDD.log the output of C:\clustercheck.bat run on NODE1 and NODE2. Finally, it deletes log files that are over 30 days old.

Share:
13,979
gmilic
Author by

gmilic

Updated on June 04, 2022

Comments

  • gmilic
    gmilic almost 2 years

    This is what i am trying to do: I have a NLB Cluster. There are two machines on said cluster: Node1 and Node2. I have a third machine that is not in that, or any, cluster. This third machine is called: Monitor1

    Once every hour, i would like to run a script to check if Node1 and Node2 are up. This script will be run via TaskScheduler. I am using the following command to execute the script on Node1 and Node2:

    wmic /node:NODE1,NODE2 process call create "C:\ClusterCheck.bat"
    

    The contents of the ClusterCheck.bat script is as follows:

    NLB Query | findstr /i /R /C:"host . is stopped"
    IF %ERRORLEVEL% EQU 0 (ECHO %COMPUTERNAME%_down)>DOWN.txt
    IF %ERRORLEVEL% EQU 1 (ECHO %COMPUTERNAME%_up)>UP.txt
    code here
    

    When I use wmic /node:"%1" process call create "C:\ClusterCheck.bat" there is not output. When I go into the server and manually double click the ClusterCheck.bat file, it gives me the appropriate output depending whether the node is up or down.

    Does anyone have any ideas how I can get those files to output?

  • rojo
    rojo about 11 years
    Actually, now that I think about this, all the backslashes might need to be escaped with another backslash, like \\\\%COMPUTERNAME%\\etc. I'll have to test this in the morning.
  • gmilic
    gmilic about 11 years
    PSExec will not work properly with Task Scheduler. I have spent the better part of yesterday trying to make it work with no luck.
  • gmilic
    gmilic about 11 years
    what is %CD::=$% supposed to mean?
  • rojo
    rojo about 11 years
    %CD::=$% is the same as %CD% which is the current working directory. The stuff in the middle substitutes colons for dollar signs. So where "%CD%" == "C:\Users" you have "%CD::=$%" == "C$\Users".
  • gmilic
    gmilic about 11 years
    The above does not work. This is what i have typed in: wmic /node:<Node1> process call create "cmd /c C:\ClusterCheck.bat >> \\\\NetworkShare\\cc.log" >>type cc.log
  • rojo
    rojo about 11 years
    Yeah, I know. I got the information from here, but I can't make it work either. Logging the remote command to the remote machine then reading and deleting that log works, though. I'm updating my answer now.
  • gmilic
    gmilic about 11 years
    I know of Command Line Kung Foo and Ed Skoudis. I have already read that site. :P
  • rojo
    rojo about 11 years
    @gmilic - is that what you needed?
  • gmilic
    gmilic about 11 years
    The following line produced the output that i needed: wmic /node:%%I /user:%user% /password:%pass% process call create "cmd /c c:\clustercheck.bat >c:\cc.log". The output was placed in the directory of the remote server. This is what i wanted. Thank you!