Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detecting logged on user then adding to local admin group

Status
Not open for further replies.

Asher01

MIS
Oct 10, 2012
3
0
0
CA
vbscript.
-----------------------
Set wshShell = WScript.CreateObject("WScript.Shell")
strUserName = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )
WScript.Echo "User Name: " & strUserName

'the above script detects User logged on to the computer and it works fine, The botom script gets the user "Test" which is already in local users group and adds it to the local admin group.

what I would like to do is joing both scripts so I detect the logged on user in the above script then fetch or put it it into the "StrUser" so it is used be added to the local admin group. in windows 7.

strUser = "Test"

Set objWshNet = CreateObject("WScript.Network")
strDomain = objWshNet.UserDomain
strComputer = objWshNet.ComputerName
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators,group")
Set objUser = GetObject("WinNT://" & strDomain & "/" & strUser & ",user")
If Not objGroup.IsMember(objUser.ADsPath) Then
objGroup.Add(objUser.ADsPath)
End If


I did this but, it did not work.

Set wshShell = WScript.CreateObject("WScript.Shell")
strUserName = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )
' WScript.Echo "User Name: " & strUserName

strUser = strUserName

Set objWshNet = CreateObject("WScript.Network")
strDomain = objWshNet.UserDomain
strComputer = objWshNet.ComputerName
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators,group")
Set objUser = GetObject("WinNT://" & strDomain & "/" & strUser & ",user")
If Not objGroup.IsMember(objUser.ADsPath) Then
objGroup.Add(objUser.ADsPath)
End If

Many thanks

 
Firstly, at which line does it fail?

Add some echoes just to sanity-check the data:
Code:
Set wshShell = WScript.CreateObject("WScript.Shell")
strUserName = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )
' WScript.Echo "User Name: " & strUserName

strUser = strUserName

Set objWshNet = CreateObject("WScript.Network")
strDomain = objWshNet.UserDomain
     wscript.echo "strDomain = '" & strDomain & "'"
strComputer = objWshNet.ComputerName
     wscript.echo "strComputer = '" & strComputer & "'"
     wscript.echo "Set objGroup = GetObject(""WinNT://" & strComputer & "/Administrators,group" & ")"
     wscript.echo "Set objUser = GetObject(""WinNT://" & strDomain & "/" & strUser & ",user" & ")"
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators,group")
Set objUser = GetObject("WinNT://" & strDomain & "/" & strUser & ",user")
If Not objGroup.IsMember(objUser.ADsPath) Then
	objGroup.Add(objUser.ADsPath)
End If

I'm not sure the "'user" and "'group" arguments are required. According to this article you should only need:
Code:
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators")
Set objUser = GetObject("WinNT://fabrikam/kenmyer")

This version of your script works for me:
Code:
Set wshShell = WScript.CreateObject("WScript.Shell")
Set objWshNet = CreateObject("WScript.Network")

strUserName = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )
	WScript.Echo "User Name: '" & strUserName & "'"
strDomain = objWshNet.UserDomain
	wscript.echo "strDomain = '" & strDomain & "'"
strComputer = objWshNet.ComputerName
	wscript.echo "strComputer = '" & strComputer & "'"

	wscript.echo "Set objGroup = GetObject(""WinNT://" & strComputer & "/Administrators" & ")"
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators")

	wscript.echo "Set objUser = GetObject(""WinNT://" & strDomain & "/" & strUserName & ")"
Set objUser = GetObject("WinNT://" & strDomain & "/" & strUserName)

If Not objGroup.IsMember(objUser.ADsPath) Then
	wscript.echo "objGroup.Add(objUser.ADsPath)"
	objGroup.Add(objUser.ADsPath)
End If

Slightly different order of things, some wscript.echo commands here and there, no "strUser = strUserName" (because, what was the point?!).

Lastly, this is much easier to achieve with Group Policy (although less fun, I'll grant you that)...
Code:
Computer Configuration -> Preferences -> Control Panel Settings -> Local Users and Groups -> Group (Name: Administrators (built-in)) -> Local Group -> Add members

[smile]

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, or photo, or breakfast...and so on)[/small]
 
Hi,

Many thanks for the answer. I found another script that does the same thing using WMI. I required to do this via config mgr,(SCCM). The above only works with local admin when you are logged on.

Many thanks again.

Regards,
Asher.
 
Frankly, I wouldn't get the Username from the shell's environment strings. I'd get it from the WScript.Network object

strUser = objWshNet.UserName
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top