'========================================================================== ' NAME: disablePCsFromList.vbs ' ' AUTHOR: Alan Kaplan, VA R3 alan dot kaplan at va dot gov ' disables a list of computers in a text file. ' DATE : 7/7/2006, 1/14/14 '========================================================================= Option Explicit Dim wshShell Set wshShell = WScript.CreateObject("WScript.Shell") Dim arrComputers, strComputer Dim oRoot, sDomain, strCommand Dim sAdsPath, message Dim strComputerName, retval Dim bNotFound Dim oRS, ComputerPath, oADTmp Dim oConn, oCommand Const ADS_SCOPE_SUBTREE = 2 If (Not IsCScript()) Then 'If not CScript, re-run with cscript... dim quote, strArgs, i quote=chr(34) For i = WScript.Arguments.Count -1 to 0 Step -1 strArgs = WScript.Arguments(i) & Space(1) & strArgs Next WshShell.Run "CScript.exe " & quote & WScript.ScriptFullName & quote & space(1) & strArgs, 1, true WScript.Quit '...and stop running as WScript End If message = "This script disables computer accounts from a list. The " & _ "file should contain the computer's NetBIOS names, one per line. Please include a [RETURN] after last entry, " & _ "and do not include a title line. " & VbCrLf & VbCrLf & "Do you want to continue?" retval = MsgBox(message, vbQuestion + vbYesNoCancel,"Welcome") If retval <> vbYes Then WScript.Quit Dim strFilter strFilter = "Text Files (*.txt)|*.txt|All Files(*.*)|*.*" ' show file types GetFileName "Select the file you want to open", "", strFilter, "" 'Set up AD connect for OU search Set oConn = CreateObject("ADODB.Connection") Set oCommand = CreateObject("ADODB.Command") oConn.Provider = "ADsDSOObject" oConn.Open "Active Directory Provider" Set oCommand.ActiveConnection = oConn oCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 'Subtree required 'Get the ADsPath for the current domain to search. Set oRoot = GetObject("LDAP://rootDSE") sDomain = ORoot.Get("defaultNamingContext") dim fso,logfile, appendout logfile = wshShell.ExpandEnvironmentStrings("%userprofile%") & "\desktop\disablePCLog.txt" 'Setup Log Const ForAppend = 8 set fso = CreateObject("Scripting.FileSystemObject") set AppendOut = fso.OpenTextFile(logfile, ForAppend, True) 'Go through the array For i = 0 To UBound(arrComputers) If Len(arrComputers(i)) > 0 Then strComputer = ucase(arrComputers(i)) bNotFound = False DisablePC strComputer End If Next 'Done '*********** Functions and Sub ******************** Function GetFileName(strCaption, strInitDir , strFilter, strDefaultFileName) 'using microsoft's common dialog component: On Error Resume Next Dim oCD Set oCD = WScript.CreateObject("MSComDlg.CommonDialog") If Err <> 0 Then Err.Clear GetFileName = InputBox(strCaption,"Open File",strInitDir & strDefaultFileName) If GetFileName = "" Then WScript.Quit Else With OCD .DialogTitle = strCaption ' set dialog caption .MaxFileSize = 512 ' allocate sufficient space for filespec(s) .InitDir = strInitDir ' set opening directory .CancelError = True ' attempting to cancel will throw error .Filter = strFilter .Flags = &H80000 AND &H2 .FilterIndex = 1 '.FilterIndex = 2 ' show all files If Len (strDefaultFileName) > 0 Then strDefaultFileName = "*.*" '.FileName = strDefaultFileName' set default filename End With oCD.ShowOpen ' show open file dialog, get user selection If Err <> 0 Then MsgBox ("Cancel pressed") WScript.Quit Else GetFileName = oCD.FileName ' show selection arrComputers = ArrayFromList(oCD.FileName) End If End If On Error GoTo 0 End Function Function ArrayFromList(strFilePath) Dim fso, f, strAll Set fso = CreateObject("Scripting.FileSystemObject") Const ForReading = 1 set f = fso.OpenTextFile(strFilePath,ForReading) strAll = f.ReadAll ArrayFromList = Split(strAll,VbCrLf) End Function Sub DisablePC(strName) On Error Resume Next Dim adObj,strADSPath, strDescription strADSPath = ComputerADSPath(strName) If bNotFound Then Exit Sub Set ADObj = GetObject(strADSPath) ADObj.Description = "{Disabled on " & Date & "} " & strDescription If ADObj.accountDisabled = False Then ADObj.AccountDisabled = True ADObj.SetInfo If Err = 0 Then EchoAndLog "disabled " & strName Else EchoAndLog "Failed to disable " & strName & Space(1) & trim(Err.Description) End If Err.Clear On Error GoTo 0 end Sub Function ComputerADSPath(strComputer) ' get ou from name strCommand = "SELECT ADsPath FROM 'LDAP://" & sDomain &"' WHERE objectCategory='Computer' " & _ "AND Name ='" & strComputer & "'" oCommand.CommandText = strCommand Set oRS = oCommand.Execute If oRS.BOF And oRS.EOF Then bNotFound = True EchoAndLog strComputer & " not found in " & sDomain Exit Function End If 'note unique answer means no need to loop ComputerADSPath = oRS.Fields("ADsPath").Value End Function Sub EchoAndLog (message) 'Echo output and write to log Wscript.Echo message AppendOut.WriteLine message End Sub Function IsCScript() If (InStr(UCase(WScript.FullName), "CSCRIPT") <> 0) Then IsCScript = True Else IsCScript = False End If End Function