powershell objects to Autohotkey

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
haichen
Posts: 631
Joined: 09 Feb 2014, 08:24

powershell objects to Autohotkey

25 Feb 2018, 11:01

Is there a way to work with powershell objects in Autohotkey? convert them to ahkobjects?
Has someone seen some code or has an idea how to do?
RickC
Posts: 299
Joined: 27 Oct 2013, 08:32

Re: powershell objects to Autohotkey

26 Feb 2018, 16:08

If you mean PowerShell cmdlets then it's very easy to wrap them in AHK.

For example:

Code: Select all

psScript =
(
   Stop-Process -ProcessName explorer
)
Run PowerShell.exe -Command %psScript%,, hide
I use this to refresh the Windows desktop.

It's also fairly easy to build queries and redirect output, for example:

Code: Select all

psScript =
(
    Get-NetTCPConnection | ? State -eq 'Established' | ? RemoteAddress -notlike 127* | FT -Autosize | Out-File C:\Support\established-connections.txt
)

; 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%
Run, C:\Support\established-connections.txt

ExitApp
Hope this helps...
haichenatwork

Re: powershell objects to Autohotkey

27 Feb 2018, 03:45

Thank you very much! I found a slightly different solution:

Code: Select all

#include json.ahk ;https://github.com/cocobelgica/AutoHotkey-JSON

ps=Get-Service "s*" | Where-Object {$_.Status -eq 'running'} | Sort-Object status -Descending
svc:=RunPS(ps)

loop, 5
msgbox, % svc[a_index].name
return


RunPS(ps) {
json_str:=ComObjCreate("WScript.Shell").Exec("powershell -command " ps " | ConvertTo-Json").StdOut.ReadAll() ;convert a Powershellobject to a json-string
return JSON.Load(json_str) ;returns an AHK-Object
}
tester
Posts: 84
Joined: 10 Jun 2021, 23:03

Re: powershell objects to Autohotkey

25 Sep 2022, 02:30

haichenatwork wrote:
27 Feb 2018, 03:45
Thank you very much! I found a slightly different solution:

Code: Select all

#include json.ahk ;https://github.com/cocobelgica/AutoHotkey-JSON

ps=Get-Service "s*" | Where-Object {$_.Status -eq 'running'} | Sort-Object status -Descending
svc:=RunPS(ps)

loop, 5
msgbox, % svc[a_index].name
return


RunPS(ps) {
json_str:=ComObjCreate("WScript.Shell").Exec("powershell -command " ps " | ConvertTo-Json").StdOut.ReadAll() ;convert a Powershellobject to a json-string
return JSON.Load(json_str) ;returns an AHK-Object
}
I've tried the posted RunPS() function by passing Get-Process like

Code: Select all

RunPS( "Get-Process" )
It took more than 1 minute to respond. If I manually open PowerShell and type Get-Process, it shows the output within a few seconds, however. Does anybody know why?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Exies, hiahkforum, JoeWinograd, mikeyww and 153 guests