Results 1 to 7 of 7
  1. #1
    Administrator Rick Corbett's Avatar
    Join Date
    Dec 2009
    Location
    South Glos., UK
    Posts
    3,216
    Thanks
    141
    Thanked 847 Times in 682 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,949
    Thanks
    421
    Thanked 1,605 Times in 1,449 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
    Administrator Rick Corbett's Avatar
    Join Date
    Dec 2009
    Location
    South Glos., UK
    Posts
    3,216
    Thanks
    141
    Thanked 847 Times in 682 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
    35
    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
    Administrator Rick Corbett's Avatar
    Join Date
    Dec 2009
    Location
    South Glos., UK
    Posts
    3,216
    Thanks
    141
    Thanked 847 Times in 682 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 08:45.

  7. #6
    Lounger
    Join Date
    Dec 2009
    Location
    Gillingham, Dorset, UK
    Posts
    35
    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

  8. #7
    New Lounger
    Join Date
    May 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Set-ExecutionPolicy to make sure you can call powershell scripts

    I've found this to be a simple way to insure you can execute the script:

    Code:
    Runwait powershell.exe Set-ExecutionPolicy Unrestricted
    Runwait powershell.exe yourscript.ps1
    Runwait powershell.exe Set-ExecutionPolicy Restricted
    Last edited by pro450; 2017-05-22 at 16:54.

Posting Permissions

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