Solved

VBSCRIPT for disabling inactive computer accounts

Posted on 2012-12-26
4
1,037 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
ID: 38724732
0
 
LVL 22

Accepted Solution

by:
yo_bee earned 500 total points
ID: 38725334
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
ID: 38731119
Thanks for your reply, I will validate tomorrow & let you know the update. Thanks once again.
0
 

Author Comment

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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In-place Upgrading Dirsync to Azure AD Connect
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

808 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