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)
3
  • 1
    -windowstyle hidden work for you? Or try the second answer here: stackoverflow.com/questions/1802127/… Commented Jun 16, 2021 at 3:19
  • 1
    Try with -WindowStyle Hidden as first parameter. Commented Jun 16, 2021 at 3: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 — stackoverflow.com/questions/10664015/… Commented Jun 16, 2021 at 5:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.