Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Echo does not return variables

103 views
Skip to first unread message

w1b0

unread,
Nov 25, 2011, 5:52:54 AM11/25/11
to
Dear all,

I have a weird issue for you.

Below you find find a script that I made, that is intended for use
during an upcoming Sharepoint Migration.

This is what it does: in c:\temp\users-to-migrate.txt there is a list
of users that exist on our internal LAN domain controller, and on an
domain controller outisde of the firewall. The thing is, is that the
naming convention for LAN differs from the one outside of the
firewall. So in the c:\temp\users-to-migrate.txt file are the CN's of
those users needed to migrate. With LDIFDE I actively query the AD's
of both environments.

I put the entire thing i a FOR DO-loop, as you can see.

The whole thing works quite nice, I thought LDIFDE was the tuff nutt
to crack, until I stumbled upon this: echo does not return my
variables. It's these lines that cause me haedaches:
FOR /F "tokens=*" %%G IN (c:\temp\extra1.txt) DO SET E3=%%G
FOR /F "tokens=*" %%H IN (c:\temp\dslan1.txt) DO SET DS=%%H

The main thing of the script is to generate 2 variables, 1 for old
user and 1 for new user, so that in the end, I can use the following
Sharpeoint command to migrate them:

stsadm -o migrateusr -oldlogin=%E3% -newlogin=%DS% -ignoresidhistory.
I need to migrate 800+ users, so to do it manually is definately not
an option.

I really have no clue what I am doing wrong.

Can you help me out? Any info is appreciated.

Cheers,

w1b0
####################################################################################################################################################
echo off
PROMPT=$P$G

echo --- > c:\temp\ldifde-output.txt
echo --- > c:\temp\stsadm-output.txt
echo --- > c:\temp\Extranet03-Users.txt
echo --- > c:\temp\DSLan-Users.txt

echo --==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--
==--==-- > c:\temp\complete-output.txt
FOR /F "tokens=* delims=" %%A IN (c:\temp\users-to-migrate.txt) DO (
cls

echo --==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--
==--==--==--
ECHO == Export for: %%A
ECHO.
echo %%A >> c:\temp\ldifde-output.txt

echo --==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--
==--==--==--
echo === Export vanuit EXTRANET03 Active Directory
ldifde -m -v -f c:\temp\EXTRAtemp.txt -s S3A-Sharepoint1 -b
it_ntadmin EXTRAnet03 Fl0ridakeys -d "CN=%
%A,OU=Internal,OU=Workplace,DC=bla,DC=bla,DC=com" -p subtree -r
"(&(objectCategory=person)(objectClass=*)(mail=*))" -l
"sAMAccountname" >> c:\temp\ldifde-output.txt

type c:\temp\EXTRAtemp.txt | FIND /I "sAMAccountName: " >> c:\temp
\complete-output.txt
type c:\temp\EXTRAtemp.txt | FIND /I "sAMAccountName: " >> c:\temp
\Extranet03-Users.txt
type c:\temp\EXTRAtemp.txt | FIND /I "sAMAccountName: " > c:\temp
\extra1.txt

echo === Export vanuit DSLAN Active Directory
ldifde -m -v -f c:\temp\DSLANtemp.txt -s KM-DC01 -b it_ntadmin
dutchspace.nl floridakeys -d "CN=%%A,OU=Normal
Users,OU=Users,DC=bla,DC=com" -p subtree -r "(&(objectCategory=person)
(objectClass=*)(mail=*))" -l "sAMAccountname" >> c:\temp\ldifde-
output.txt

type c:\temp\DSLANtemp.txt | FIND /I "sAMAccountName: " >> c:\temp
\complete-output.txt
type c:\temp\DSLANtemp.txt | FIND /I "sAMAccountName: " >> c:\temp
\DSLan-Users.txt
type c:\temp\DSLANtemp.txt | FIND /I "sAMAccountName: " > c:\temp
\dslan1.txt

echo --==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--
==--==--==--
echo == Postprocessing output
echo.
C:\temp\GR c:\temp\extra1.txt "sAMAccountName: " ""
C:\temp\GR c:\temp\dslan1.txt "sAMAccountName: " ""

echo --==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--
==--==--==--
echo == Call variables
echo.
echo stsadm voor %%A: oldlogin=%E3% newlogin=%DS%
FOR /F "tokens=*" %%G IN (c:\temp\extra1.txt) DO SET E3=%%G
FOR /F "tokens=*" %%H IN (c:\temp\dslan1.txt) DO SET DS=%%H
ECHO=%E3%
ECHO=%DS%

echo --==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--
==--==--==--
echo == run STSADM voor %%A
echo.
echo stsadm -o migrateuser oldlogin:%E3% newlogin:%DS%
echo stsadm voor %%A: oldlogin=%E3% newlogin=%DS% >> c:\temp
\stsadm-output.txt
echo --==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--
==--==--==-- >> c:\temp\complete-output.txt
echo --==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--
==--==--==-- >> c:\temp\ldifde-output.txt
echo --==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--
==--==--==--==--==--==--==--==-- >> c:\temp\stsadm-output.txt
pause
)

del /Q c:\temp\EXTRAtemp.txt
del /Q c:\temp\DSLANtemp.txt
del /Q c:\temp\extra1.txt
del /Q c:\temp\dslan1.txt

billious

unread,
Nov 25, 2011, 1:20:51 PM11/25/11
to

"w1b0" <wib...@gmail.com> wrote in message
news:7d831acf-2a6d-439b...@w1g2000vba.googlegroups.com...
Common "delayed expansion" problem.

The ENTIRE statement between
FOR /F "tokens=* delims=" %%A IN (c:\temp\users-to-migrate.txt) DO (

and the matching close-parenthesis ")" is parsed when the FOR is
encountered. At that time (parse time) any %variable% is replaced by the
value of "variable" at THAT time - before the loop is executed. Hence, since
E3 is undefined at the time, it is replaced by [nothing] and THEN the
procedure is executed, so it appears to 'disappear.'

Two solutions:

Solution 1:

Add a line

SETLOCAL ENABLEDELAYEDEXPANSION

before the FOR statement (commonly directly after the @echo off)

AND

replace %var% with !var! to reference the CURRENT value of var rather than
the PARSE-TIME value.

(See SETLOCAL /? from the prompt for documentation)

Solution 2:

Use an internal subroutine.

FOR .... DO call :main %%A
...
GOTO :EOF

:main
:: within this routine, %1 refers to the first parameter
:: provided by the CALL, not the first parameter to the batch
:: therefore, replace %%A with %1
...
GOTO :EOF

Where :main means 'the internal subroutine "main" ' and GOTO :EOF means
go-to-logical-end-of-file which terminates the subroutine. Note the colon is
required and the label ":EOF" is understood and should NOT be declared in
your batch.

(See CALL /? from the prompt for documentation)

Both solutions have side-effects that will probably not be significant in
this instance. The first causes "!" to be a special character in a similar
way as "%" is a special character in batch. The second has difficulties
should the parameter contain separator characters like spaces, semicolons or
commas.

For many, many examples and solutions, see newsgroup alt.msdos.batch.nt


0 new messages