Jump to content

AutoIT meeting Powershell


ptrex
 Share

Recommended Posts

AutoIT meeting Powershell

Wether you like it or not, but we can't ignore Powershell these days. Since Powershell is built on top of .NET this enables a lot of potential for all scripters around.

Jon made his first steps releasing 2 .NET Assemblies in prior Beta versions. Which is a very good step toward the .NET integration.

In fact there are 2 Assemblies available in the Beta release, a .NET Assembly and a Powershell Assembly.

As it seems the .Net Assembly is derived from the COM AutoItX3.dll.

While the PS Assembly derived from the .NET Assembly, which speaks for itself.

But of course the Method and Properties exposed are limited to the ones in the AutoITX3 COM Object.
 

Therefore we add this handshake in the meantime awaiting that Jon adds full .NET support in AU3 or AU4, just like other Scripting languages have done ?

The approach is simple.

We use the AU3 commandline interface to run AU3 commands or Script Files. And Powershell picks it up to process them as PS Objects.

This PS function reads out the -> PS Code below.

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)
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) 
 
 
$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:\temp\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 passed on to PS for further treatment.
 
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

 

Just some WinAPI Test 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

 

Since both languages have the strenght and weaknesses. AU3 is realy good in WinAPI's / WinGUI's, whick PS is good in accessing .NET Assemblies.

Seems to be a good mariage. :)

Enjoy !

ptrex

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

×
×
  • Create New...