• Be aware of this PowerShell/AutoHotkey gotcha

    Home » Forums » Developers, developers, developers » DevOps Lounge » Be aware of this PowerShell/AutoHotkey gotcha

    Author
    Topic
    #506143

    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:
    44938-ps_gotcha1
    Click to enlarge

    Screenshot of amended AHK script showing reproducible error about a missing curly brace. (It’s not missing!)
    44939-ps_gotcha2
    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…

    Viewing 3 reply threads
    Author
    Replies
    • #1569787

      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 :cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

      • #1569801

        Hi RG… I’ve just tried that. The error doesn’t appear, only the comment line, i.e. the command itself doesn’t execute.

        44940-ps_gotcha3
        Click to enlarge

    • #1571157

      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

    • #1571471

      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).

    • #1571483

      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

      • #1595376

        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
    Viewing 3 reply threads
    Reply To: Be aware of this PowerShell/AutoHotkey gotcha

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: