TIP: PowerShell comments in AHK

Put simple Tips and Tricks that are not entire Tutorials in this forum
RickC
Posts: 187
Joined: 27 Oct 2013, 08:32

TIP: PowerShell comments in AHK

08 Jul 2016, 17:23

This is a quick tip: Do not end any PowerShell code block with a comment line.

As an example, the following AHK script works perfectly:

Code: [Select all] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus


However, the following AHK script fails with a "Missing closing '}' in statement block." error (even though there's no missing end curly brace):

Code: [Select all] [Download] GeSHi © Codebox Plus

psScript =
(
Get-HotFix | Select HotFixID,InstalledOn,InstalledBy -First 1
# This is also a comment but creates an error because it's the final line of the PowerShell code block!
)

; 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%}

Hope this helps... it took me a while to find the reason for the error.
arrrghhh
Posts: 11
Joined: 20 Nov 2015, 17:09

Re: TIP: PowerShell comments in AHK

13 Oct 2016, 15:18

OK, stupid question which is somewhat related to your post...

How do I run 'any' PowerShell cmdlet? I used your code, and I am able to run the "Get-Hotfix" cmdlet, the pipe with select, no problem. However, I would like to run a different cmdlet - Install-WindowsFeature. If I run this from its own powershell window, no problem. If I run the PS from within AHK, I get failures. I even tried to just have an inputbox for the command so I could play with what is happening here, and I still can't quite figure out why Get-Hotfix works great, but Install-WindowsFeature doesn't.

Code: [Select all] [Download] GeSHi © Codebox Plus

Install-WindowsFeature : The Term 'Install-WindowsFeature' is not recognized as the name of a cmdlet, function, script file, or operable program.  Check the spelling of the name, or if a path was included, verify that the path is correct and try again.


Any tips? Thanks!
RickC
Posts: 187
Joined: 27 Oct 2013, 08:32

Re: TIP: PowerShell comments in AHK

14 Oct 2016, 13:10

arrrghhh wrote:OK, stupid question which is somewhat related to your post...

How do I run 'any' PowerShell cmdlet? I used your code, and I am able to run the "Get-Hotfix" cmdlet, the pipe with select, no problem. However, I would like to run a different cmdlet - Install-WindowsFeature. If I run this from its own powershell window, no problem. If I run the PS from within AHK, I get failures. I even tried to just have an inputbox for the command so I could play with what is happening here, and I still can't quite figure out why Get-Hotfix works great, but Install-WindowsFeature doesn't.

Code: [Select all] [Download] GeSHi © Codebox Plus

Install-WindowsFeature : The Term 'Install-WindowsFeature' is not recognized as the name of a cmdlet, function, script file, or operable program.  Check the spelling of the name, or if a path was included, verify that the path is correct and try again.


Any tips? Thanks!


I've just tried to run Install-WindowsFeature -Name Web-Server -IncludeAllSubFeature -WhatIf at a PowerShell command prompt in Win 10 AU and get the message that the cmdlet isn't recognised.

I could be wrong but AFAIK the cmdlet is not (yet) available in Windows 10. This TechNet article (https://technet.microsoft.com/en-us/library/jj205467%28v=wps.630%29.aspx?f=255&MSPPError=-2147217396) suggests it's only available in Server 2012 R2/Win 8.1. Have a look at this article for more info: http://serverfault.com/questions/713187/powershell-install-windowsfeature-and-family-missing-on-windows-10.

It looks like you may need to use Enable-WindowsOptionalFeature instead which IS available for Win 10. (See https://technet.microsoft.com/en-gb/library/dn376466(v=wps.640).aspx)

EDIT: It looks like you should also be able to use Get-WindowsOptionalFeature as well. Have a look at this excellent article that explains what commands are available per Windows version: https://peter.hahndorf.eu/blog/WindowsFeatureViaCmd.html. I wish I had found it a lot earlier.

Hope this helps...
arrrghhh
Posts: 11
Joined: 20 Nov 2015, 17:09

Re: TIP: PowerShell comments in AHK

17 Oct 2016, 21:14

RickC wrote:I've just tried to run Install-WindowsFeature -Name Web-Server -IncludeAllSubFeature -WhatIf at a PowerShell command prompt in Win 10 AU and get the message that the cmdlet isn't recognised.

I could be wrong but AFAIK the cmdlet is not (yet) available in Windows 10. This TechNet article (https://technet.microsoft.com/en-us/library/jj205467%28v=wps.630%29.aspx?f=255&MSPPError=-2147217396) suggests it's only available in Server 2012 R2/Win 8.1. Have a look at this article for more info: http://serverfault.com/questions/713187/powershell-install-windowsfeature-and-family-missing-on-windows-10.

It looks like you may need to use Enable-WindowsOptionalFeature instead which IS available for Win 10. (See https://technet.microsoft.com/en-gb/library/dn376466(v=wps.640).aspx)

EDIT: It looks like you should also be able to use Get-WindowsOptionalFeature as well. Have a look at this excellent article that explains what commands are available per Windows version: https://peter.hahndorf.eu/blog/WindowsFeatureViaCmd.html. I wish I had found it a lot earlier.

Hope this helps...


I do appreciate this tip, as I was testing the script occasionally on my local machine, but the script is designed to run in a Server 2012 (R2) environment. I will definitely stop testing it on my local machine knowing that is a bad test locally... To be honest I'm not sure if my question was posted from my machine or the environment it will be utilized in, so I apologize and appreciate the thoroughness of your response.

But I am still curious about running powershell scripts here. I think I found a way, using comspec/wmic... but then I get 0 feedback unless the command is run locally.

This works:

Code: [Select all] [Download] GeSHi © Codebox Plus

Run, %comspec% /k wmic /node:"%line%" process call create 'powershell.exe -NoExit -Command Add-WindowsFeature -IncludeAllSubFeature SNMP-WMI-Provider'


But this does not:

Code: [Select all] [Download] GeSHi © Codebox Plus

Run, powershell.exe -NoExit -ComputerName %line% -Command Add-WindowsFeature -IncludeAllSubFeature SNMP-WMI-Provider


Basically I am trying to run powershell cmdlets on remote machines, and provide the stdout to the user if they require it. The comspec method does seem to work finally, but of course I get nothing on the machine for feedback this way.

What seems to be the source of the problem is this -ComputerName part, I found I can run most of it locally (Get-Hotfix at least works locally...) So perhaps the more specific question, how do I run these powershell cmdlets against remote machines...

I suppose in the short term I can continue to use comspec and wmic, but I would much prefer to use powershell directly - so I can provide the stdout to the end-user easily if needed (and actually have it be useful).

Thanks!
RickC
Posts: 187
Joined: 27 Oct 2013, 08:32

Re: TIP: PowerShell comments in AHK

18 Oct 2016, 17:33

At a guess, your local environment needs to be Win 8.1 or later in order to test PowerShell commands wrapped in AHK that you want to run against Server 2012 R2.

As I found out to my cost, the version of PowerShell installed on the development device is often the difference between success or failure.

As for running PS scripts wrapped in AHK against remote devices, I think you need to create a post in the Ask For Help forum 'cos I'm just an amateur posting about my experiences.
arrrghhh
Posts: 11
Joined: 20 Nov 2015, 17:09

Re: TIP: PowerShell comments in AHK

27 Oct 2016, 14:31

No worries, I'll post a new thread - didn't mean to steal yours, you just seemed to know your stuff about powershell & AHK.

As for testing, I always test the scripts in a Server 2012 R2 environment now, to rule out any environmental issues. I always did final testing on an actual server environment, but I do sometimes break down issues and test locally to see what in particular is failing.

Thanks again.

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 1 guest