Reputation: 11
I created a python program that collects information from active directory. To gather the info, the program runs certain PowerShell scripts to connect to the server.
After debugging seemed to be done, I converted the file into a .exe using pyinstaller -onefile -w. When I run the .exe the gui opens up without issue, but when I try to execute the commands that connect to the active directory server, the program crashes. There is no error code in the console when this happens. After troubleshooting on my own for a minute, I realized that if I create the .exe without the "-w" making the console run in the background, the program works fine. The problem is that I really don't want the console running in the background as this is supposed to be a user-friendly application.
Is there anyway that I can run the program without the console window or at least keep it hidden when the gui is running? Also, apologies if this is a dumb quesion. I am new to programming and even newer to powershell.
p.s. here's a snippet of the python code that runs the PowerShell script:
pscommandUsers = '$users = ' + userNameList + ';'
pscommandHeader = 'foreach ($u in $users) {'
pscommandGroups = '$Group = Get-ADPrincipalGroupMembership -Identity $u | ' \
'Select-Object -ExpandProperty Name | Sort-Object | ft -hidetableheaders;'
pscommandTitles = '$ADProp = Get-ADUser -Identity $u -Properties Department, ' \
'Title | ForEach-Object {$_.Department, $_.Title};'
pscommandFooter = '$Array = $ADProp, $Group; Write-Output "*****" $Array}'
...process = subprocess.Popen(["powershell.exe", pscommand],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output, error_description = process.communicate()
Upvotes: 0
Views: 493
Reputation: 11
Okay, I figured this out. First off, all the things that did not work: -Trying to create the .exe with --no console in pyinstaller and then manipulating subprocess to hide the console. -Trying other options outside of Popen within subprocess -Trying alternative .exe creation procedures within pyinstaller
What finally resolved the issue was dropping pyinstaller altogether and using cx_freeze instead. When creating my .exe with cx_freeze, I was able to use creationflags= subprocess.CREATE_NO_WINDOW within my subprocess routine and create my .exe without any annoying console in the background but also without my program crashing as well.
Upvotes: 1
Reputation: 331
When you spawn the subprocess that executes powershell.exe
, see if you can use the -WindowStyle <WindowStyle>
command-line parameter.
powershell.exe -WindowStyle Hidden -Command <Command(s)>
Documentation:
about_PowerShell_exe
Upvotes: 0