Results 1 to 6 of 6
  1. #1
    Super Moderator Rick Corbett's Avatar
    Join Date
    Dec 2009
    Location
    South Glos., UK
    Posts
    2,143
    Thanks
    101
    Thanked 580 Times in 464 Posts

    Be aware of this PowerShell/AutoHotkey gotcha

    I tend to do most of my automation using AutoHotkey, for example to automate tweaks to new installs of Windows. Windows 10 has made things more difficult (deliberate obfuscation, in my opinion) so there are times when only PowerShell can be used, for example to automate the uninstallation of Windows 10s built-in apps.

    Whilst I was testing an AutoHotkey script that tweaks a new install of Windows 10 I discovered a gotcha which had me beaten for a while. The following will demonstrate what I ran into.

    Screenshot of AHK script calling PowerShell with successful result:
    ps_gotcha1.png
    Click to enlarge

    Screenshot of amended AHK script showing reproducible error about a missing curly brace. (It's not missing!)
    ps_gotcha2.png
    Click to enlarge

    The only difference is that I added a comment to the end of the PowerShell section.

    Note that error cannot be reproduced in the PowerShell ISE or just running a PowerShell (.PS1) script instead.

    Hope this helps if anyone else uses AutoHotkey with Powershell...

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Rick,

    I don't have AutoHotKey but out of curiosity give this a try.

    psScript =
    (
    {
    Your code lines here
    }
    )

    That will turn the code & comments into a Script Block which PS may interpret differently.

    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. #3
    Super Moderator Rick Corbett's Avatar
    Join Date
    Dec 2009
    Location
    South Glos., UK
    Posts
    2,143
    Thanks
    101
    Thanked 580 Times in 464 Posts
    Hi RG... I've just tried that. The error doesn't appear, only the comment line, i.e. the command itself doesn't execute.

    ps_gotcha3.png
    Click to enlarge

  4. #4
    Lounger
    Join Date
    Dec 2009
    Location
    Gillingham, Dorset, UK
    Posts
    30
    Thanks
    0
    Thanked 13 Times in 11 Posts
    Rick,

    Removing the ampersand as well as the braces produces the expected result.

    i.e. Instead of Run powershell.exe -NoExit -Command & {%psScript%}
    change it to Run powershell.exe -NoExit -Command %psScript%

    HTH

  5. The Following User Says Thank You to Cliff.H For This Useful Post:

    Rick Corbett (2016-07-24)

  6. #5
    Super Moderator Rick Corbett's Avatar
    Join Date
    Dec 2009
    Location
    South Glos., UK
    Posts
    2,143
    Thanks
    101
    Thanked 580 Times in 464 Posts
    Hi Cliff,

    Hmmm, you're right. Thank you. And it works even when there's a comment after the PS commandline, provided I don't try to change the PS code into a code block.

    This works:
    Code:
    psScript =
    (
        Get-HotFix | Select HotFixID,InstalledOn,InstalledBy -First 1
        # This is a comment
    )
    
    ; Use this call if you don't want to see PowerShell output
    ;RunWait PowerShell.exe -Command &{%psScript%} ,, hide
    
    ; Use this call if you want to see PowerShell output
    Run powershell.exe -NoExit -Command %psScript%
    This doesn't work:
    Code:
    psScript =
    (
    {
        Get-HotFix | Select HotFixID,InstalledOn,InstalledBy -First 1
        # This is a comment
    }
    )
    
    ; Use this call if you don't want to see PowerShell output
    ;RunWait PowerShell.exe -Command &{%psScript%} ,, hide
    
    ; Use this call if you want to see PowerShell output
    Run powershell.exe -NoExit -Command %psScript%
    I also noticed that PowerShell 5 (Windows 10) adds a comment to the top of the results re: "Cannot load PSReadline module. Console is running without PSReadline." This hasn't happened with PowerShell 2 (Win 7).
    Last edited by Rick Corbett; 2016-07-24 at 09:45.

  7. #6
    Lounger
    Join Date
    Dec 2009
    Location
    Gillingham, Dorset, UK
    Posts
    30
    Thanks
    0
    Thanked 13 Times in 11 Posts
    Rick,

    If this is a new installation of Win10, check the ExecutionPolicy setting. It is probably set to Restricted or AllSigned. If so, change it to something a bit less restrictive.

    Regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •