Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Server > Scripting > Re: Query OU for disabled computers

Reply
Thread Tools Display Modes

Re: Query OU for disabled computers

 
 
Richard Mueller [MVP]
Guest
Posts: n/a

 
      03-20-2009
"rushtosri" <> wrote in message
news:ac856bf4-0fcd-4932-9420-...
> Hi Guys,
>
> I'm working on a script to enumerate OUs for disabled computer
> objects. Any idea how the query should look like? The following script
> can lookup for disabled user accounts. Any thoughts on how to modify
> this script to lookup for disabled computer accounts??
>
> Set objCommand = CreateObject("ADODB.Command")
> objCommand.ActiveConnection = objConnection
> objCommand.CommandText = _
> "<GC://dc=fabrikam,dc=com>;(objectCategory=User)" & _
> ";userAccountControl,distinguishedName;subtree "
> Set objRecordSet = objCommand.Execute
>
> intCounter = 0
> Do Until objRecordset.EOF
> intUAC=objRecordset.Fields("userAccountControl")
> If intUAC AND ADS_UF_ACCOUNTDISABLE Then
> WScript.echo objRecordset.Fields("distinguishedName") & " is
> disabled"
> intCounter = intCounter + 1
> End If
> objRecordset.MoveNext
> Loop
>
> Thanks in advance.


Besides oldcomp and dsquery, you can revise your VBScript solution. The
filter for computer objects is (objectCategory=computer). Rather than
returning all computer objects and testing the ADS_UF_ACCOUNTDISABLE bit of
userAccountControl, you can query for just the computer objects where that
bit is set. See below:
===========
Option Explicit

Dim adoCommand, adoConnection, strBase, strFilter, strAttributes

Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strDN

Dim intCounter



' Setup ADO objects.

Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection



' Search entire Active Directory domain.

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

strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"


' Filter on disabled computer objects.
strFilter = "(&(objectCategory=computer)" _

& "(userAccountControl:1.2.840.113556.1.4.803:=2 ))"



' Comma delimited list of attribute values to retrieve.
strAttributes = "distinguishedName"



' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False



' Run the query.
Set adoRecordset = adoCommand.Execute


' Enumerate the resulting recordset.

intCount = 0
Do Until adoRecordset.EOF

' Retrieve values and display.
strDN = adoRecordset.Fields("distinguishedName").Value

Wscript.Echo strDN & " is disabled"

intCount = intCount + 1

' Move to the next record in the recordset.
adoRecordset.MoveNext
Loop



' Clean up.

adoRecordset.Close

adoConnection.Close



Wscript.Echo CStr(intCounter) & " computers are disabled.


--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Query OU for disabled computers Lanwench [MVP - Exchange] Scripting 2 03-20-2009 12:39 AM
Re: Query OU for disabled computers Masterplan Scripting 0 03-19-2009 01:30 PM
Re: Query Disabled computers in AD Jerold Schulman Scripting 0 01-17-2007 12:10 PM
Query for disabled users...? Per Hagstrom Active Directory 8 10-26-2006 03:39 AM
disabled users query?? Jack Black Active Directory 5 12-09-2004 01:47 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59