user2381130
user2381130

Reputation: 193

Batch script, call cmd on servers

I am trying to call x.cmd from many servers in a list and basically x.cmd will return a log file so I will copy the output log files into a new folder. the structure in that list is: server name&folder path of the x.cmd on thet server&output folder name that I will create

when I run the script below, it doesn't return any error and return value is 0. but x.cmd just doesn't do anything when I check on the actual server. x.cmd working fine once I run it manually.

Please if possible point me out the mistake of my script. my other concern is the folder path being too long(but it is within the limits of Microsoft if I am right). :) Thanks for your time.

@echo on

cd /d %~dp0

setlocal EnableDelayedExpansion
set serverList=List.txt

for /f "usebackq tokens=1,2,3* delims=&" %%A in ("%serverList%") DO (

        set remoteCmd=x.cmd
        wmic /node:%%A process call create "%%B\remoteCmd"
        pause
        robocopy %%B\logs Output\%%C /e
)
endlocal
Pause 

JS

Upvotes: 2

Views: 1994

Answers (1)

zb226
zb226

Reputation: 10500

The problem seems to be that you're not actually calling x.cmd on the remote servers, since the variable isn't substituted in the wmic call. Move the definition of remoteCmd out of the loop and enclose remoteCmd in the wmic call in percent signs:

set serverList=List.txt
set remoteCmd=x.cmd    
for /f "usebackq tokens=1,2,3* delims=&" %%A in ("%serverList%") DO (
        wmic /node:%%A process call create "%%B\%remoteCmd%"
        pause
        robocopy %%B\logs Output\%%C /e
)

Update 1: Since you're getting a response on the shell with a processID and Error level=0, it's fairly safe to say that x.cmd was in fact started on the remote server. The fact that the logfile does not turn up in the same directory where x.cmd resides in, is very probably because the logfile is not given an absolute path in x.cmd. That means it will be created in the directory on the remote machine where the cmd.exe is located, which is executing x.cmd. In most systems this will be %WINDIR%\system32, i.e. c:\windows\system32.

Update 2: If modifying x.cmd is no option, then you need to change the path to it's directory before calling it. You can do it like this:

wmic /node:%%A process call create "cmd /c cd /d %%B & x.cmd"

Note: The cd /d allows you to change directories even across different drives.

Update 3: Since there are spaces in the directory names you'll need to enclose them with quotes - which will have to be escaped, since there already outer quotes enclosing the whole remote command. So that means:

wmic /node:%%A process call create "cmd /c \"cd /d %%B\" & x.cmd"

Note: A possible alternative to the inner quotes would be to use 8.3-filenames of the involved directories. You can view those with dir /x on the command line.

Upvotes: 1

Related Questions