Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable multiple computer accounts using TXT file with computer names

Status
Not open for further replies.

Palito1916

Systems Engineer
Dec 4, 2023
2
GB
So I have Windows Server 2003 AD with a lot of machines and PowerShell is not an option.
I run dsquery computer comand to give me devices inctive for 104 and removed some that could not be removed due to still being in use.

I am trying to come up with a VBScript to disable the devices in a TXT file with devices names but the script errors out.

This is what I am trying to use.

Code:
Option Explicit

Dim strFileName, objFSO, objFile, strComputerName, objComputer
Dim arrComputerNames, intCount

' Specify the TXT file containing the list of computer names
strFileName = "C:\path\to\computers.txt"

' Read computer names from the TXT file into an array
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, 1)

Do Until objFile.AtEndOfStream
    strComputerName = Trim(objFile.ReadLine)
    If strComputerName <> "" Then
        ReDim Preserve arrComputerNames(intCount)
        arrComputerNames(intCount) = strComputerName
        intCount = intCount + 1
    End If
Loop

objFile.Close

' Bind to each computer account and disable it
For Each strComputerName In arrComputerNames
    On Error Resume Next
    Set objComputer = GetObject("LDAP://" & strComputerName)

    If Err.Number = 0 Then
        ' Check if the computer account is not already disabled
        If Not objComputer.AccountDisabled Then
            ' Disable the computer account
            objComputer.AccountDisabled = True
            objComputer.SetInfo
            WScript.Echo "Disabled computer account: " & strComputerName
        Else
            WScript.Echo "Computer account is already disabled: " & strComputerName
        End If
        Set objComputer = Nothing
    Else
        WScript.Echo "Error binding to computer account " & strComputerName & ": " & Err.Description
    End If

    On Error GoTo 0
Next

WScript.Echo "Script completed."

My Server errors out and says:
Line:16
Char:9
Error:Type mismatch
Error Code:800A000D

Appreciate all your help
 
Declare [tt]arrComputerNames[/tt] as array:
[pre]Dim arrComputerNames(), intCount[/pre]

combo
 
So that would require the change like below?

Code:
Option Explicit

Dim strFileName, objFSO, objFile, strComputerName, objComputer
Dim arrComputerNames(), intCount

' Specify the TXT file containing the list of computer names
strFileName = "C:\path\to\computers.txt"

Is there anything else I need to change?
Does this need to change as well?

Code:
Do Until objFile.AtEndOfStream
    strComputerName = Trim(objFile.ReadLine)
    If strComputerName <> "" Then
        ReDim Preserve arrComputerNames(intCount)
        arrComputerNames(intCount) = strComputerName
        intCount = intCount + 1
    End If
Loop

To something like that?

Code:
Do Until objFile.AtEndOfStream
    strComputerName = Trim(objFile.ReadLine)
    If strComputerName <> "" Then
        ReDim Preserve arrComputerNames(intCount)
        arrComputerNames(intCount) = strComputerName
        intCount = intCount + 1
    End If
Loop
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top