PowerShell: Integrate Invocation of AutoIT Scripts

Although PowerShell is an incredibly powerful automation platform [primarily] for Microsoft Windows systems, it does have some gaps that other tools can fill. One of those tools is called AutoIT, which many of you are probably familiar with prior to reading this. AutoIT can handle GUI automation much more easily than PowerShell, and is therefore a desirable alternative. But, what if you need to combine PowerShell automation and some GUI automation with AutoIT? Wouldn’t it be nice to be able to simply call an AutoIT script from inside your PowerShell script, without having external dependencies to copy around alongside your PowerShell script?

Here is a simple helper function that will execute an AutoIT script in-line with your PowerShell code. I hope this makes it easier to integrate AutoIT with your existing and future PowerShell scripts. Although quite basic in its current form, this function could be enhanced to include support for firing off AutoIT scripts asynchronously, passing parameters into them, accepting output from AutoIT scripts, and so on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<#
    .Synopsis
    This function will invoke an AutoIT script based on an input string.
   
    .Parameter $ScriptText
    The ScriptText parameter is the text of the AutoIT script file that
    will be executed by the function.
   
    .Outputs
    None (for now) :)
   
    .Link
    http://trevorsullivan.net
   
    .Author
    Trevor Sullivan
   
    .Date
    2012-05-25
#>

function Execute-AutoItScript {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string] $ScriptText
    )
   
    # Create a temporary file name
    $TempFileName = '{0}\{1}.au3' -f $env:TEMP, [Guid]::NewGuid().Guid.Replace('-',' ');
    # Write the script text to the AutoIT script file
    [IO.File]::WriteAllText($TempFileName, $ScriptText);
    # Get the path to AutoIT (this could be "dynamicized" :)
    $AutoIt = 'c:\Program Files (x86)\AutoIT3\AutoIT3.exe';
    # Invoke AutoIT with the script path
    Start-Process -FilePath $AutoIt -ArgumentList $TempFileName -Wait;
    # Remove the temporary AutoIT script file
    Remove-Item -Path $TempFileName;
}

# Define the AutoIT script in a PowerShell here-string
$Script = @"
    WinActivate("
XML Notepad")
"
@

# Invoke AutoIT script defined above
Execute-AutoItScript -ScriptText $Script;

PowerShell: Getting an access token from Instagram (oAuth 2.0)

So I’ve recently been struggling with the first step of oAuth 1.0a, which is getting a “request token.” Twitter still uses oAuth 1.0a, and although they have fairly decent documentation on the authentication flow, I’m still having a rough time with it. I had read that supposedly oAuth 2.0 would be easier to work with than oAuth 1.0a, but that didn’t really matter to me since Twitter isn’t using oAuth 2.0 yet.

When push came to shove, and oAuth 1.0a was still giving me headaches, I figured I would just try out oAuth 2.0, to see how easy it was to get an access token. As it turns out, it’s REALLY FREAKIN’ easy, and you don’t have to worry about giving out your application’s consumer secret key (aka. consumer key, client secret, etc.), which is sensitive information. Rather, all you need to do is pass in your callback URL (the one that you configure on your application registration), and the consumer ID (aka. client ID) that the service provides you with.
Continue reading