Solved

VBSCRIPT for disabling inactive computer accounts

Posted on 2012-12-26
4
1,002 Views
Last Modified: 2012-12-31
Hello Team,

I need a VBscript to disable the list of computers which is available in different OU. It should be like if we can have the computers name in text file & script can take the input from the file itself & can disable it.

Regards,
Biplab
0
Comment
Question by:biplabmukh
  • 2
4 Comments
 
LVL 1

Expert Comment

by:Shizzo
Comment Utility
0
 
LVL 21

Accepted Solution

by:
yo_bee earned 500 total points
Comment Utility
That code is nice, but if you are looking to a more dynamic script you might want to try this.

On Error Next
Dim StrComp

'************************************************************************************
'Creates a Connection Object for connection to AD				
'************************************************************************************
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")

'************************************************************************************
'Opens the text file for reading
'************************************************************************************
set myFSO = CreateObject("Scripting.FileSystemObject") 
set myFile = myfso.openTextFile("\\flhlaw.local\userdata\Folders\BOSCAM\Documents\Computername.txt", 1)


Do
'************************************************************************************
'Starts to read line by line													
'************************************************************************************

strComp = myfile.Readline

'************************************************************************************
'Connecting to Active Directory														
'************************************************************************************
objConnection.Provider = "ADSDSOObject"
objConnection.Open "" 

'************************************************************************************
'Get the Root DSE from a random DC													
'************************************************************************************  

Set objRootDSE = GetObject("LDAP://RootDSE")

'************************************************************************************
'Connect to the schema container on a random DC													
'************************************************************************************  

strDNSDomain = objRootDSE.Get("defaultNamingContext")
Set StrDC = GetObject("LDAP://" & StrDNSDomain)
StrServices = Ucase(StrDC.DC)



'************************************************************************************
'Searches AD for the computer name and returns the LDAP DN
'************************************************************************************
Set objRS = objConnection.Execute("<LDAP://" & StrDNSDomain & ">;(&(&(&(objectCategory=computer)(objectClass=computer)(Name=" & strComp &"))));Name,ADsPath;SubTree")
While Not objRS.EOF

strName = objRS.Fields.Item("Name").Value
strADs = objRS.Fields.Item("ADsPath").Value


'************************************************************************************
'Connects to the Computer Object and disables it
'************************************************************************************
 set objComputer = GetObject(StrADs)

objComputer.AccountDisabled = True 
objComputer.SetInfo 
WScript.Echo("Disabled " & strComputer & " on Active Directory")

objRS.MoveNext

Wend

set objRS = Nothing
objConnection.Close
Loop Until Myfile.AtEndOfStream
MyFile.close

Open in new window

0
 

Author Comment

by:biplabmukh
Comment Utility
Thanks for your reply, I will validate tomorrow & let you know the update. Thanks once again.
0
 

Author Comment

by:biplabmukh
Comment Utility
I have tested the script & it is working fine. Thanks for your help.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Mapping Drives using Group policy preferences Are you still using old scripts to map your network drives if so this article will show you how to get away for old scripts and move toward Group Policy Preference for mapping them. First things f…
Synchronize a new Active Directory domain with an existing Office 365 tenant
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

771 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

12 Experts available now in Live!

Get 1:1 Help Now