-1

I am installing multiple services (using dsmcutil) from CMD batch file in windows 2012 however while dsmcutil command lien is correct variables created in for /f do not work properly. I guess it is a similar behavior known as globing

I tried syntax as follow (dsmcutil command is tested and correct, )

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
set cpath=C:\Program Files\tivoli\TSM\baclient\cifs
set dpath=C:\Program Files\tivoli\TSM\baclient
set dsmcutil=%dpath%\dsmcutil
set User=service_user
set UserPa=sjkdfIHO127346JHG
set pass=xxxkkk123
set ntdo=polka.net
for /F "usebackq tokens=*" {%A} IN "%cpath%\nodes.lst" do (
set mopt=%cpath%\%A\%A-dsm.opt
%dsmcutil% install sched /name:"TSM Client Scheduler %A" /node:%A-m /password:%pass% /eventlogging:no /optfile:"%mopt%" /schedlog:"%cpath%\%A\DSMSched_%A.log" /errorlog:"%cpath%\%A\DSMError_%A.log" /startnow:yes /autostart:yes /ntdomain:%ntdo% /ntaccount:%User% /ntpassword:%UserPa%
)

the error I get is "cpath\nodes.lst" do ( was unexpected at this time."

The question is what is proper syntax working on windows 2012 r2? ..... if You call the variable from "for" in the syntax normally it was %A% which gives the same error as %A only. Moreover (for /F "usebackq tokens=*" %A ) has to be used and not %%A as in previous w2k versions.

syntax normally should endup getting : 1. path/node_name (from for loop and txt file = nodes.lst) 2. pointing to right path\dsm.opt file 3. executing dsmutil installing service with the right name.

as it is it never gets over point 1.

frodo
  • 1
  • 3
  • 1
    Did you guess the syntax? That won't work. Stick to the [correct syntax](https://ss64.com/nt/for_f.html). The syntax never changed and is the same in all Windows versions (at least XP onward) – Stephan Sep 06 '19 at 13:37
  • 1
    oh - and of course the variable `mopt` has to be [delayed](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028). – Stephan Sep 06 '19 at 13:41

1 Answers1

1

The biggest two errors with your example are that you have not checked the output from for /? at the Command Prompt, and require delayed expansion, for variables created and used within the same code block. An additional important note is that you should not generally create variables with names that already exist within the system, in this case %dpath%.

Here's a basic example showing the correct syntax, as per your question. Please note that I have not checked your dsmcutil line as that is outside of the question scope.

@Echo Off
SetLocal DisableDelayedExpansion
Set "cpath=%ProgramFiles%\tivoli\TSM\baclient\cifs"
Set "d_path=%ProgramFiles%\tivoli\TSM\baclient"
Set "dsmcutil=%d_path%\dsmcutil"
Set "User=service_user"
Set "UserPa=sjkdfIHO127346JHG"
Set "pass=xxxkkk123"
Set "ntdo=polka.net"

For /F "UseBackQ Tokens=*" %%A In ("%cpath%\nodes.lst") Do (
    Set "mopt=%cpath%\%%A\%%A-dsm.opt"
    SetLocal EnableDelayedExpansion
    "%dsmcutil%" install sched /name:"TSM Client Scheduler %%A" /node:"%%A-m" /password:"%pass%" /eventlogging:"no" /optfile:"!mopt!" /schedlog:"%cpath%\%%A\DSMSched_%%A.log" /errorlog:"%cpath%\%%A\DSMError_%%A.log" /startnow:"yes" /autostart:"yes" /ntdomain:"%ntdo%" /ntaccount:"%User%" /ntpassword:"%UserPa%"
    EndLocal
)
Compo
  • 36,585
  • 5
  • 27
  • 39