I'm trying to get the wireless debugging port of ADB in my Android, using the command here:
& "D:\Tools\Nmap\nmap.exe" -T4 192.168.2.20 -p 37000-44000 | Where-Object {$_ -match "tcp open"} | ForEach-Object {$_.split("/")[0]}
And I would like to make a Python script for further purposes:
ip = '192.168.2.20'
nmap_path = r'D:\Tools\Nmap\nmap.exe'
def get_port():
port_result = subprocess.run(
f'& "{nmap_path}" -T4 {ip} -p 37000-44000 | '
f'Where-Object {{$_ -match "tcp open"}} | '
f'ForEach-Object {{$_.split("/")[0]}}',
shell=True
)
port = port_result.stdout.decode('utf-8').strip()
return port
But it gave the following error: & was unexpected at this time., which indicated that the command was run in CMD instead of PowerShell.

I do not want to use powershell -Command, nor saving the commands into a .ps1 file.
Could I make subprocess.run to run specifically in PowerShell?
You can provide the path to powershell with the executable option of run.
powershell_path = 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe'
subprocess.run(
'echo This is simpler',
shell=True,
executable=powershell_path,
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With