Jump to content

Passing a result from vbs to AutoIT3


jefhal
 Share

Recommended Posts

I'm trying to get the Active Directory "location" field from the vbs where I find it to AutoIT3 where I want to use it.

When I try AutoIT's stdout function with my run command the retrieved value, $rawloc, is empty.

Here's the code in vbs that works and outputs either using wscript.echo or wscript.stdout (with cscript). I run it under csript.exe from AutoIT:

'---------------------------------------------------------------
' Returns the name and location for all the computer accounts in 
' Active Directory.
'--------------------------------------------------------------- 
Dim oArgs
Set oArgs = WScript.Arguments
dn = oArgs(0)
rem wscript.echo dn
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.CommandText = "Select Name, Location from 'LDAP://" & dn & "' where objectClass='computer'"

rem objCommand.CommandText = "Select Name, Location from 'LDAP://DC=fabrikam,DC=com' " & "where objectClass='computer'"  

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 30 
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
objCommand.Properties("Cache Results") = False 
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
    rem Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value
    strLocation = objRecordSet.Fields("Location").Value
    rem wscript.Echo "Location: " & strLocation
    Wscript.StdOut.WriteLine strLocation
    objRecordSet.MoveNext
Loop

Here's the AutoIT script that tries to get it:

#include <array.au3>
#include <DellRelatedFunctions.au3>
#include <constants.au3>
;Make Active Directory Name
dim $esc, $macids, $model, $NewDesc, $newdesc, $OrigDesc, $sertag, $strTAG, $rawloc
dim $onlymacsdim 
dim $pingreply[4] = ["Host is offline","Host is unreachable","Bad destination","Unknown error"]   

global $strComputer; = "sys0258"
ToolTip("Starting")
HotKeySet("{ESC}","_terminate")
Opt("TrayIconDebug", 1) 
if $CmdLine[0] = "" Then
    $strComputer = InputBox("Enter System Name", "Please enter system name","localhost")
Else
    $strComputer = $CmdLine[1]
EndIf   

$ping = Ping($strComputer,500)
ToolTip("Pinging strcomputer")
If $ping Then; if $var was not equal to zero --- also possible: If @error = 0 Then ...
Else
    Msgbox(0,"Status",$pingreply[@error],2)
Exit
EndIf
dim $out = ""
    $scope = '"ou=SHS",dc=Locale39,dc=webname,dc=com'
;MsgBox(1,"Scope =", $scope)
    $pid = run(@comspec & " /c adfind -b " & $scope & ' -dn -f name=' & $strComputer,@TempDir,"",$stdout_child)
    Sleep(2000)
    While 1
    $out &= StdoutRead($pid)
    If @error = -1 Then ExitLoop
   ;MsgBox(0, "STDOUT read:", $out)
Wend
; THE ABOVE STDOUT WORKS FINE
;MsgBox(1,"$out=",$out)


$dnright = StringTrimLeft($out,StringInStr($out,"dn:")+2)
$dn = StringLeft($dnright,StringInStr($dnright,@lf)-2)
MsgBox(1,"DN=",$dn)

; THE FOLLOWING STDOUT DOES NOT GET THE OUTPUT OF THE VBS SCRIPT. It appears in the dos box that pops up, but does not go into the stdout below:
$pid = Run(@comspec & ' /c cscript.exe \\server\users$\me\vbs\findallcomputerlocations.vbs "' & $dn & '" ,@TempDir,"",$stdout_child')
;Sleep(5000)
    While 1
    $rawloc &= StdoutRead($pid)
    If @error = -1 Then ExitLoop
   ;MsgBox(0, "STDOUT read:", $out)
Wend
_ArrayDisplay($rawloc,"$rawloc")
MsgBox(1,"$rawloc=",$rawloc)

;run(@comspec & ' /c admod -b ' & $dn & ' "location::' & $description & '"',@TempDir,"",@SW_HIDE)

Exit

Func _terminate()
exit 0
EndFunc

The result is that $rawloc is empty.

Is it because cscript does not send stdout the way that AutoIT expects to receive it?

Is there a better or different way to get strLocation from vbs to AutoIT?

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

just rewrite the vbs to autoit code?

1. I just found my stupid typo in the second instance of stdout_child! There is no ' after stdout_child, is there?

2. I'd still like to see it written inside of AutoIT, but don't have a clue where to start. Could sOuter help a poor "art student" find his way?

B)

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

1. I just found my stupid typo in the second instance of stdout_child! There is no ' after stdout_child, is there?

2. I'd still like to see it written inside of AutoIT, but don't have a clue where to start. Could sOuter help a poor "art student" find his way?

B)

Here's a translation of part of it to get you started... except for the enumeration, you should be able to piece it together from here. If you can't find the enumerated value, let me know.

Dale

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

$objCommand.CommandText = "Select Name, Location from 'LDAP://" & dn & "' where objectClass='computer'"

$objCommand.Properties("Page Size") = 1000
$objCommand.Properties("Timeout") = 30 
$objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE; <---- This is an enumerated value AutoIt won't know -- you need the integer
$objCommand.Properties("Cache Results") = False 
$objRecordSet = $objCommand.Execute
$objRecordSet.MoveFirst

Edit:

Here are the enumeration values from here:

ADS_SCOPEENUM
The ADS_SCOPEENUM enumeration specifies the scope of a directory search.

typedef enum 
{
  ADS_SCOPE_BASE = 0,
  ADS_SCOPE_ONELEVEL = 1,
  ADS_SCOPE_SUBTREE = 2
} ADS_SCOPEENUM;
Edited by DaleHohm

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

Here's a translation of part of it to get you started...

Thanks Dale- I'll give it a try at work today. It looks obvious after you see it done, but it's a bit daunting starting from scratch. I'm not sure I found the references that Randallc had suggested, but I found some. Together with your code above, I should be able to get it working. Thanks to all...
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

Here's a translation of part of it to get you started...

Thanks again, Dale!

Here's what I ended up with that works in real life. Replace $scope with the DN of your organization. This code can be run as an exe from the command line, from Active Directory taskpad, or from AutoIT. You need to get ADMOD and ADFIND from www.joeware.com:

; Replace $scope with your own DN for your organization
#include <array.au3>
#include <DellRelatedFunctions.au3>
#include <constants.au3>
;Make Active Directory Name
dim $esc, $macids, $model, $NewDesc, $newdesc, $OrigDesc, $sertag, $strTAG, $rawloc, $realLocation
dim $onlymacsdim 
dim $pingreply[4] = ["Host is offline","Host is unreachable","Bad destination","Unknown error"]   

global $strComputer 
ToolTip("Starting")
HotKeySet("{ESC}","_terminate")
Opt("TrayIconDebug", 1) 
if $CmdLine[0] = "" Then
    $strComputer = InputBox("Enter System Name", "Please enter system name","localhost")
Else
    $strComputer = $CmdLine[1]
EndIf   

$ping = Ping($strComputer,500)
ToolTip("Pinging strcomputer")
If $ping Then; if $var was not equal to zero --- also possible: If @error = 0 Then ...
    MsgBox(1,"Found","Machine is online!",2)
Else
    Msgbox(0,"Status",$pingreply[@error],2)
Exit
EndIf
dim $out = ""
    $scope = '"ou=Business",dc=domain,dc=domain,dc=com'
;MsgBox(1,"Scope =", $scope)
    $pid = run(@comspec & " /c adfind -b " & $scope & ' -dn -f name=' & $strComputer,@TempDir,"",$stdout_child)
    Sleep(2000)
    While 1
    $out &= StdoutRead($pid)
    If @error = -1 Then ExitLoop
   ;MsgBox(0, "STDOUT read:", $out)
Wend
;MsgBox(1,"$out=",$out)
$dnright = StringTrimLeft($out,StringInStr($out,"dn:")+2)
$dn = StringLeft($dnright,StringInStr($dnright,@lf)-2)
MsgBox(1,"DN=",$dn)
$strLocation = _getADFieldLocation($dn)
MsgBox(1,"$strLocation=",$strLocation)

$realLocation = InputBox("Where are you?","What room are you in?",$strLocation)
if $realLocation = $strLocation Then
    MsgBox(1,"Done","Room is correct",2)
    Exit
Else
    run(@comspec & ' /c admod -b "' & $dn & '" "location::' & $realLocation & '"',@TempDir,"",@SW_HIDE)
EndIf
Exit
Func _terminate()
    exit 0
EndFunc

Func _getADFieldLocation($dn)
dim $ADS_SCOPE_SUBTREE = 2
$objConnection = ObjCreate("ADODB.Connection")
$objCommand =   ObjCreate("ADODB.Command")
$objConnection.Provider = "ADsDSOObject"
$objConnection.Open ("Active Directory Provider")
$objCommand.ActiveConnection = $objConnection
$objCommand.CommandText = "Select Name, Location from 'LDAP://" & $dn & "' where objectClass='computer'"
$objCommand.Properties("Page Size") = 1000
$objCommand.Properties("Timeout") = 30 
$objCommand.Properties("Searchscope") = $ADS_SCOPE_SUBTREE; <---- This is an enumerated value AutoIt won't know -- you need the integer
$objCommand.Properties("Cache Results") = False 
$objRecordSet = $objCommand.Execute
$objRecordSet.MoveFirst
Do 
    $strLocation = $objRecordSet.Fields("Location").Value
;MsgBox(1,"$strLocation=",$strLocation)
    $objRecordSet.MoveNext
Until $objRecordSet.EOF
Return $strLocation
EndFunc
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...