1

I'm writing a python script to make a rule in firewall, its a project for local application prevent my children of watching porn actually. I figure out that I need powershell, so I wrote this script, but still whenever python run a powershell command, powershell windows showing up, any ideas to hide it ?

from __future__ import print_function
from __future__ import print_function
import ctypes, sys
import subprocess

def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False

if not(is_admin()):
    if sys.version_info[0] == 3:
        ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
cmd = 'PowerShell -Executionpolicy byPass -Command New-NetFirewallRule -DisplayName "System_x64" -Enable True -Profile Any -Action Block -Direction Outbound -RemoteAddress "8.8.8.8"'
check = subprocess.run("PowerShell -Executionpolicy byPass -Command Get-NetFirewallRule -DisplayName 'System_x64'", stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if(check.returncode != 0):
    rs = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  • 1
    `-windowstyle hidden` work for you? Or try the second answer here: https://stackoverflow.com/questions/1802127/how-to-run-a-powershell-script-without-displaying-a-window – Abraham Zinala Jun 16 '21 at 03:19
  • 1
    Try with `-WindowStyle Hidden` as first parameter. – Santiago Squarzon Jun 16 '21 at 03:20
  • from what i can tell, you have no need for using python in this case. if you want a no-window run for PoSh, you can do that with something like the `Set objShell` Answer to this Question >>> Set objShell — https://stackoverflow.com/questions/10664015/hide-empty-console-window-in-a-all-gui-powershell-script – Lee_Dailey Jun 16 '21 at 05:26

0 Answers0