Morel Posted March 22, 2006 Posted March 22, 2006 Hello Can Some One Help me to Convet This Code To AU3 i have try The VAConvert But i have some Errors expandcollapse popupOption Explicit Dim strCN, objRootDSE, strDNSDomain, objCommand, objConnection Dim strBase, strFilter, strAttributes, strQuery, objRecordSet Dim strDN, strDisplay, strObjectCategory, intIndex ' Check for required argument. If Wscript.Arguments.Count < 1 Then Wscript.Echo "Required argument <Common Name> missing. For example:" _ & vbCrLf & "cscript CommonName.vbs TestUser" Wscript.Quit(0) End If strCN = Wscript.Arguments(0) ' Determine DNS domain name. Set objRootDSE = GetObject("LDAP://RootDSE") strDNSDomain = objRootDSE.Get("defaultNamingContext") ' Use ADO to search Active Directory. Set objCommand = CreateObject("ADODB.Command") Set objConnection = CreateObject("ADODB.Connection") objConnection.Provider = "ADsDSOObject" objConnection.Open "Active Directory Provider" objCommand.ActiveConnection = objConnection strBase = "<LDAP://" & strDNSDomain & ">" strFilter = "(cn=" & strCN & ")" strAttributes = "distinguishedName,objectCategory" strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree" objCommand.CommandText = strQuery objCommand.Properties("Page Size") = 100 objCommand.Properties("Timeout") = 30 objCommand.Properties("Cache Results") = False Set objRecordSet = objCommand.Execute If objRecordSet.EOF Then Wscript.Echo "Object not found with cn=" & strCN Wscript.Quit End If strDisplay = "Object(s) found" Do Until objRecordSet.EOF strDN = objRecordSet.Fields("distinguishedName") strObjectCategory = objRecordSet.Fields("objectCategory") intIndex = InStr(strObjectCategory, "=") strObjectCategory = Mid(strObjectCategory, intIndex + 1) intIndex = InStr(strObjectCategory, ",") strObjectCategory = Mid(strObjectCategory, 1, intIndex - 1) strDisplay = strDisplay & vbCrLf & strDN & " (" _ & strObjectCategory & ")" objRecordSet.MoveNext Loop Wscript.Echo strDisplay ' Clean up. objConnection.Close Set objRootDSE = Nothing Set objCommand = Nothing Set objConnection = Nothing Set objRecordSet = Nothing Thanks Morel
JerryD Posted March 22, 2006 Posted March 22, 2006 Hello Can Some One Help me to Convet This Code To AU3 i have try The VAConvert But i have some Errors Thanks MorelOK, here goes. I'm not on a machine that I can test this on at the moment. If I get time later today I'll check it, but this code passes a Beta Syntax check and theoretically should work. expandcollapse popup#include <Constants.au3> ; Defines common AutoIt variables #include <_COMErrHandler.au3> ; COM Object error handler #include <_ADQuery.au3> AutoItSetOption ( 'MustDeclareVars', 1 ) ; All variables must be declared before using them Dim $strCN, $objRootDSE, $strDNSDomain, $objCommand, $objConnection Dim $strBase, $strFilter, $strAttributes, $strQuery, $objRecordSet Dim $strDN, $strDisplay, $strObjectCategory, $intIndex ;' Check for required argument. If $CmdLine[0] <> 1 Then MsgBox ( 0, '', 'Required argument <Common Name> missing. For example:' & _ @CRLF & @ScriptName & ' TestUser' ) Exit 0 EndIf $strCN = $CmdLine[1] ;' Determine DNS domain name. $objRootDSE = ObjGet("LDAP://RootDSE") $strDNSDomain = $objRootDSE.Get("defaultNamingContext") ;' Use ADO to search Active Directory. $objCommand = ObjCreate("ADODB.Command") $objConnection = ObjCreate("ADODB.Connection") $objConnection.Provider = "ADsDSOObject" $objConnection.Open ("Active Directory Provider") $objCommand.ActiveConnection = $objConnection $strBase = "<LDAP://" & $strDNSDomain & ">" $strFilter = "(cn=" & $strCN & ")" $strAttributes = "distinguishedName,objectCategory" $strQuery = $strBase & ";" & $strFilter & ";" & $strAttributes & ";subtree" $objCommand.CommandText = $strQuery $objCommand.Properties("Page Size") = 100 $objCommand.Properties("Timeout") = 30 $objCommand.Properties("Cache Results") = False $objRecordSet = $objCommand.Execute If $objRecordSet.EOF Then MsgBox ( 0, '', "Object not found with cn=" & $strCN ) Exit EndIf $strDisplay = "Object(s) found" Do $strDN = $objRecordSet.Fields("distinguishedName") $strObjectCategory = $objRecordSet.Fields("objectCategory") $intIndex = StringInStr($strObjectCategory, "=") $strObjectCategory = StringRight($strObjectCategory, Stringlen($strObjectCategory) - $intIndex + 1); !!! MAY NOT BE CORRECT !!! $intIndex = StringInStr($strObjectCategory, ",") $strObjectCategory = StringMid($strObjectCategory, 1, $intIndex - 1); I think this should be right $strDisplay = $strDisplay & @CRLF & $strDN & " (" & _ $strObjectCategory & ")" $objRecordSet.MoveNext Until $objRecordSet.EOF MsgBox ( 0, '', $strDisplay ) ; ' Clean up. ; This shouldn't be needed, but here it is anyway... $objConnection.Close $objRootDSE = '' $objCommand = '' $objConnection = '' $objRecordSet = '' There's one line I'm not sure of the translation which is commented as such. As noted in the comments, I don't think the Clean up section is needed, but is included anyway. You'll see I included two files. One - _COMErrHandler.au3 - is to handle COM errors and prevent the script from crashing. The other - _ADQuery.au3 - isn't actually used in the code, but I thought might be of interest. Both files are attached. I've also attached the above code, but I took the original VB code, commented it out and added the replacement AutoIt code immediately after the VB code line (VB_Conversion_AU3.au3). Oh, and BTW - _ADQuery needs some MAJOR work as it doesn't include any COM error handling. It should #include _COMErrHandler.au3 and doesn't have an #include-once directive like it should. VB_Conversion.au3_ADQuery.au3_COMErrHandler.au3
kpu Posted July 20, 2006 Posted July 20, 2006 JerryD, Awesome job on the AdQuery.au3! I've been fighting with converting this for an hour! Great job! http://www.kpunderground.com
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now