0

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()
Gray
  • 11
  • 2
  • 1
    ***'I created a python program that collects information from Active Directory. *** Just curious... Why? PS is specifically designed to deal with all things ADDS (RSAT or ADSI) and more. Write UX/UI for PowerShell using WinForms/WPF is a straightforward thing. – postanote Jun 22 '21 at 01:09
  • Hey Postanote, good question. The reason why Im using python instead of purely PowerShell is because this specific tool is one of many tools that are incorporated into a larger program. The Program interacts with servers, manipulates data, and provides other utilites. – Gray Jun 22 '21 at 01:25

2 Answers2

1

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.

Gray
  • 11
  • 2
0

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

D-squared
  • 311
  • 1
  • 6
  • Hey D-squared, thanks for the response. So, I went through the subprocess module and couldn't find the WindowStyle property. I also tried adding it to the string as: ["powershell.exe -WindowStyle Hidden", pscommand] but python didnt like that either. Am I doing this correctly? Forgive me again if im being dumb, I have very little experience with PowerShell, much less subprocess. – Gray Jun 22 '21 at 01:13
  • Try enclosing the WindowStyle parameter in its own set of double quotes: `["powershell.exe", "-WindowStyle Hidden", pscommand]` – D-squared Jun 22 '21 at 22:57
  • If that doesn't work you can also try the following: `["powershell.exe", "-WindowStyle Hidden", "-NoLogo", "-NonInteractive", pscommand]` – D-squared Jun 22 '21 at 23:00
  • Also came across these posts that you might find helpful: [link](https://stackoverflow.com/questions/1016384/cross-platform-subprocess-with-hidden-window); [link](https://stackoverflow.com/questions/25122823/hide-console-with-subprocess-popen) – D-squared Jun 22 '21 at 23:03