Page 1 of 1

Trap search error

Posted: Wed May 02, 2012 6:28 am
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

Trap search error

Posted: Wed May 02, 2012 6:28 am
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

Trap search error

Posted: Wed May 02, 2012 9:19 am
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.

Trap search error

Posted: Wed May 02, 2012 1:47 pm
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

Trap search error

Posted: Wed May 02, 2012 5:37 pm
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
 

Trap search error

Posted: Thu May 03, 2012 2:11 am
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.