Re: scripting newb




"Polonius19" <polonius19@xxxxxxxxx> wrote in message
news:8469ba9a-a10d-4c3c-8d35-6ef1a932cd74@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Mar 11, 11:32 am, Polonius19 <poloniu...@xxxxxxxxx> wrote:
What I'm trying to do is fairly simple at the command line (dsquery
computer -name tw* -limit 0 -o rdn > c:\thinclient1.list), but I'm
having a hard time scripting that in vbs...

I was able to hobble this together from another script posted by Dan
Thomson, but I'm not getting the output I want. The -o flag gives me
just the computer name. In this script, I get the same output as if I
hadn't used that flag, this is the script that I have:

'******************************************************************************
'Script to search for a user, users or groups in an Active Directory
Domain.
'The script accepts wild cards to perform the search.
'
'The search is performed against the AD information stored in
samAccountName
'
'Modified on 3/11/09 to write to a file by hluna@xxxxxxxxxxxx
'
'SearchDC.vbs version 0.2 was last updated on 24/03/2003 by Dan
Thomson
'E-mail address: - dethomson@xxxxxxxxxxx
'
'This script was originally authored by Nigel Thomas on 28/06/2002
'E-mail address: - nthomas2020@xxxxxxxxx
'
' All rights reserved.
'******************************************************************************

Option Explicit
'On Error Resume Next

'Declare variables
Dim strSearch, strAdsPath, strServerName 'from search script
Dim strDefaultDomainNC, strADSQuery 'from search script
Dim objQueryResultSet, objADOConn, objADOCommand 'from search script
Dim intCount 'from search script
Dim objFSO1, objFSO2, objFile, objShell 'from strip_quotes script
Dim strFile1, strFile2, strContents 'from strip_quotes script


Const adStateOpen = 1
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

strFile1 = "c:\thinclient1.list"
strFile2 = "c:\thinclient2.list"

Set objFSO1 = CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject("WScript.Shell")

If objFSO1.FileExists(strFile1) Then
objFSO1.DeleteFile(strFile1)
Wscript.Echo "Just deleted " & strFile1
End If

If objFSO1.FileExists(strFile2) Then
objFSO1.DeleteFile(strFile2)
Wscript.Echo "Just deleted " & strFile2
End If

Set objFile = objFSO1.CreateTextFile(strFile1)
Wscript.Echo "Just created " & strFile1

Set objFile = objFSO1.CreateTextFile(strFile2)
Wscript.Echo "Just created " & strFile2

'Check for command line argument
If WScript.Arguments.Count < 1 Then
Do While strSearch = ""
'Prompt for search criteria
strSearch = InputBox("Please specify the search criteria", "Search
AD")
'If no input...Check if user wants to Exit
'If user selects Yes then exit.
If strSearch = "" Then If MsgBox("Do you wish to cancel ?", 36, _
"Search AD") = 6 Then Call Cleanup(1)
Loop
Else
'Use search criteria specified on the command line
strSearch = WScript.Arguments.Item(0)
End If

'Get the local Computer Name
strServerName = WScript.CreateObject("WScript.Network").ComputerName

'Get the Default Domain Naming Context
strDefaultDomainNC = GetObject("LDAP://RootDSE";).Get
("DefaultNamingContext")

'Dispaly Server Name and Default Naming Context
WScript.Echo("")
WScript.Echo("The Computer Name is: " & strServerName)
WScript.Echo("The Domain Naming Context is: " & strDefaultDomainNC)

'Set up the ADO connection required to implement the search.
Set objADOConn = CreateObject("ADODB.Connection")

objADOConn.Provider = "ADsDSOObject"
'Connect using current user credentials
objADOConn.Open "Active Directory Provider"

'Verify successful connection state
If objADOConn.State = adStateOpen Then
Wscript.Echo("")
WScript.Echo("Authentication Successful!")
Else
Wscript.Echo("Authentication Failed.")
Call Cleanup(3)
End If

Set objADOCommand = CreateObject("ADODB.Command")
Set objADOCommand.ActiveConnection = objADOConn

'Format search criteria using SQL syntax
strADSQuery = "SELECT computerName, samAccountName, givenName, sn,
AdsPath FROM 'LDAP:// " & _
strDefaultDomainNC & "' WHERE samAccountName = '" & strSearch & "'"

objADOCommand.CommandText = strADSQuery

'Execute the search
Set objQueryResultSet = objADOCommand.Execute

'Gather and echo some general information about the user
WScript.Echo("")
Wscript.Echo "Info for search criteria " & strSearch
WScript.Echo("")

intCount = 0
While Not objQueryResultSet.EOF
strAdsPath = objQueryResultSet.Fields("AdsPath")
set objFile = objFSO1.OpenTextFile(strFile1, ForAppending, True)
objFile.WriteLine(strAdsPath)
objFile.Close
WScript.Echo("The AdsPath is: " & strAdsPath)
WScript.Echo("")
intCount = intCount + 1
objQueryResultSet.MoveNext
Wend
objADOConn.Close




'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Sub Cleanup
'
' Purpose: Cleanup objects and exit
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub Cleanup(intExitCode)

Set objADOConn = Nothing
Wscript.Quit(intExitCode)

End Sub
--

My output looks like this:

LDAP://CN=TWCOXX01664,OU=THIN CLIENTS,OU=CVC
WORKSTATIONS,DC=CVCHEART,DC=local
LDAP://CN=TWCOXX02510,OU=THIN CLIENTS,OU=CVC
WORKSTATIONS,DC=CVCHEART,DC=local
LDAP://CN=TWCOXX02570,OU=THIN CLIENTS,OU=CVC
WORKSTATIONS,DC=CVCHEART,DC=local

I need it to look like this:

TWCOXX01664
TWCOXX02510
TWCOXX02570
----------------------===================
Your script uses ADO to retrieve the following attributes of computer
objects:

computerName, samAccountName, givenName, sn, AdsPath

There is no such attribute as computerName, so that confuses me. The script
only outputs ADsPath, which is exactly what you see in the output. What you
want is the value of the cn attribute (Common Name of the object). Replace
the list of attributes with simply "cn", then in the final loop retrieve the
value. For example, replace:

strADSQuery = "SELECT computerName, samAccountName, givenName, sn, AdsPath
FROM 'LDAP:// " & _
strDefaultDomainNC & "' WHERE samAccountName = '" & strSearch & "'"

with this:

strADSQuery = "SELECT cn FROM 'LDAP://"; & strDefaultDomainNC & "' " _
& "WHERE sAMAccountName = '" & strSearch & "'"

Then in the loop replace "ADsPath" with "cn" throughout. I assume the rest
of the script is OK, as I have only briefly reviewed the code. I cannot help
but notice, however, that the output file is continually opened and closed,
which is slow and unnecessary. In fact, it would suffice to simply echo the
values to the console (the script should be run at a command console using
the cscript host) and redirected to a text file.

For more on using ADO in VBScript programs to retrieve information, see this
link:

http://www.rlmueller.net/ADOSearchTips.htm

I personally would code the program as follows:
==========
Option Explicit

Dim adoCommand, adoConnection, strBase, strFilter, strAttributes
Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strCN

' 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 computer objects whose Common Name begins with "tw".
strFilter = "(&(objectCategory=computer)(cn=tw*))"

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

' 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.
Do Until adoRecordset.EOF
' Retrieve values and display.
strCN = adoRecordset.Fields("cn").value
Wscript.Echo strCN
' Move to the next record in the recordset.
adoRecordset.MoveNext
Loop

' Clean up.
adoRecordset.Close
adoConnection.Close
===========
If the VBScript program is saved in the file FindComputers.vbs, then I you
could run the following at a command prompt to create the file report.txt:

cscript //nologo FindComputers.vbs > report.txt

My example uses LDAP syntax. ADO supports SQL syntax, but only limited
features that people are familiar with. All queries are actually translasted
to LDAP syntax anyway under the covers.
--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


.



Relevant Pages

  • RE: export user accounts from NT 4.0 domain
    ... ATTENTION THE SCRIPT MUST BE RUNNED FROM A COMPUTER WHERE EXCEL IS ... from the information in a Microsoft Excel spreadsheet. ... Dim strLast, strFirst, strMiddle, strPW, intRow, intCol ... On Error GoTo 0 ...
    (microsoft.public.windows.server.scripting)
  • Re: Password Expire
    ... We have one fron end Edge server in our DMZ which passes email onto two ... I used to schedule a script to run every 24 hours on my Exchange 2003 ... Dim fso, txtarray, BodyText ... Call ProcessFolder (objContainer, numDays) ...
    (microsoft.public.exchange.admin)
  • Re: Collecting output generated via one spreadsheet into a new spreasheet
    ... A quick read of the script indicates that it makes no attempt to actually write out any of the information is has found. ... 'Call Output sub ... I would hazard a guess that if the output contains only the info associated with the last record from the input file, that you are failing to increment the row counter associated with the output spreadsheet after writing to it. ... Dim strUserName, objUserDomain, objGroup, objUser, strGroupList ...
    (microsoft.public.scripting.vbscript)
  • Re: LDAP query information
    ... a "Dim" statement. ... execution of the script. ... ' Filter on computer object. ... ' Construct LDAP syntax query. ...
    (microsoft.public.windows.server.scripting)
  • Re: ADSI Problem
    ... Right I've got the script working now with my ASP applications by passing the ... If you are doing forms authentication using ADSI (which it sounds like you ... Restarting IIS usually gets it working again. ... Dim strUserName ...
    (microsoft.public.windows.server.active_directory)