Submitted By: Jimmy Godard

Reads in information from a text file and then adds specified users to the Local Administrators group on a set of computers. Scripts results are logged to an Excel spreadsheet.

Visual Basic
Edit|Remove
Set objDialog = CreateObject("UserAccounts.CommonDialog")
Set oWshNet = CreateObject("WScript.Network")
Set ExcelSheet = CreateObject("Excel.Application")
ExcelSheet.Workbooks.Add
Const ForReading = 1
Const ForWriting = 2
intCount = 0
x = 2

objDialog.Filter = "All Files|*.*"
objDialog.InitialDir = "C:\"
intResult = objDialog.ShowOpen
 
If intResult = 0 Then
	Wscript.echo "Operation Terminated"
    Wscript.Quit
End If
AdminFile = objDialog.FileName

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    (AdminFile, ForReading)

ExcelSheet.ActiveSheet.Cells(1,1).Value = "Computer Name"
ExcelSheet.ActiveSheet.Cells(1,1).Font.Bold = TRUE

ExcelSheet.ActiveSheet.Cells(1,2).Value = "User Name"
ExcelSheet.ActiveSheet.Cells(1,2).Font.Bold = TRUE

ExcelSheet.ActiveSheet.Cells(1,3).Value = "Comments"
ExcelSheet.ActiveSheet.Cells(1,3).Font.Bold = TRUE


Do Until objTextFile.AtEndOfStream
    strReadFile = objTextFile.Readline

arrReadFile = Split(strReadFile , ",")
strWorkstation = Trim(arrReadFile(0))
strStandard_ID = Trim(arrReadFile(1))
strDomain = "CORP"

On Error Resume Next
Set objGroup = GetObject("WinNT://" & strWorkstation & "/Administrators,group")
If Err.Number <> 0 Then
  On Error GoTo 0
ExcelSheet.ActiveSheet.Cells(x,1).Value = strWorkstation

ExcelSheet.ActiveSheet.Cells(x,2).Value = strStandard_ID

ExcelSheet.ActiveSheet.Cells(x,3).Value = "Unable to connect to workstation" 
	Else
	On Error GoTo 0
 	On Error Resume Next
	Set objNBK = GetObject("WinNT://" & strDomain & "/" & strStandard_ID & ",user")
	If Err.Number <> 0 Then
      	    On Error GoTo 0
ExcelSheet.ActiveSheet.Cells(x,1).Value = strWorkstation

ExcelSheet.ActiveSheet.Cells(x,2).Value = strStandard_ID

ExcelSheet.ActiveSheet.Cells(x,3).Value = "User not found"
		Else
		' suppress errors in case the user is already a member
		  objGroup.Add(objNBK.ADsPath)
		     If Err.Number <> 0 Then
       			On Error GoTo 0
ExcelSheet.ActiveSheet.Cells(x,1).Value = strWorkstation

ExcelSheet.ActiveSheet.Cells(x,2).Value = strStandard_ID

ExcelSheet.ActiveSheet.Cells(x,3).Value = "User already a member"
		          Else
 ExcelSheet.ActiveSheet.Cells(x,1).Value = strWorkstation

ExcelSheet.ActiveSheet.Cells(x,2).Value = strStandard_ID


ExcelSheet.ActiveSheet.Cells(x,3).Value = "Successful"
        		    On Error GoTo 0
        		    intCount = intCount + 1
		     End If
	End If
End If

x = x + 1
Loop

objTextFile.Close
Set objTextFile = Nothing
Set objFSO = Nothing
Set objGroup = Nothing
Set objNBK = Nothing
ExcelSheet.ActiveWorkbook.SaveAs "C:\Administrator_Report.XLS"
ExcelSheet.Application.Quit
Set ExcelSheet = Nothing

Set ExcelSheet = CreateObject("Excel.Application")
ExcelSheet.Workbooks.Open("C:\Administrator_Report.XLS")
ExcelSheet.Application.Visible = True
Wscript.echo "COMPLETED"