Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


December 22, 2006

How can I set an environment variable to a user's domain SID, using standard commands?

RSS
Subscribe to Windows IT Pro | See More Domains Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

11126 » How can I set an environment variable to a user's domain SID, using standard commands? (22-Dec-06)

I have scripted GetUserSID.bat to return the current user's domain SID.

The syntax for using GetUserSID.bat is:

[call GetUserSID SID

Where SID is a call directed environment variable that will contain the user's domain SID.

GetUserSID.bat contains:

@echo off
if {%1}=={} @echo Syntax: GetUserSID SID&goto :EOF
if exist "%TEMP%\GetUserSID.VBS" goto getit
@echo.Set WshShell = Wscript.CreateObject("Wscript.Shell") >"%TEMP%\GetUserSID.VBS"
@echo.Set oADinfo = createobject("ADSystemInfo") >>"%TEMP%\GetUserSID.VBS"
@echo.strUserAdPath = oADinfo.userName >>"%TEMP%\GetUserSID.VBS"
@echo.Set objADObject = GetObject("LDAP://" ^& strUserAdPath) >>"%TEMP%\GetUserSID.VBS"
@echo.tempsid = objADobject.objectsid >>"%TEMP%\GetUserSID.VBS"
@echo.hexsid = OctetToHexStr(tempsid) >>"%TEMP%\GetUserSID.VBS"
@echo.decsid = HexStrToDecStr(hexsid) >>"%TEMP%\GetUserSID.VBS"
@echo.Wscript.Echo decsid >>"%TEMP%\GetUserSID.VBS"
@echo.Wscript.Quit >>"%TEMP%\GetUserSID.VBS"
@echo.Function OctetToHexStr(arrbytOctet) >>"%TEMP%\GetUserSID.VBS"
@echo.  Dim k >>"%TEMP%\GetUserSID.VBS"
@echo.  OctetToHexStr = "" >>"%TEMP%\GetUserSID.VBS"
@echo.  For k = 1 To Lenb(arrbytOctet) >>"%TEMP%\GetUserSID.VBS"
@echo.    OctetToHexStr = OctetToHexStr _ >>"%TEMP%\GetUserSID.VBS"
@echo.      ^& Right("0" ^& Hex(Ascb(Midb(arrbytOctet, k, 1))), 2) >>"%TEMP%\GetUserSID.VBS"
@echo.  Next >>"%TEMP%\GetUserSID.VBS"
@echo.End Function >>"%TEMP%\GetUserSID.VBS"
@echo.Function HexStrToDecStr(strSid) >>"%TEMP%\GetUserSID.VBS"
@echo.  Dim arrbytSid, lngTemp, j >>"%TEMP%\GetUserSID.VBS"
@echo.  ReDim arrbytSid(Len(strSid)/2 - 1) >>"%TEMP%\GetUserSID.VBS"
@echo.  For j = 0 To UBound(arrbytSid) >>"%TEMP%\GetUserSID.VBS"
@echo.    arrbytSid(j) = CInt("&H" ^& Mid(strSid, 2*j + 1, 2)) >>"%TEMP%\GetUserSID.VBS"
@echo.  Next >>"%TEMP%\GetUserSID.VBS"
@echo.  HexStrToDecStr = "S-" ^& arrbytSid(0) ^& "-" _ >>"%TEMP%\GetUserSID.VBS"
@echo.    ^& arrbytSid(1) ^& "-" ^& arrbytSid(8) >>"%TEMP%\GetUserSID.VBS"
@echo.  lngTemp = arrbytSid(15) >>"%TEMP%\GetUserSID.VBS"
@echo.  lngTemp = lngTemp * 256 + arrbytSid(14) >>"%TEMP%\GetUserSID.VBS"
@echo.  lngTemp = lngTemp * 256 + arrbytSid(13) >>"%TEMP%\GetUserSID.VBS"
@echo.  lngTemp = lngTemp * 256 + arrbytSid(12) >>"%TEMP%\GetUserSID.VBS"
@echo.  HexStrToDecStr = HexStrToDecStr ^& "-" ^& CStr(lngTemp) >>"%TEMP%\GetUserSID.VBS"
@echo.  lngTemp = arrbytSid(19) >>"%TEMP%\GetUserSID.VBS"
@echo.  lngTemp = lngTemp * 256 + arrbytSid(18) >>"%TEMP%\GetUserSID.VBS"
@echo.  lngTemp = lngTemp * 256 + arrbytSid(17) >>"%TEMP%\GetUserSID.VBS"
@echo.  lngTemp = lngTemp * 256 + arrbytSid(16) >>"%TEMP%\GetUserSID.VBS"
@echo.  HexStrToDecStr = HexStrToDecStr ^& "-" ^& CStr(lngTemp) >>"%TEMP%\GetUserSID.VBS"
@echo.  lngTemp = arrbytSid(23) >>"%TEMP%\GetUserSID.VBS"
@echo.  lngTemp = lngTemp * 256 + arrbytSid(22) >>"%TEMP%\GetUserSID.VBS"
@echo.  lngTemp = lngTemp * 256 + arrbytSid(21) >>"%TEMP%\GetUserSID.VBS"
@echo.  lngTemp = lngTemp * 256 + arrbytSid(20) >>"%TEMP%\GetUserSID.VBS"
@echo.  HexStrToDecStr = HexStrToDecStr ^& "-" ^& CStr(lngTemp) >>"%TEMP%\GetUserSID.VBS"
@echo.  lngTemp = arrbytSid(25) >>"%TEMP%\GetUserSID.VBS"
@echo.  lngTemp = lngTemp * 256 + arrbytSid(24) >>"%TEMP%\GetUserSID.VBS"
@echo.  HexStrToDecStr = HexStrToDecStr ^& "-" ^& CStr(lngTemp) >>"%TEMP%\GetUserSID.VBS"
@echo.End Function >>"%TEMP%\GetUserSID.VBS"
:getit
set %1=NONE
for /f "Tokens=*" %%a in ('cscript //nologo "%TEMP%\GetUserSID.VBS"') do (
 set %1=%%a
)


End of Article



Reader Comments

You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
Microsoft: Save Money ... By Paying for Software

Microsoft this week adopted an interesting tactic in its long-running battle with open source software: Businesses looking to save money over the long haul should simply pay for software instead of moving to free, open source solutions. The rationale? ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

Microsoft Delivers Service Pack 2 Beta 2 for Vista, Server 2008

Microsoft on Tuesday announced the availability of the Beta 2 version of Service Pack 2 (SP2) for Windows Vista and Windows Server 2008. Since both operating systems were developed from the same code base, they have a common servicing structure and thus ...


Related Events Check out our list of Free Email Newsletters!

Related Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing