Premkumar Yogeswaran
asked on
Add Users to Remote machine local groups
Hi,
I found a script from online to add users to remote local groups like Administrator and Remote Desktop groups.
But in this script i can able to query, but i am unable to add users to the group.
Error: User doesn't exist in Domain.
Please could you check and help me to fix this script.
Thanks,
Prem
I found a script from online to add users to remote local groups like Administrator and Remote Desktop groups.
But in this script i can able to query, but i am unable to add users to the group.
Error: User doesn't exist in Domain.
Please could you check and help me to fix this script.
Thanks,
Prem
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Remote Local Group Manager</title>
<HTA:APPLICATION
applicationName = "Remote Local Group Manager"
singleInstance = "yes"
showInTaskbar = "yes"
border = "thin"
scroll = "no"
maximizeButton = "no"
contextMenu = "no"
version = "2.1"
>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style>
body{
background-color: buttonface;
}
p{
text-align: right;
}
button{
clear: both;
}
#left{
float: left;
width: 200px;
}
#right{
float: right;
width: 225px;
}
</style>
<SCRIPT LANGUAGE="vbScript">
OPTION EXPLICIT
Const DOMAIN = "[Your domain]"
Const READ_ONLY = 1
Const FOR_WRITING = 2
Const FOR_APPENDING = 8
Const OUTPUT_FILE = "Remote Local Group Manager Results.log"
Sub Window_OnLoad()
Call CenterWindow(500, 395)
Call GetLocalGroups()
pcList.Focus()
End Sub 'End Window_OnLoad()
Sub CenterWindow(intWidth, intHeight)
self.ResizeTo intWidth, intHeight
self.MoveTo (screen.Width - intWidth)/2, (screen.Height - intHeight)/2
End Sub 'End CenterWindow(intWidth, intHeight)
Sub GetLocalGroups()
Dim objGroup
Dim colGroups : Set colGroups = GetObject("WinNT://.")
colGroups.Filter = Array("group")
For Each objGroup in colGroups
Dim objOption : Set objOption = Document.CreateElement("option")
objOption.value = objGroup.Name
objOption.Text = objGroup.Name
sel_Groups.Add(objOption)
Next
Set objGroup = Nothing
Set colGroups = Nothing
Set objOption = Nothing
End Sub 'End GetLocalGroups()
Sub Execute_OnClick()
Dim strComputer, blnLog, objTask, intRtnCode, strAction
Dim arrComputers : arrComputers = Split(pcList.Value, vbCrLf)
If UBound(arrComputers) < 0 Then
Msgbox "At least one remote PC required!", vbApplicationModal & vbExclamation, "Remote PC's - Error"
pcList.Focus()
ElseIf LEN(txt_User.Value) < 4 Then
Msgbox "Invalid User ID!", vbApplicationModal & vbExclamation, "User ID - Error"
txt_User.Focus()
Else
Dim strUser : strUser = UCase(txt_User.Value)
Dim strGroup : strGroup = sel_Groups.Value
If chk_log.checked Then
blnLog = True
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objOutput : Set objOutput = objFSO.OpenTextFile(OUTPUT_FILE, FOR_WRITING, True)
objOutput.WriteLine Now
Else
blnLog = False
End If
Dim arrTasks : Set arrTasks = Document.GetElementsByName("rad_shutdownOpt")
For Each strComputer In arrComputers
strComputer = UCASE(TRIM(strComputer))
Dim blnExecuteNext : blnExecuteNext = ValidatePC(strComputer)
If blnExecuteNext = True Then
For each objTask In arrTasks
If objTask.Checked Then
Select Case objTask.id
Case "add"
intRtnCode = AddUser(strComputer, strUser)
strAction = " added " & strUser & " to " & strGroup
Case "delete"
intRtnCode = DeleteUser(strComputer, strUser)
strAction = " removed " & strUser & " from " & strGroup
Case "query"
Call QueryGroup(strComputer, strGroup, objOutput)
Case Else
Msgbox "There has been a fatal error and the application needs to close.", vbApplicationModal & vbCritical, "User Options - Fatal Error"
self.Close()
End Select
End If
Next
Else
If blnLog = True Then
objOutput.WriteLine "Failed to connect\access " & strComputer
objOutput.WriteBlankLines(2)
End If
End If 'End ValidatePC Check
If blnLog = True And blnExecuteNext = True Then
Select Case intRtnCode
Case "0"
objOutput.WriteLine "Successfully" & strAction & " on " & strComputer
Case "-2147023518"
objOutput.WriteLine "User " & strUser & " is already in " & strGroup & " on " & strComputer
Case "-2147023519"
objOutput.WriteLine "User " & strUser & " not found in " & strGroup & " on " & strComputer
Case "-2147024844"
objOutput.WriteLine "A duplicate name exists on the network for " & strComputer
Case "424"
objOutput.WriteLine "User " & strUser & " does not exist in the Active Directory"
Case Else
If strUser <> "DISABLED" Then
objOutput.WriteLine "Unknown Error for user " & strUser & " for " & strGroup & " on " & strComputer
End If
End Select
End If
Next
If blnLog = True Then
objOutput.WriteBlankLines(2)
objOutput.Close
End If
Dim objWSH : Set objWSH = CreateObject("WScript.Shell")
objWSH.Run "notepad.exe " & OUTPUT_FILE
End If
Set objWSH = Nothing
Set objFSO = Nothing
Set objOutput = Nothing
End Sub 'End Execute_OnClick()
Function ValidatePC(strComputer)
If strComputer = "" Then
ValidatePC = False
Exit Function
End If
ON ERROR RESUME NEXT
Dim colPing : Set colPing = GetObject("winmgmts:\\").ExecQuery("Select * from Win32_PingStatus where Address='" & strComputer & "'")
Dim objPingStatus
For Each objPingStatus in colPing
If objPingStatus.StatusCode = 0 Then
If IsNull(GetObject("winmgmts:\\" & strComputer & "\root\cimv2")) Then
ValidatePC = False
Else
ValidatePC = True
End If
Else
ValidatePC = False
End If
Next
Set objPingStatus = Nothing
Set colPing = Nothing
End Function 'End ValidatePC(strComputer)
Function AddUser(strComputer, strUser)
On Error Resume Next
Dim objGroup : Set objGroup = GetObject("WinNT://" & strComputer & "/" & sel_Groups.Value & ",group")
Dim objUser : Set objUser = GetObject("WinNT://" & DOMAIN &"/" & strUser & ",user")
If Err.Number <> "-2147024844" Then
objGroup.Add(objUser.ADsPath)
End If
Set objGroup = Nothing
Set objUser = Nothing
AddUser = Err.Number
End Function 'End AddUser(strUser, strComputer)
Function DeleteUser(strComputer, strUser)
On Error Resume Next
Dim objGroup : Set objGroup = GetObject("WinNT://" & strComputer & "/" & sel_Groups.Value & ",group")
Dim objUser : Set objUser = GetObject("WinNT://" & DOMAIN &"/" & strUser & ",user")
If Err.Number <> "-2147024844" Then
objGroup.Remove(objUser.ADsPath)
End If
Set objGroup = Nothing
Set objUser = Nothing
DeleteUser = Err.Number
End Function 'End DeleteUser(strUser, strComputer)
Sub QueryGroup(strComputer, strGroup, objOutput)
On Error Resume Next
Dim objMember
Dim objGroup : Set objGroup = GetObject("WinNT://" & strComputer & "/" & sel_Groups.Value & ",group")
If Err.Number = "-2147024844" Then
objOutput.WriteLine "A duplicate name exists on the network for " & strComputer
objOutput.WriteBlankLines(2)
Else
objOutput.WriteLine "The following Users are in the " & strGroup & " group on " & strComputer
objOutput.WriteLine "--------------------------------------------------------------------"
For Each objMember in objGroup.Members
objOutput.WriteLine objMember.Name
Next
objOutput.WriteLine "--------------------------------------------------------------------"
objOutput.WriteBlankLines(2)
End If
Set objGroup = Nothing
End Sub
Sub ChangeState()
If Document.GetElementByID("query").Checked = True Then
txt_User.Disabled = True
txt_User.Value = "Disabled"
chk_Log.Checked = True
chk_Log.Disabled = True
Else
txt_User.Disabled = False
chk_Log.Checked = True
chk_Log.Disabled = False
If txt_User.Value = "Disabled" Then
txt_User.Value = ""
End If
End If
End Sub
Sub LoadFile_OnClick()
Dim objDialog : Set objDialog = CreateObject("UserAccounts.CommonDialog")
With objDialog
.Filter = "Text Files (*.txt)|*.txt"
.FilterIndex = 1
.InitialDir = "C:\"
End With
objDialog.ShowOpen
If objDialog.FileName <> "" Then
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(objDialog.FileName) Then
Dim objFile : Set objFile = objFSO.OpenTextFile(objDialog.FileName, READ_ONLY)
pcList.Value = ""
Do Until objFile.AtEndOfStream
Dim strLine : strLine = TRIM(objFile.ReadLine)
pcList.Value = pcList.Value & strLine & vbCrLf
Loop
Else
Msgbox objDialog.FileName & " not found!", vbExclamation,"Open File - Error"
End If
End If
Set objFile = Nothing
Set objFSO = Nothing
Set objDialog = Nothing
End Sub 'End GetFile_OnClick()
Sub Exit_OnClick()
self.Close()
End Sub 'End CloseApplication()
</SCRIPT>
</head>
<body>
<div id="left">
Remote PC(s):
<br>
<textarea id="pcList" rows="15" cols="25"></textarea>
<br>
</div>
<div id="right">
<fieldset>
<legend>Task</legend>
<input type="radio" name="rad_shutdownOpt" id="add" onClick="ChangeState" checked />Add User<br>
<input type="radio" name="rad_shutdownOpt" id="delete" onClick="ChangeState"/>Delete User<br>
<input type="radio" name="rad_shutdownOpt" id="query" onClick="ChangeState"/>Query Group<br>
</fieldset>
<br>
<fieldset>
<legend>User</legend>
<input type="text" id="txt_User" size="29"
/>
</fieldset>
<br>
<fieldset>
<legend>Local Group</legend>
<select id="sel_Groups"></select>
</fieldset>
<br>
<fieldset>
<legend>Logging</legend>
<input type="checkbox" id="chk_log" checked/>Enable
</fieldset>
</div>
<p>
<button name="LoadFile" accesskey="l"><u>L</u>oad File...</button> 
<button name="Execute" accesskey="e"><u>E</u>xecute</button> 
<button name="Exit" accesskey="x">E<u>x</u>it</button> 
</p>
</body>
</html>
If you're running a Domain, use GPO in order to add users to groups.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Dear Rob,
I missed that line, :( you are the man..!!!
Now the script works great...!!!
Need another help, In this script "Remote Local Group Manager Results.log" getting replaced for each action.
Is it possible to append each and every activity executed by this script?
Thanks,
Prem
I missed that line, :( you are the man..!!!
Now the script works great...!!!
Need another help, In this script "Remote Local Group Manager Results.log" getting replaced for each action.
Is it possible to append each and every activity executed by this script?
Thanks,
Prem
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks ROB....!!!
Cheers,
Prem
Cheers,
Prem