How to have AutoIT interact with Powershell

Both languages have there advantages !

AutoIT can easily interact with Windows API’s and COM.

PowerShell can get access to .NET and more.

So if we could get both to communicate together that would be nice Smile

Here we go :

CLS

C:\PROGRA~2\AutoIt3\AutoIt3.exe /AutoIt3ExecuteLine "ConsoleWrite('Hello World' & ' ' & @IPAddress1)" `
                        | % { "processing Au3 Output : $_ " } 

Echo "Filter Object 1"

C:\PROGRA~2\AutoIt3\AutoIt3.exe /AutoIt3ExecuteScript "C:\_\Apps\test.au3"  `
                        |  % { "processing Au3 Output : $_ " } | Where-object {$_ -notlike '*Idle*'}

Echo "Filter Object 2"

C:\PROGRA~2\AutoIt3\AutoIt3.exe /AutoIt3ExecuteScript "C:\_\Apps\test.au3" `
                         | Where-object {$_ -like '*Idle*'}
Output here

image

This is the Test.au3 Script. Save it before running the above PS Script.

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

Enjoy !

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: