AutoIT meeting Powershell

Both scripting languages have their advandtages.

AutoIT is good at Windows API’s, GUI automation, and more …

Powershell is good at using .Net Classes and more …

So making this a couple would be just great Smile

Well here we go.

This code here will take AU3 output and PS will re-use them by means of objects.

CLS

function Au3_cmd ($sEnv = "env:programfiles(x86)" $sArch = "\AutoIt3\AutoIt3_x64.exe "$sType = " /AutoIt3ExecuteLine ", $sCmd = "" )
{

$sPath = get-content $sEnv

$sPath =  $sPath + $sArch

$oObj = New-Object -ComObject Scripting.FileSystemObject

$sf = $oObj.GetFile($sPath) # GetFile gets the dir ShortPath

    Return $sPath, $sf.ShortPath, $sType, $sCmd
}

$sCmd1 = Au3_cmd -sCmd "InputBox('Question', 'Where were you born?', 'Planet Earth 1', '', -1, -1, 0, 0)" 
$sCmd1 = Au3_cmd -sCmd "InputBox('Question', 'Where were you born?', 'Planet Earth 1', '', -1, -1, 0, 0)"
$sCmd2 = Au3_cmd -sCmd "MsgBox(0,'Au3 => PS Test','Hello World 2')"
$sCmd3 = Au3_cmd -sCmd "ConsoleWrite(@CRLF & 'Hello World 3' & @CRLF)"
$sCmd4 = Au3_cmd -sCmd "ConsoleWrite(@CRLF & 'Hello World 4' & ' - ' & @IPAddress1 & @CRLF)"

invoke-expression  ($sCmd1[1] + $sCmd1[2] + [CHAR]34 + $sCmd1[3] + [CHAR]34) | % { "processing Au3 Output 4 : $_ " } 
invoke-expression  ($sCmd2[1] + $sCmd2[2] + [CHAR]34 + $sCmd2[3] + [CHAR]34) 

# 2 ways to capture the invoke-expression Pipe output 
# 1. Write-Host 2. using the tee-object where objects can be re-used via the pipe.

$Ret3 =  ($sCmd3[1] + $sCmd3[2] + [CHAR]34 + $sCmd3[3] + [CHAR]34) 
invoke-expression  "$Ret3  | Write-Host"

$Ret4 =  ($sCmd4[1] + $sCmd4[2] + [CHAR]34 + $sCmd4[3] + [CHAR]34) 
invoke-expression "$Ret4 | tee-object -variable return" | % { "processing Au3 Output 4 : $_ " }

echo "Number of Objects $($Return.Count) :)" 

# -----------------------------------------------

$sCmd5 = Au3_cmd -sType " /AutoIt3ExecuteScript " -sCmd "C:\_\Apps\_PowerShell\_Au3 & VBScript in PS\test.au3"
$Ret5 = ($sCmd5[1] + $sCmd5[2] + [CHAR]34 + $sCmd5[3] + $sCmd5[4] + [CHAR]34)

Echo ' --- ' # All object as output
invoke-expression  "$Ret5 | Write-Host" 

Echo ' --- ' # All objects moved to a variable and passed on to PS.
invoke-expression "$Ret5 | tee-object -variable script" | % { "processing Au3 Output 5 : $_ " } | Where-object {$_ -notlike '*Idle*'}


rv sCmd1
rv sCmd2
rv sCmd3
rv sCmd4
rv sCmd5

rv ret3
rv ret4
rv ret5

rv script

And call some Windows GUI’s like Msg boxes or Input boxes as well. AutoIT can use command line and script file input.

image

This output comes from AU3 calling some Windows API’s

image

And passes it to PS as Object where we can apply filters to it etc.

image

This is the AU3 script file code.

ConsoleWrite(test() & @CRLF)

Func test()
	$sRet = StringLeft("I am a string", 3)
 Return $sRet
EndFunc

; ------------------------------------------

ConsoleWrite("Idle since " &_IdleTicks() & " MSecs"& @CRLF)

Func _IdleTicks()
    Local $aTSB, $ticksSinceBoot, $struct, $ticksSinceIdle

    $aTSB = DllCall ("kernel32.dll", "long", "GetTickCount")
    $ticksSinceBoot = $aTSB[0]

    $struct = DllStructCreate("uint;dword");
    DllStructSetData($struct, 1, DllStructGetSize($struct));
    DllCall("user32.dll", "int", "GetLastInputInfo", "ptr", DllStructGetPtr($struct))
    $ticksSinceIdle = DllStructGetData($struct, 2)

    return $ticksSinceBoot - $ticksSinceIdle
endfunc

; ------------------------------------------

Dim $idays
Dim $ihours
Dim $imins
Dim $isecs

_uptime($idays,$ihours,$imins,$isecs)

MsgBox(0,"Uptime","Days: " & $idays & " - Hours: " & $ihours & " - mins: " & $imins & " - secs: " & $isecs)

ConsoleWrite("Days: " & $idays & " - Hours: " & $ihours & " - mins: " & $imins & " - secs: " & $isecs & @CRLF)

Func _uptime(ByRef $iDays, ByRef $iHours, ByRef $iMins, ByRef $iSecs)
    $iTicks = DllCall("kernel32.dll", "long", "GetTickCount")
    $iTicks = $iTicks[0]
    If Number($iTicks) > 0 Then
        $iTicks = Round($iTicks / 1000)
        $iDays  = Int($iTicks / 86400)
        $iHours = Int($iTicks / 3600)
        $iTicks = Mod($iTicks, 3600)
        $iMins = Int($iTicks / 60)
        $iSecs = Round(Mod($iTicks, 60))
        Return 1
    ElseIf Number($iTicks) = 0 Then
        $iDays = 0
        $iHours = 0
        $iTicks = 0
        $iMins = 0
        $iSecs = 0
        Return 1
    Else
        SetError(1)
        Return 0
    EndIf
EndFunc   ;==>_uptime

As you can see PS can handle this perfectly for further processing.

Great marriage !

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: