TechTalkz.com Logo Ask the Expert

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Microsoft > Windows Server 2003

Notices

Script to interrogate number of users in AD

Windows Server 2003


Reply
 
Thread Tools Display Modes
Old 26-06-2008, 04:43 AM   #1
Robin
Guest
 
Posts: n/a
Script to interrogate number of users in AD

Hi all

I am having issues with the following Script:

On Error Resume Next

Const ADS_SCOPE_SUBTREE = 2

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

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

objCommand.CommandText = _
"SELECT Name FROM 'LDAP://dc=fabrikam,dc=com' WHERE
objectCategory='user'"
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst
Do Until objRecordSet.EOF
Wscript.Echo objRecordSet.Fields("Name").Value
objRecordSet.MoveNext
Loop

I have replaced dc=fabrikam,dc=com with the name of our domain contoller.
This script has been pulled off the script repository.

It errors with table does not exist on the Set objRecordSet =
objCommand.Execute line.

Am I doing something wrong?
  Reply With Quote
Old 26-06-2008, 04:45 AM   #2
Chris Dent [MVP]
Guest
 
Posts: n/a
Re: Script to interrogate number of users in AD


DC=fabrikam,DC=com should be replaced with your own naming context.

That is, if your domain name is domain.com then the naming context becomes:

DC=domain,DC=com

Or if it is corp.somedomain.local it should be:

DC=corp,DC=somedomain,DC=local

--
Chris Dent
MVP Directory Services


"Robin" <Robin@discussions.microsoft.com> wrote in message
news:9166D6C9-3164-40D6-B289-35DAFE7A30C6@microsoft.com...
> Hi all
>
> I am having issues with the following Script:
>
> On Error Resume Next
>
> Const ADS_SCOPE_SUBTREE = 2
>
> Set objConnection = CreateObject("ADODB.Connection")
> Set objCommand = CreateObject("ADODB.Command")
> objConnection.Provider = "ADsDSOObject"
> objConnection.Open "Active Directory Provider"
> Set objCommand.ActiveConnection = objConnection
>
> objCommand.Properties("Page Size") = 1000
> objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
>
> objCommand.CommandText = _
> "SELECT Name FROM 'LDAP://dc=fabrikam,dc=com' WHERE
> objectCategory='user'"
> Set objRecordSet = objCommand.Execute
>
> objRecordSet.MoveFirst
> Do Until objRecordSet.EOF
> Wscript.Echo objRecordSet.Fields("Name").Value
> objRecordSet.MoveNext
> Loop
>
> I have replaced dc=fabrikam,dc=com with the name of our domain contoller.
> This script has been pulled off the script repository.
>
> It errors with table does not exist on the Set objRecordSet =
> objCommand.Execute line.
>
> Am I doing something wrong?



  Reply With Quote
Old 26-06-2008, 04:45 AM   #3
Richard Mueller [MVP]
Guest
 
Posts: n/a
Re: Script to interrogate number of users in AD

Robin wrote:

>
> I am having issues with the following Script:
>
> On Error Resume Next
>
> Const ADS_SCOPE_SUBTREE = 2
>
> Set objConnection = CreateObject("ADODB.Connection")
> Set objCommand = CreateObject("ADODB.Command")
> objConnection.Provider = "ADsDSOObject"
> objConnection.Open "Active Directory Provider"
> Set objCommand.ActiveConnection = objConnection
>
> objCommand.Properties("Page Size") = 1000
> objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
>
> objCommand.CommandText = _
> "SELECT Name FROM 'LDAP://dc=fabrikam,dc=com' WHERE
> objectCategory='user'"
> Set objRecordSet = objCommand.Execute
>
> objRecordSet.MoveFirst
> Do Until objRecordSet.EOF
> Wscript.Echo objRecordSet.Fields("Name").Value
> objRecordSet.MoveNext
> Loop
>
> I have replaced dc=fabrikam,dc=com with the name of our domain contoller.
> This script has been pulled off the script repository.
>
> It errors with table does not exist on the Set objRecordSet =
> objCommand.Execute line.
>
> Am I doing something wrong?


The script works fine for me when I substitute the DNS name of my domain and
remove "On Error Resume Next". Since there are no errors to be trapped, I
would recommend removing "On Error Resume Next". If you get an error raised
on the "objCommand.Execute" statement, the best guess is that the DNS name
of your domain is wrong. Also, note that the SQL query is one line. The code
line wrapped in you post. The following is two lines of code (not 3):

objCommand.CommandText = _
"SELECT Name FROM 'LDAP://dc=fabrikam,dc=com' WHERE
objectCategory='user'"

Perhaps "On Error Resume Next" allowed you to run the script with this
broken up into 3 lines, then raised the error later when ADO could not run
the query.

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


  Reply With Quote
Old 26-06-2008, 04:47 AM   #4
Robin
Guest
 
Posts: n/a
Re: Script to interrogate number of users in AD

I have removed the on error resume next. The VBscript then recieves the error.

The domain name is SECUREAD.rnli2.org.uk

How should this look in the script?

Like this? "SELECT name FROM 'LDAP://dc=SECUREAD.rnli2.org.uk, dc=com'
WHERE objectCategory='user'"

This is all one line not wrapped.

Thanks for your help.




"Richard Mueller [MVP]" wrote:

> Robin wrote:
>
> >
> > I am having issues with the following Script:
> >
> > On Error Resume Next
> >
> > Const ADS_SCOPE_SUBTREE = 2
> >
> > Set objConnection = CreateObject("ADODB.Connection")
> > Set objCommand = CreateObject("ADODB.Command")
> > objConnection.Provider = "ADsDSOObject"
> > objConnection.Open "Active Directory Provider"
> > Set objCommand.ActiveConnection = objConnection
> >
> > objCommand.Properties("Page Size") = 1000
> > objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
> >
> > objCommand.CommandText = _
> > "SELECT Name FROM 'LDAP://dc=fabrikam,dc=com' WHERE
> > objectCategory='user'"
> > Set objRecordSet = objCommand.Execute
> >
> > objRecordSet.MoveFirst
> > Do Until objRecordSet.EOF
> > Wscript.Echo objRecordSet.Fields("Name").Value
> > objRecordSet.MoveNext
> > Loop
> >
> > I have replaced dc=fabrikam,dc=com with the name of our domain contoller.
> > This script has been pulled off the script repository.
> >
> > It errors with table does not exist on the Set objRecordSet =
> > objCommand.Execute line.
> >
> > Am I doing something wrong?

>
> The script works fine for me when I substitute the DNS name of my domain and
> remove "On Error Resume Next". Since there are no errors to be trapped, I
> would recommend removing "On Error Resume Next". If you get an error raised
> on the "objCommand.Execute" statement, the best guess is that the DNS name
> of your domain is wrong. Also, note that the SQL query is one line. The code
> line wrapped in you post. The following is two lines of code (not 3):
>
> objCommand.CommandText = _
> "SELECT Name FROM 'LDAP://dc=fabrikam,dc=com' WHERE
> objectCategory='user'"
>
> Perhaps "On Error Resume Next" allowed you to run the script with this
> broken up into 3 lines, then raised the error later when ADO could not run
> the query.
>
> --
> Richard Mueller
> MVP Directory Services
> Hilltop Lab - http://www.rlmueller.net
> --
>
>
>

  Reply With Quote
Old 26-06-2008, 04:47 AM   #5
Robin
Guest
 
Posts: n/a
Re: Script to interrogate number of users in AD

Sorry I meant

"SELECT name FROM 'LDAP://dc=SECUREAD, dc=rnli2.org.uk' WHERE
objectCategory='user'"


"Robin" wrote:

> I have removed the on error resume next. The VBscript then recieves the error.
>
> The domain name is SECUREAD.rnli2.org.uk
>
> How should this look in the script?
>
> Like this? "SELECT name FROM 'LDAP://dc=SECUREAD.rnli2.org.uk, dc=com'
> WHERE objectCategory='user'"
>
> This is all one line not wrapped.
>
> Thanks for your help.
>
>
>
>
> "Richard Mueller [MVP]" wrote:
>
> > Robin wrote:
> >
> > >
> > > I am having issues with the following Script:
> > >
> > > On Error Resume Next
> > >
> > > Const ADS_SCOPE_SUBTREE = 2
> > >
> > > Set objConnection = CreateObject("ADODB.Connection")
> > > Set objCommand = CreateObject("ADODB.Command")
> > > objConnection.Provider = "ADsDSOObject"
> > > objConnection.Open "Active Directory Provider"
> > > Set objCommand.ActiveConnection = objConnection
> > >
> > > objCommand.Properties("Page Size") = 1000
> > > objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
> > >
> > > objCommand.CommandText = _
> > > "SELECT Name FROM 'LDAP://dc=fabrikam,dc=com' WHERE
> > > objectCategory='user'"
> > > Set objRecordSet = objCommand.Execute
> > >
> > > objRecordSet.MoveFirst
> > > Do Until objRecordSet.EOF
> > > Wscript.Echo objRecordSet.Fields("Name").Value
> > > objRecordSet.MoveNext
> > > Loop
> > >
> > > I have replaced dc=fabrikam,dc=com with the name of our domain contoller.
> > > This script has been pulled off the script repository.
> > >
> > > It errors with table does not exist on the Set objRecordSet =
> > > objCommand.Execute line.
> > >
> > > Am I doing something wrong?

> >
> > The script works fine for me when I substitute the DNS name of my domain and
> > remove "On Error Resume Next". Since there are no errors to be trapped, I
> > would recommend removing "On Error Resume Next". If you get an error raised
> > on the "objCommand.Execute" statement, the best guess is that the DNS name
> > of your domain is wrong. Also, note that the SQL query is one line. The code
> > line wrapped in you post. The following is two lines of code (not 3):
> >
> > objCommand.CommandText = _
> > "SELECT Name FROM 'LDAP://dc=fabrikam,dc=com' WHERE
> > objectCategory='user'"
> >
> > Perhaps "On Error Resume Next" allowed you to run the script with this
> > broken up into 3 lines, then raised the error later when ADO could not run
> > the query.
> >
> > --
> > Richard Mueller
> > MVP Directory Services
> > Hilltop Lab - http://www.rlmueller.net
> > --
> >
> >
> >

  Reply With Quote
Old 26-06-2008, 04:47 AM   #6
Robin
Guest
 
Posts: n/a
RE: Script to interrogate number of users in AD

Ok great guys got that working!

But it outputs all the users in the directory to screen.

How would I edit the code so that I can get the output to a txt file or
equivalent?

Thanks in advqance

"Robin" wrote:

> Hi all
>
> I am having issues with the following Script:
>
> On Error Resume Next
>
> Const ADS_SCOPE_SUBTREE = 2
>
> Set objConnection = CreateObject("ADODB.Connection")
> Set objCommand = CreateObject("ADODB.Command")
> objConnection.Provider = "ADsDSOObject"
> objConnection.Open "Active Directory Provider"
> Set objCommand.ActiveConnection = objConnection
>
> objCommand.Properties("Page Size") = 1000
> objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
>
> objCommand.CommandText = _
> "SELECT Name FROM 'LDAP://dc=fabrikam,dc=com' WHERE
> objectCategory='user'"
> Set objRecordSet = objCommand.Execute
>
> objRecordSet.MoveFirst
> Do Until objRecordSet.EOF
> Wscript.Echo objRecordSet.Fields("Name").Value
> objRecordSet.MoveNext
> Loop
>
> I have replaced dc=fabrikam,dc=com with the name of our domain contoller.
> This script has been pulled off the script repository.
>
> It errors with table does not exist on the Set objRecordSet =
> objCommand.Execute line.
>
> Am I doing something wrong?

  Reply With Quote
Old 26-06-2008, 04:48 AM   #7
Richard Mueller [MVP]
Guest
 
Posts: n/a
Re: Script to interrogate number of users in AD

Run the program at a command prompt and redirect to a text file. For
example:

cscript //nologo Example.vbs > report.txt

This assumes the VBScript program is saved in a file called Example.vbs. If
the file is not in the current directory, you must include the full path and
file name in the command above. The file report.txt is created in the
current directory. The optional "//nologo" suppresses the logo information
produced by cscript.exe.

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

"Robin" <Robin@discussions.microsoft.com> wrote in message
news:2DCE6681-3E13-42EE-B006-961E52A824E8@microsoft.com...
> Ok great guys got that working!
>
> But it outputs all the users in the directory to screen.
>
> How would I edit the code so that I can get the output to a txt file or
> equivalent?
>
> Thanks in advqance
>
> "Robin" wrote:
>
>> Hi all
>>
>> I am having issues with the following Script:
>>
>> On Error Resume Next
>>
>> Const ADS_SCOPE_SUBTREE = 2
>>
>> Set objConnection = CreateObject("ADODB.Connection")
>> Set objCommand = CreateObject("ADODB.Command")
>> objConnection.Provider = "ADsDSOObject"
>> objConnection.Open "Active Directory Provider"
>> Set objCommand.ActiveConnection = objConnection
>>
>> objCommand.Properties("Page Size") = 1000
>> objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
>>
>> objCommand.CommandText = _
>> "SELECT Name FROM 'LDAP://dc=fabrikam,dc=com' WHERE
>> objectCategory='user'"
>> Set objRecordSet = objCommand.Execute
>>
>> objRecordSet.MoveFirst
>> Do Until objRecordSet.EOF
>> Wscript.Echo objRecordSet.Fields("Name").Value
>> objRecordSet.MoveNext
>> Loop
>>
>> I have replaced dc=fabrikam,dc=com with the name of our domain contoller.
>> This script has been pulled off the script repository.
>>
>> It errors with table does not exist on the Set objRecordSet =
>> objCommand.Execute line.
>>
>> Am I doing something wrong?



  Reply With Quote
Old 26-06-2008, 04:53 AM   #8
Robin
Guest
 
Posts: n/a
Re: Script to interrogate number of users in AD

Cheers richard you are a Star.

"Richard Mueller [MVP]" wrote:

> Run the program at a command prompt and redirect to a text file. For
> example:
>
> cscript //nologo Example.vbs > report.txt
>
> This assumes the VBScript program is saved in a file called Example.vbs. If
> the file is not in the current directory, you must include the full path and
> file name in the command above. The file report.txt is created in the
> current directory. The optional "//nologo" suppresses the logo information
> produced by cscript.exe.
>
> --
> Richard Mueller
> MVP Directory Services
> Hilltop Lab - http://www.rlmueller.net
> --
>
> "Robin" <Robin@discussions.microsoft.com> wrote in message
> news:2DCE6681-3E13-42EE-B006-961E52A824E8@microsoft.com...
> > Ok great guys got that working!
> >
> > But it outputs all the users in the directory to screen.
> >
> > How would I edit the code so that I can get the output to a txt file or
> > equivalent?
> >
> > Thanks in advqance
> >
> > "Robin" wrote:
> >
> >> Hi all
> >>
> >> I am having issues with the following Script:
> >>
> >> On Error Resume Next
> >>
> >> Const ADS_SCOPE_SUBTREE = 2
> >>
> >> Set objConnection = CreateObject("ADODB.Connection")
> >> Set objCommand = CreateObject("ADODB.Command")
> >> objConnection.Provider = "ADsDSOObject"
> >> objConnection.Open "Active Directory Provider"
> >> Set objCommand.ActiveConnection = objConnection
> >>
> >> objCommand.Properties("Page Size") = 1000
> >> objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
> >>
> >> objCommand.CommandText = _
> >> "SELECT Name FROM 'LDAP://dc=fabrikam,dc=com' WHERE
> >> objectCategory='user'"
> >> Set objRecordSet = objCommand.Execute
> >>
> >> objRecordSet.MoveFirst
> >> Do Until objRecordSet.EOF
> >> Wscript.Echo objRecordSet.Fields("Name").Value
> >> objRecordSet.MoveNext
> >> Loop
> >>
> >> I have replaced dc=fabrikam,dc=com with the name of our domain contoller.
> >> This script has been pulled off the script repository.
> >>
> >> It errors with table does not exist on the Set objRecordSet =
> >> objCommand.Execute line.
> >>
> >> Am I doing something wrong?

>
>
>

  Reply With Quote
Reply

Thread Tools
Display Modes



< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT +5.5. The time now is 12:37 PM.


vBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2010, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional