DougInCanada
Technical User
I am trying to create a vbscript to perform the following:
- Get the current user's logon name
- Determine if that user is a member of the local administrator group
- delete that username from the local administrator group.
This is needed because we have over 100 field users who have laptops, but do not have computer accounts in AD, so the script must be downloaded via our intranet and run locally.
Here's what I have so far:
Set Sh = WScript.CreateObject("Wscript.Shell)
' For non-networked users
Set Env = Sh.Environment("SYSTEM"
' For networked users
If Env("USERNAME"
="" then
Set Net = WScript.CreateObject("Wscript.Network"
CurrentUser = Net.UserName
Else
CurrentUser = Env("USERNAME"
End If
' If the current user is the Local Administrator
If CurrentUser = "Administrator" then
Wscript.Echo "You are a local Administrator!"
' Can't remove administrator from the Built-in Group
Else
strComputer = "."
' Designate the local computer
Set colGroups = GetObject(WinNT://" & strComputer & ""
colGroups.Filter = Array("group"
For Each objGroup in colGroups
For Each objUser in objGroups.Members
If objUser.Name = CurrentUser then
' The script works great up to this point...
If objGroup.Name = "Administrators" then
Set objGroup = GetObject("WinNT://" & strComputer & "Administrators, Group"
Set objUser = GetObject("WinNT://" & strComputer & "/" & CurrentUser & ", user"
objGroup.Remove(objUser.ADsPath)
Wscript.Echo "You have been removed from the Local Administrators Group!"
End If
End If
Next
Next
End if
When I try the script, it dies on objGroup.Remove(objUser.ADsPath)...
I’ve seen many references to ‘Remove’ as the most common method of removing a user from a group but I can’t get it to work. Can someone help me with this ?
- Get the current user's logon name
- Determine if that user is a member of the local administrator group
- delete that username from the local administrator group.
This is needed because we have over 100 field users who have laptops, but do not have computer accounts in AD, so the script must be downloaded via our intranet and run locally.
Here's what I have so far:
Set Sh = WScript.CreateObject("Wscript.Shell)
' For non-networked users
Set Env = Sh.Environment("SYSTEM"
' For networked users
If Env("USERNAME"
Set Net = WScript.CreateObject("Wscript.Network"
CurrentUser = Net.UserName
Else
CurrentUser = Env("USERNAME"
End If
' If the current user is the Local Administrator
If CurrentUser = "Administrator" then
Wscript.Echo "You are a local Administrator!"
' Can't remove administrator from the Built-in Group
Else
strComputer = "."
' Designate the local computer
Set colGroups = GetObject(WinNT://" & strComputer & ""
colGroups.Filter = Array("group"
For Each objGroup in colGroups
For Each objUser in objGroups.Members
If objUser.Name = CurrentUser then
' The script works great up to this point...
If objGroup.Name = "Administrators" then
Set objGroup = GetObject("WinNT://" & strComputer & "Administrators, Group"
Set objUser = GetObject("WinNT://" & strComputer & "/" & CurrentUser & ", user"
objGroup.Remove(objUser.ADsPath)
Wscript.Echo "You have been removed from the Local Administrators Group!"
End If
End If
Next
Next
End if
When I try the script, it dies on objGroup.Remove(objUser.ADsPath)...
I’ve seen many references to ‘Remove’ as the most common method of removing a user from a group but I can’t get it to work. Can someone help me with this ?