Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

store SID in a variable

Tags:

batch-file

cmd

I need a way to store the current user's SID in a variable, I tried a lot of variants of:

setlocal enableextensions 
for /f "tokens=*" %%a in ( 
'"wmic path win32_useraccount where name='%UserName%' get sid"'
) do ( 
if not "%%a"==""
set myvar=%%a
echo/%%myvar%%=%myvar% 
pause 
endlocal 

None are working.

wmic path win32_useraccount where name='%UserName%' get sid should be returning 3 lines, and I need the second one stored in a variable.

Can someone fix my script?

Edit: I am using a .cmd file.

like image 454
user361191 Avatar asked Jun 08 '10 09:06

user361191


1 Answers

This should fix it:

for /f "delims= " %%a in ('"wmic path win32_useraccount where name='%UserName%' get sid"') do (
   if not "%%a"=="SID" (          
      set myvar=%%a
      goto :loop_end
   )   
)

:loop_end
echo %%myvar%%=%myvar%

note the "delims= " in the FOR loop. It will separate the input at spaces, that are contained at the end of the output ouf your WMI query.

The condition if not "%%a"=="SID" will be true for the second iteration and then assign the variable and break out of the loop.

Hope that helps.

like image 126
Frank Bollack Avatar answered Oct 11 '22 12:10

Frank Bollack