Trap search error

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
new_user
Posts: 157
Joined: Wed May 30, 2007 7:01 am

Trap search error

Post by new_user »

I was trying to do a quick modification to my script. The script returns the DN of the groups found in a text file. Obviously "On Error.." is not proper, but I keep making mistakes trying to add a trpa here to report if the group name is not found. Looking for help to mod my code. Thank you in advance. On error resume nextDim objFSO, objFile, sourceFile, strLinesourceFile = "groups.txt"Set objFSO = CreateObject("Scripting.FileSystemObject")Set objFile = objFSO.OpenTextFile(sourceFile)Do Until objFile.AtEndOfStream strLine = objFile.ReadLine If Not strLine = "" Then WScript.Echo GetDN(strLine) End IfLoop Function GetDN(strSAcctName) 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") = 10000 objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE objCommand.CommandText = _ "SELECT distinguishedName FROM 'LDAP://DC=Domain,DC=Com' WHERE objectCategory='group' " & _ "AND sAMAccountName='" & strSAcctName & "'" Set objRecordSet = objCommand.Execute GetDN = objRecordSet.Fields("distinguishedName").ValueEnd Function

User avatar
new_user
Posts: 157
Joined: Wed May 30, 2007 7:01 am

Trap search error

Post by new_user »

I was trying to do a quick modification to my script. The script returns the DN of the groups found in a text file. Obviously "On Error.." is not proper, but I keep making mistakes trying to add a trpa here to report if the group name is not found. Looking for help to mod my code. Thank you in advance. On error resume nextDim objFSO, objFile, sourceFile, strLinesourceFile = "groups.txt"Set objFSO = CreateObject("Scripting.FileSystemObject")Set objFile = objFSO.OpenTextFile(sourceFile)Do Until objFile.AtEndOfStream strLine = objFile.ReadLine If Not strLine = "" Then WScript.Echo GetDN(strLine) End IfLoop Function GetDN(strSAcctName) 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") = 10000 objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE objCommand.CommandText = _ "SELECT distinguishedName FROM 'LDAP://DC=Domain,DC=Com' WHERE objectCategory='group' " & _ "AND sAMAccountName='" & strSAcctName & "'" Set objRecordSet = objCommand.Execute GetDN = objRecordSet.Fields("distinguishedName").ValueEnd Function

User avatar
new_user
Posts: 157
Joined: Wed May 30, 2007 7:01 am

Trap search error

Post by new_user »

Thank you I will ad that, and remove the "On Error.." knowing it needed to be removed. Will try to add the above, trying to trap the error and wscript.echo it in place of the output which would be the DN if found.

User avatar
new_user
Posts: 157
Joined: Wed May 30, 2007 7:01 am

Trap search error

Post by new_user »

So I am sure I missed something. Code is below. If I trap the error correctly, it should log it but continue correct. Code is below I am sure I misplaced where you mentioned to place it. Any help is appreciated thank you. 'On error resume nextDim objFSO, objFile, sourceFile, strLinesourceFile = "groups.txt"Set objFSO = CreateObject("Scripting.FileSystemObject")Set objFile = objFSO.OpenTextFile(sourceFile)Do Until objFile.AtEndOfStream strLine = objFile.ReadLine If Not strLine = "" Then WScript.Echo GetDN(strLine) End IfLoop Function GetDN(strSAcctName) 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") = 10000 objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE objCommand.CommandText = _ "SELECT distinguishedName FROM 'LDAP://DC=DOMAIN,DC=COM' WHERE objectCategory='group' " & _ "AND sAMAccountName='" & strSAcctName & "'"On Error Resume NextSet objRecordSet = objCommand.ExecuteIf Err Thenwscript.echo "Not found"End IfOn Error GoTo 0 GetDN = objRecordSet.Fields("distinguishedName").ValueEnd Function

new_user2012-05-02 20:48:35

jvierra
Posts: 14543
Joined: Tue May 22, 2007 9:57 am
Contact:

Trap search error

Post by jvierra »

Code: Select all

	

domain="MYDOM"
samAccountName="someuser"
distinguishedName=GetDN(domain & samAccountName )
if distinguishedName  "" Then MsgBox distinguishedName
	
Function GetDN( nt4Account ) 
	
     Set nto = CreateObject("NameTranslate")
     nto.Init 3, ""
     On Error Resume Next
     nto.Set 3, nt4Account
     If Err Then
          WScript.Echo "ERROR:0x" & Hex(Err.Number)
     Else
           GetDN = nto.Get(1)
     End If
	
End Function
 

User avatar
new_user
Posts: 157
Joined: Wed May 30, 2007 7:01 am

Trap search error

Post by new_user »

For WMI I have that figured out but when searching AD like in this example, searching for DN from a list of group names not so much.

Locked