Solved

Add Users to Remote machine local groups

Posted on 2014-09-12
5
179 Views
Last Modified: 2014-09-25
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

<!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>&nbsp 
   <button name="Execute" accesskey="e"><u>E</u>xecute</button>&nbsp 
   <button name="Exit" accesskey="x">E<u>x</u>it</button>&nbsp 
  </p> 
  </body> 
</html> 

Open in new window

0
Comment
Question by:Premkumar Yogeswaran
  • 2
  • 2
5 Comments
 
LVL 19

Expert Comment

by:strivoli
ID: 40318853
If you're running a Domain, use GPO in order to add users to groups.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 40321176
Hi, as long as you change
DOMAIN = "<YOUR_DOMAIN>"
To your short domain name, then use the logon ID as the username to add, it should work.

Rob.
0
 
LVL 17

Author Comment

by:Premkumar Yogeswaran
ID: 40321895
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
0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 500 total points
ID: 40322076
Yes, you change FOR_WRITING in the OpenTextFile call to FOR_APPENDING

Rob.
0
 
LVL 17

Author Closing Comment

by:Premkumar Yogeswaran
ID: 40345377
Thanks ROB....!!!

Cheers,
Prem
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Learn about cloud computing and its benefits for small business owners.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now