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