All Forums >> [Scripting] >> Post a VBScript >> Add Network Group as Local Administrators and Change local Administrator Password - Web Interface Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Hello Friends out there, here you'll find a Script that remotely add a domain group as member of local administrators groups, and also provide you as an option, changing the local administrator password. In this script it's not needed write down user neither passwords in the code. Special Thanks to Scott Mikesell and FF. Enjoy yourself!!!
'******************************************************************************************************* 'Local Administrator Group ADD 'wrote by Jeferson Propheta and Scott Mikesell '******************************************************************************************************* On Error Resume Next '******************************************************************************************************* 'Constants '******************************************************************************************************* Const ForWriting = 2 Const ADS_SECURE_AUTHENTICATION = &H1 '******************************************************************************************************* 'Sets '******************************************************************************************************* InterfaceFile = "C:\LocalAdmin.htm" AcceptLocalPass = "NO" Set WshShell = WScript.CreateObject("WScript.Shell") Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject") Set objExplorer = WScript.CreateObject("InternetExplorer.Application", "IE_") Set objHTMLFile = objFSO.OpenTextFile (InterfaceFile, ForWriting, True) Set HtmlFile = objFSO.GetFile(InterfaceFile)
objExplorer.Quit Wscript.Sleep 250 If strButton = "Cancelled" Then HtmlFile.Delete Wscript.Quit Else HtmlFile.Delete If strAdminGrp = "" Then HtmlFile.Delete WScript.Quit(1) End IF If strTxtFile = "" Then HtmlFile.Delete WScript.Quit(1) End IF
Set objTextFile = objFSO.OpenTextFile(strTxtFile, 1) If Err.Number = "53" Then msgerror= MsgBox("Computer List File Not Found" & vbCrLf & "Now Aborting..." & vbCrLf & "Please Try Again." , vbCritical + vbOKOnly, "Script Error") HtmlFile.Delete WScript.Quit(1) End IF
If strLocalAdmPasswordCheck <> strLocalAdmPAssword Then msgerror= MsgBox("New Password and Confirm must match! Please re-type them " & vbCrLf & "Now Aborting..." & vbCrLf & "Please Try Again.", vbCritical + vbOKOnly, "Script Error") HtmlFile.Delete WScript.Quit(1) End IF '******************************************************************************************************* 'Starting Excel to Generate Report '******************************************************************************************************* Set objExcel = CreateObject("Excel.Application") objExcel.Visible = True objExcel.Workbooks.Add objExcel.Columns(1).ColumnWidth = 3 objExcel.Columns(2).ColumnWidth = 25 objExcel.Columns(3).ColumnWidth = 40 objExcel.Columns(4).ColumnWidth = 40 objExcel.ActiveCell.Font.Size = 12 objExcel.ActiveCell.Font.Bold = True objExcel.ActiveCell.Value = "Computer List - Local Group ADD( " & Date & " - " & Time & " )" objExcel.ActiveCell.Offset(1,0).Activate objExcel.ActiveCell.Value = "File: " & strTxtFile & " - " & "Adding Group: " & strAdminGrp & " | Changing Local Password: " & AcceptLocalPass objExcel.ActiveCell.Offset(2,0).Activate objExcel.ActiveCell.Value = "#" objExcel.ActiveCell.Offset(0,1).Activate objExcel.ActiveCell.Value = "Computer Name" objExcel.ActiveCell.Offset(0,1).Activate objExcel.ActiveCell.Value = "Group Adding Status" objExcel.ActiveCell.Offset(0,1).Activate objExcel.ActiveCell.Value = "Password Changing Status" objExcel.ActiveCell.Offset(1,-4).Activate
objExcel.Range("A4:D4").Select objExcel.Selection.Font.Bold = True objExcel.Selection.Interior.ColorIndex = 15 '******************************************************************************************************* 'Open Text File and Execute while not EOF '******************************************************************************************************* Do While objTextFile.AtEndOfStream <> True objExcel.ActiveCell.Offset(1,0).Activate PCCount = PCCount + 1 objExcel.ActiveCell.Value = PCCount
strFinished = MsgBox("Done!!!!!" & vbCrLf & "At " & Now(), vbInformation + vbOKOnly, "Finished!!!") '******************************************************************************************************* 'Routine: AdminGroup '******************************************************************************************************* Sub adminGroup(strComputer) ' Bind to domain group with WinNT provider. Set objDomainGroup = GetObject("WinNT://" & strDomain & "/" & strAdminGrp & ",group")
If Err.Number = "-2147024843" Then msgerror= MsgBox("Domain or Admin Group Not Found." & vbCrLf & "Now Aborting..." & vbCrLf & "Please Try Again.", vbCritical + vbOKOnly, "Script Error") WScript.Quit(1) End IF ' Bind to local Administrators group on remote computer. ' Use alternate credentials. Set objNS = GetObject("WinNT:") On Error Resume Next
Set objLocalGroup = objNS.OpenDSObject("WinNT://" & strComputer & "/Administrators,group", strUser, strPassword, ADS_SECURE_AUTHENTICATION) If Err.Number = "-2147022676" Then Err.Clear Set objLocalGroup = objNS.OpenDSObject("WinNT://" & strComputer & "/Administradores,group", strUser, strPassword, ADS_SECURE_AUTHENTICATION) End If If Err.Number <> 0 Then Select Case Hex(Err.Number)
Case "80070035" : objExcel.ActiveCell.Offset(0,1).Activate objExcel.ActiveCell.Value = "Computer is not Reachable - " & Hex(Err.Number) objExcel.ActiveCell.Font.Bold = True objExcel.ActiveCell.Font.ColorIndex = 3 objExcel.ActiveCell.Offset(0,1).Activate objExcel.ActiveCell.Value = "N/A" ExcellineBack
Case "800708AC" : objExcel.ActiveCell.Offset(0,1).Activate objExcel.ActiveCell.Value = "Local Administrator Group Name is Wrong - " & Hex(Err.Number) objExcel.ActiveCell.Offset(0,1).Activate objExcel.ActiveCell.Value = "N/A" ExcellineBack
Case "80070562" : objExcel.ActiveCell.Offset(0,1).Activate objExcel.ActiveCell.Value = "Group is Already Member." objExcel.ActiveCell.Offset(0,1).Activate objExcel.ActiveCell.Value = "N/A" ExcellineBack
If Err.Number = 0 Then objExcel.ActiveCell.Offset(0,1).Activate objExcel.ActiveCell.Value = "Added Successfully" Else Select Case Hex(Err.Number)
Case "800708AC" : objExcel.ActiveCell.Offset(0,1).Activate objExcel.ActiveCell.Value = "Local Administrator Group Name is Wrong."
Case "80070562" : objExcel.ActiveCell.Offset(0,1).Activate objExcel.ActiveCell.Value = "Group is Already Member." Case Else : objExcel.ActiveCell.Offset(0,1).Activate objExcel.ActiveCell.Value = "Unknown Error: " & Hex(Err.Number) & " - " & Err.Description & " - " & Err.Source End Select End IF
IF AcceptLocalPass = "YES" Then Err.Clear Set objUser = objNS.OpenDSObject("WinNT://" & strComputer & "/Administrator", strUser, strPassword, ADS_SECURE_AUTHENTICATION) If Err.Number <> 0 Then Err.Clear Set objUser = objNS.OpenDSObject("WinNT://" & strComputer & "/Administrador", strUser, strPassword, ADS_SECURE_AUTHENTICATION)
If Err.Number = 0 Then objUser.SetPassword(strLocalAdmPAssword)
If Err.Number = 0 Then objExcel.ActiveCell.Offset(0,1).Activate objExcel.ActiveCell.Value = "Successfully Changed" ExcellineBack Else objExcel.ActiveCell.Offset(0,1).Activate objExcel.ActiveCell.Value = "Error: " & Hex(Err.Number) ExcellineBack End IF Else objExcel.ActiveCell.Offset(0,1).Activate objExcel.ActiveCell.Value = "Error! Cannot find Admin Users - "& Hex(Err.Number) ExcellineBack End IF Else objUser.SetPassword(strLocalAdmPAssword) If Err.Number = 0 Then objExcel.ActiveCell.Offset(0,1).Activate objExcel.ActiveCell.Value = "Successfully Changed" ExcellineBack Else objExcel.ActiveCell.Offset(0,1).Activate objExcel.ActiveCell.Value = "Error: " & Hex(Err.Number) ExcellineBack End IF End IF Else objExcel.ActiveCell.Offset(0,1).Activate objExcel.ActiveCell.Value = "Password was not changed..." ExcellineBack End IF End If '******************************************************************************************************* 'Cleaning UP '******************************************************************************************************* Set objDomainGroup = Nothing Set objLocalGroup = Nothing Set objNS = Nothing Err.clean ExcellineBack End Sub End If
Function ExcellineBack() objExcel.ActiveCell.Offset(0,-1).Activate objExcel.ActiveCell.Offset(0,-2).Activate objExcel.ActiveCell.Offset(0,-3).Activate objExcel.ActiveCell.Offset(0,-4).Activate objExcel.ActiveCell.Offset(0,-5).Activate End Function