1

I have a python file, which contains a gui 'window.py', a powershell script 'get-aduser.ps1'. Is it possible to bundle these two into one exe?

The window.py file contains a combobox which runs the .ps1 script when run. The combobox is then filled with the output of the Powershell script.

Reason why I want to do this:

My colleagues should run the programm on their client, without having to install python and all the libraries used.

Everyone has the RSAT tools so this shouldnt be a problem when the powershell script is executed.

  • Consider rewriting the GUI part in PowerShell using WinForms - then all you'll need to distribute is a PowerShell script, possibly embedded in a batch file - see https://stackoverflow.com/a/49063524/45375 – mklement0 Nov 27 '18 at 13:44
  • Well, thats not what I want... it may be a way to do this but its not my goal... since I am an apprentice and my executive told me to do build the GUI in python... For example: I can use PS scripts to retrieve information from the AD for filling the comboboxes (since its a simple one liner doing this in PS) instead of hardcoding it into the python GUI or doing it in python. So back to my original question: Is it possible or is there any tool to build an .EXE including the .ps1 and .py GUI? – Filip Stanisic Nov 27 '18 at 14:50
  • The simplest approach may be to embed the PowerShell script's _content_ as a _string_ directly in your Python script and pass it to `powershell.exe -command` – mklement0 Nov 27 '18 at 14:58
  • Oh so you mean `subprocess.call('powershell.exe' , '$pathtoOU\nGet-ADUser 'Name'')`? Would I need to consider \n in the string like in the example? – Filip Stanisic Nov 27 '18 at 15:08
  • The comments aren't the right place to discuss this. Please create a _new_ question focused on this follow-up question, but not before you've experimented yourself and can provide an [MCVE (Minimal, Complete, and Verifiable Example)](http://stackoverflow.com/help/mcve) or something close to it. You're much more likely to get the help you need that way. – mklement0 Nov 27 '18 at 15:11

2 Answers2

1

A generic way to provide python based solutions to consumers without to forcing them to step into the python module stuff is: pyinstaller

mklement0
  • 382,024
  • 64
  • 607
  • 775
woodz
  • 737
  • 6
  • 13
  • Thats what I found already, but could you give me an example how to build the .EXE with the .py file and .ps1 file in it? Do I need to modify anything or just run `pyinstaller C:\Path\to\file.py C:\Path\to\file.ps1`? – Filip Stanisic Nov 27 '18 at 14:54
  • 1
    pyinstaller usually walks through your dependency chain coming with your project. It scans the file imports and resolves compiled (eg. c-based) Libraries. It's only for pure python. The simplest reference to your powershell stuff imho is to specify the path to this outside of the bundle residing script. You likely call it via python's subprocess command / function. Since you didn't specify the logic, how you obtain the output of the script back into the python program, I suggest a lose file-based coupling (a bit odd). Again, some sources and explanation would clarify your concept – woodz Nov 27 '18 at 15:13
  • have a look onto the [manual](https://pyinstaller.readthedocs.io/en/stable/). I suggest to start by simplifying things the first time (without the powershell stuff). Make a simple "hello world" module hw.py and another module with `__main__` mymain.py where you import the hw module. Then call `pyinstaller mymain.py`. Playing around with this should give you a principal understanding – woodz Nov 27 '18 at 15:27
  • Thank you :) posted my answer below – Filip Stanisic Nov 29 '18 at 08:26
0

How I solved the problem:

Im creating the powershell script temporary when the .exe is run instead of trying to pack into one .exe with the python file, like this:

with open("C:\\Temp\\newusers.ps1", 'w', encoding='utf8') as f:
f.write('$s = "OU=,DC=,DC="\n$User = Get-ADUser -Filter * -Properties name, employeeID -Searchbase $s | Select-Object -Property name,employeeID\n$User | Out-File -Encoding utf8 C:\\Users\\USER\\Desktop\\users.txt')`

and after the GUI is closed, I am deleting the .ps1 script:

os.remove('C:\\Temp\\newusers.ps1')

Packing the python file into an .exe.

Step one: Install pyinstaller

pip install pyinstaller

Step two: Use it

pyinstaller -F C:\Path\to\your\file.py