Powershell / AHK Integration

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
arrrghhh
Posts: 11
Joined: 20 Nov 2015, 17:09

Powershell / AHK Integration

27 Oct 2016, 15:07

Hello,

I have built a lot of system management tools into AHK, and I'm realizing that using wmic/comspec is going away partially (if not completely).

In trying to get my scripts to work in powershell, I am having a lot of issues. For example, this first code snippet works:

Code: Select all

Run, %comspec% /k wmic /node:"%line%" process call create 'powershell.exe -NoExit -Command Add-WindowsFeature -IncludeAllSubFeature SNMP-WMI-Provider'
However, if I try to run PS directly and use -ComputerName to direct to a remote node, it fails:

Code: Select all

Run, powershell.exe -NoExit -ComputerName %line% -Command Add-WindowsFeature -IncludeAllSubFeature SNMP-WMI-Provider
Another issue I've noticed - and hopefully this will be resolved if I switch over to pure PS - is I cannot put commas in the PS cmdlet:

Code: Select all

Run, %comspec% /k wmic /node:"%A_LoopField%" process call create 'powershell.exe -NoExit -Command Add-WindowsFeature Web-WebServer`,Web-Common-Http`,Web-Default-Doc'
I made sure to escape the commas with ` (tilde) and this allowed the commas to passthru AHK... but I cannot run the wmic command with commas in it. I suppose I could just run each PS command one at a time, but there are a ton of features I'd like to add - if I could do it all in one line (and provide meaningful output to the end user) that would be ideal.

Thanks!!
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Powershell / AHK Integration

27 Oct 2016, 19:56

Have you tried wrapping parameters in quotes?
For instance:

Run, %comspec% /k ""wmic /node:"%A_LoopField%" process call create 'powershell.exe -NoExit -Command Add-WindowsFeature Web-WebServer`,Web-Common-Http`,Web-Default-Doc""'

If you look at comspec in the Run command docs, there's a breif explaination.
Sorry I can't specifically test this out right now
arrrghhh
Posts: 11
Joined: 20 Nov 2015, 17:09

Re: Powershell / AHK Integration

27 Oct 2016, 23:17

Hm, I will try this...

But my preference would be to eliminate wmic/comspec entirely and just run powershell directly. Is there some reason why the -ComputerName option would screw up PS? If I run the cmdlet without -ComputerName, it runs fine (locally of course).

The output to the end-user is just so much better when you run PS directly. I found that I can also provide the PS output from wmic, but then there are 2 windows created for each command, it's quite ugly (and of course each command is submitted one at a time for now since I can't do commas either).
arrrghhh
Posts: 11
Joined: 20 Nov 2015, 17:09

Re: Powershell / AHK Integration

28 Oct 2016, 13:07

Ok let me re-ask my question.

Why does this work:

Code: Select all

Run powershell.exe -NoExit -Command Get-Hotfix
But this does not:

Code: Select all

Run powershell.exe -NoExit -Command Get-WindowsFeature
I am running this on a Windows 2012R2 server, and when I run the powershell prompt by hand I can do Get-WindowsFeature, Add-WindowsFeature etc... Of course if I send this all thru comspec/wmic, it works (remote servers too) - but the output is awful, and then the system doesn't wait properly, doesn't work with commas etc etc. (I found out the comma issue is more of a wmic/cmd problem than an issue with AHK - I know about escaping the commas).

Edit - the jist, I would like to get away from comspec/wmic and just run everything through powershell - or at least the stuff that I have already in PS...

Thanks!!
JJohnston2
Posts: 204
Joined: 24 Jun 2015, 23:38

Re: Powershell / AHK Integration

30 Oct 2016, 01:52

arrrghhh wrote:I'm realizing that using wmic/comspec is going away partially (if not completely).
Just curious about this... do you have a link by any chance with more info?
JJohnston2
Posts: 204
Joined: 24 Jun 2015, 23:38

Re: Powershell / AHK Integration

30 Oct 2016, 02:04

I don't have the same setup you do but when I run the second command it fails and says it's not recognized as the name of a cmdlet.

When you run the second instance 'manually', are you doing it manually from within PowerShell using Get-Hotfix? or manually from a command prompt via powershell.exe -NoExit -Command Get-WindowsFeature? Do these both behave the same way?--I would expect the AutoHotkey Run results to match behavior from the command prompt.

Wondering if there is a PowerShell-specific path or environment issue related to launching PowerShell via the Run command. For example, could the default path be launching a different version of PowerShell (if that's even possible, I wouldn't really know), or perhaps an instance of PowerShell without appropriate path/environment setup variables pointing it to the Get-WindowsFeature cmdlet.
RickC
Posts: 299
Joined: 27 Oct 2013, 08:32

Re: Powershell / AHK Integration

02 Nov 2016, 14:25

Instead of Get-WindowsFeature or Add-WindowsFeature Web-WebServer you may need to use Get-WindowsOptionalFeature (with IIS-WebServer) instead. I can't duplicate your environment but to generate a list of optional features, including their enabled status, I use the following in Win 10:

Code: Select all

; Note: This will need to be run/compiled with the 64-bit version of AHK
; if it's to be used on a 64-bit version of Windows

; Prompt to 'Run as Admin', i.e. show UAC dialog
If Not A_IsAdmin
{
   Run *RunAs "%A_ScriptFullPath%"  ; Requires v1.0.92.01+
   ExitApp
}

psScript =
(
Get-WindowsOptionalFeature -Online |Sort -Property State |ft
)

; 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%
So, to enable an optional feature you would use something like: Enable-WindowsOptionalFeature –FeatureName IIS-WebServer –Online.

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

Re: Powershell / AHK Integration

15 Nov 2016, 12:52

RickC wrote:Instead of Get-WindowsFeature or Add-WindowsFeature Web-WebServer you may need to use Get-WindowsOptionalFeature (with IIS-WebServer) instead. I can't duplicate your environment but to generate a list of optional features, including their enabled status, I use the following in Win 10:

Code: Select all

; Note: This will need to be run/compiled with the 64-bit version of AHK
; if it's to be used on a 64-bit version of Windows

; Prompt to 'Run as Admin', i.e. show UAC dialog
If Not A_IsAdmin
{
   Run *RunAs "%A_ScriptFullPath%"  ; Requires v1.0.92.01+
   ExitApp
}

psScript =
(
Get-WindowsOptionalFeature -Online |Sort -Property State |ft
)

; 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%
So, to enable an optional feature you would use something like: Enable-WindowsOptionalFeature –FeatureName IIS-WebServer –Online.

Hope this helps...
Oh wow. Looking at your code comment, I just had a light bulb... You mention the 64-bit version of AHK needs to be used. I have been using the 32-bit version because of some character encoding issues. So now I need to use the 64-bit version for powershell, but the 32-bit version for proper output... Well I guess I need to look deeper into using powershell more, as the output in those windows is significantly better.

And re-installing AHK with 64-bit, I can now do a lot of the commands which were previously unavailable. Is there some way around this? I am looking into how this will impact the text output for some other stuff I was doing. I had originally switched from 64 to 32-bit for that reason...
arrrghhh
Posts: 11
Joined: 20 Nov 2015, 17:09

Re: Powershell / AHK Integration

15 Nov 2016, 12:54

JJohnston2 wrote:
arrrghhh wrote:I'm realizing that using wmic/comspec is going away partially (if not completely).
Just curious about this... do you have a link by any chance with more info?
Sorry I don't have anything to prove it - I am just noticing a lot of Windows management is now done via powershell. I'm sure this is because powershell is more powerful - but they are deprecating a lot of the old command line utilities in favor of PS cmdlets. Just look at the cluster commands - cluster.exe was deprecated in 2008R2, and completely removed from 2012 (although you can add it back, the powershell cmdlets work fine).
JJohnston2
Posts: 204
Joined: 24 Jun 2015, 23:38

Re: Powershell / AHK Integration

16 Nov 2016, 01:03

I see. I don't do a lot of work in that arena so was just curious... appreciate your insights. Thanks.
RickC
Posts: 299
Joined: 27 Oct 2013, 08:32

Re: Powershell / AHK Integration

18 Nov 2016, 07:38

arrrghhh wrote:Oh wow. Looking at your code comment, I just had a light bulb... You mention the 64-bit version of AHK needs to be used. I have been using the 32-bit version because of some character encoding issues. So now I need to use the 64-bit version for powershell, but the 32-bit version for proper output... Well I guess I need to look deeper into using powershell more, as the output in those windows is significantly better.

And re-installing AHK with 64-bit, I can now do a lot of the commands which were previously unavailable. Is there some way around this? I am looking into how this will impact the text output for some other stuff I was doing. I had originally switched from 64 to 32-bit for that reason...
Re: "Is there some way around this?"... ROFL. Imagine a sliding scale from 1(totally new to AHK) to 10 (AHK gods and gurus like Lexikos). I'm between 2-4 depending on the task. Sorry but I'm at my limit of being able to advise further. Have a look at Lexikos' reply here: https://autohotkey.com/boards/viewtopic ... 45#p116445

Hope this helps...
Last edited by RickC on 19 Nov 2016, 16:42, edited 1 time in total.
JJohnston2
Posts: 204
Joined: 24 Jun 2015, 23:38

Re: Powershell / AHK Integration

19 Nov 2016, 16:35

And on the previous note about Microsoft transitioning to PowerShell, looks like they just ditched the traditional command prompt for Win 10 and made it a PowerShell prompt
arrrghhh
Posts: 11
Joined: 20 Nov 2015, 17:09

Re: Powershell / AHK Integration

21 Nov 2016, 10:33

RickC wrote:Re: "Is there some way around this?"... ROFL. Imagine a sliding scale from 1(totally new to AHK) to 10 (AHK gods and gurus like Lexikos). I'm between 2-4 depending on the task. Sorry but I'm at my limit of being able to advise further. Have a look at Lexikos' reply here: https://autohotkey.com/boards/viewtopic ... 45#p116445

Hope this helps...
It does! I think I'm about a 2 maybe 3, getting dangerous but still scratching the surface on a lot it seems... I swear I read about this before, perhaps even that thread... and just forgot about the issue entirely until I read that code comment in your snippet.

Thanks again.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], just me, Rohwedder and 155 guests