Powershell and Autohotkey v1.1.33.10

I have been struggling with a script for days now trying to get the correct syntax for handing off to Powershell from AutoHotkey correct.

I have the gateway command to powershell working great:

; Get the default gateway IP address using PowerShell
powershellCommand := Get-NetRoute
; Execute the PowerShell command and capture the output to a temporary file
RunWait, PowerShell.exe %powershellCommand% 2> %A_Temp%\PowerShellError.txt > %A_Temp%\PowerShellOutput.txt, , Hide UseErrorLevel

What I am strugglng with is the Test-NetConnection section. I’d like to have a series of ports scanned but right now I just have the array filled with 2 (1 that passes and 1 that fails). If I run my commands manually they work everytime so I know that it is an issue with the way I am handling the variables. I just don’t know what to do.

	; Generate the PowerShell command to test the connection to the port and output to a file
	powershellCommand := "Test-NetConnection -Port " . port . " -ComputerName " . gateway . " | Out-File -FilePath " . A_Temp . "\PowerShellOutput.txt"

	; Execute the PowerShell command
	RunWait, PowerShell.exe %powershellCommand%

Any assistance is greatly welcome!
Thanks,
WeirDave

My entire function/label:

; -----------------------------------------------------------------------------
; PortScanner:
; This section handles the functionality related to scanning common ports and specific ports needed for Meraki equipment.
; -----------------------------------------------------------------------------
PortScanner:
; Update GUI status
Gui, Font, Italic cRed
GuiControl, Font, % hStatus
GuiControl, % hStatus, Scanning ports…

; Create a variable to store the gateway
gateway := ""

; Get the default gateway IP address using PowerShell
powershellCommand := "Get-NetRoute | Where-Object { $_.DestinationPrefix -eq '0.0.0.0/0' } | Select-Object -ExpandProperty NextHop"
; Execute the PowerShell command and capture the output to a temporary file
RunWait, PowerShell.exe "%powershellCommand%" 2> %A_Temp%\PowerShellError.txt > %A_Temp%\PowerShellOutput.txt, , Hide UseErrorLevel

; Read the output from the temporary file
FileRead, gateway, %A_Temp%\PowerShellOutput.txt

; Check if the gateway IP address was successfully retrieved
if (gateway = "") {
	; Update GUI status on failure to get the gateway
	Gui, Font, Italic cRed
	GuiControl, Font, % hStatus
	GuiControl,, % hStatus, Failed to retrieve gateway IP address.
	Sleep, 3000
	return
} else {
	; Update GUI status with the retrieved gateway
	Gui, Font, Bold Norm cGreen
	GuiControl, Font, % hStatus
	GuiControl, , % hStatus, Gateway retrieved: %gateway%! 
	Sleep, 1000
}

; Define the list of ports to scan
ports := [22, 25]  ; Example list of ports to scan, add more as needed

; Initialize a variable to store port scanning results
portResults := ""

; Loop through each port and check its status
for index, port in ports {
	; Update status bar with current port being scanned
	GuiControl,, % hStatus, Scanning port %port%...
	Sleep, 1000
	; Generate the PowerShell command to test the connection to the port
	powershellCommand := "Test-NetConnection -Port " . port . " -ComputerName " . gateway . " | Out-File -FilePath " . A_Temp . "\PowerShellOutput.txt"

	; Execute the PowerShell command and capture the output to a temporary file
	RunWait, PowerShell.exe %powershellCommand%

	; Read the output from the temporary file
	FileRead, OutputVar, %A_Temp%\PowerShellOutput.txt

	; Parse the PowerShell output to determine port status
	if InStr(OutputVar, "TcpTestSucceeded : True") {
		portResults .= "Port " . port . " is open on " . gateway . ".`n"
	} else if InStr(OutputVar, "TcpTestSucceeded : False") {
		portResults .= "Port " . port . " is closed on " . gateway . ".`n"
	}
}

; Update status bar
Gui, Font, Norm cGreen Bold
GuiControl, Font, % hStatus
GuiControl,, % hStatus, Port scanning complete!
SetTimer, ResetStatus, 3000 ; 3 seconds

; Show the port scanning results in a GUI
Gui, New
Gui, Add, Edit, vPortScanResults ReadOnly r10 w400 h300, % portResults
Gui, Show, , Port Scan Results

return

R David Paine III,
Welcome to the forum. :wave:t3:

Since this is a PowerShell forum I’m afraid there won’t be that many people with a lot of experience in AutoHotkey. And my AutoHotkey days are long time gone - sorry.

Have you tried asking for help in the AutoHotkey forum?
https://www.autohotkey.com/boards/

Apart from that you may call

PowerShell.exe /?

to learn how to run PowerShell and pass a command with parameters.

Regardless of all that: It seems like you’re neither an AutoHotkey expert nor PowerShell. I’d recommend to focus on one technology only. That’ll make you life a lot easier. :wink: :love_you_gesture:t3:

Closing as this is out of scope really. it’s really auto- hot key code not PS code.