Re: logon script, username and registry edit by Kendoh
Kendoh
Mon Sep 12 16:29:01 CDT 2005
This will work for any domain...
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
strKeyPath = "SOFTWARE\Logged on User"
strValueName = "User Name"
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:stdRegProv")
objReg.CreateKey HKEY_LOCAL_MACHINE, strKeyPath
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")
For Each objComputer in colComputer
strValue = objComputer.UserName
stringlength = len(strValue)
charnumber = InStrRev(strValue, "\")
strValue = right(strValue,stringlength-charnumber)
objReg.SetStringValue HKEY_LOCAL_MACHINE, strKeyPath, StrValueName, strValue
Next
"Greg Herzog" wrote:
> This is a quick and dirty way
>
> strValueName = "DOMAIN\User Name"
> strValueName = right(StrValueName,len(StrValueName)-7)
>
> I set it to 7 because DOMAIN\ has 7 characters. Adjust for the length of
> your domain name
>
> Also, if you have more than one domain, you should search for the "\" each
> time and do a dynamic calculation.
>
>
>
> "Mark R." <MarkR@discussions.microsoft.com> wrote in message
> news:CB50B41D-AE2D-4F28-AA1C-54D280339299@microsoft.com...
> > how best to "parse out" the domain portion?
> >
> > "Kendoh" wrote:
> >
> >> the following will create a Reg string value in a key called "Logged in
> >> User"
> >> under software HKLM. You can modify those portions as needed. A quick
> >> note,
> >> the logged in user will be stored as "domain\username". You can parse out
> >> the
> >> domain\ if you wanted to.
> >>
> >> Const HKEY_LOCAL_MACHINE = &H80000002
> >>
> >> strComputer = "."
> >> strKeyPath = "SOFTWARE\Logged on User"
> >> strValueName = "User Name"
> >>
> >> Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
> >> strComputer & "\root\default:stdRegProv")
> >>
> >> Set objWMIService = GetObject("winmgmts:" _
> >> & "{impersonationLevel=impersonate}!\\" _
> >> & strComputer & "\root\cimv2")
> >>
> >> Set colComputer = objWMIService.ExecQuery _
> >> ("Select * from Win32_ComputerSystem")
> >>
> >> For Each objComputer in colComputer
> >> strValue = objComputer.UserName
> >>
> >> objReg.SetStringValue HKEY_LOCAL_MACHINE, strKeyPath, StrValueName,
> >> strValue
> >> Next
> >>
> >>
> >> "Mark R." wrote:
> >>
> >> > I'm trying to modify a registry key in HKLM to receive the value of the
> >> > currently logged on user (%username%). I need this to run as part of
> >> > the
> >> > normal user logon script...whether it is embedded in the current script
> >> > or
> >> > called separately. My logon script is simply a *.bat file that
> >> > synchronizes
> >> > the time, maps some drives and copies files to the local machine. I'm
> >> > not a
> >> > scripter so I tried Scriptomatic and Tweakomatic with similar
> >> > (negative)
> >> > results. The key in question is a string value. I'm running a W2K3
> >> > domain if
> >> > this matters. Thanks!
>
>
>