4

Is there a way to run powershell code in python? I've been searching around and all I'm finding is how to run a separate PS script file in python, but nothing about running PS code.

Example: Let's say I want to run this command from inside a Python script\program...

Start-Process -filepath "Path\Filename.exe" -wait

I understand in that example I could just run the file using Python. I'm looking into writing a Python app, but it would use some powershell code behind the scenes to actually do what it needs to do. In the case for my app idea, it would connect to Office 365 and manipulate users\account info (add, remove, etc) and things you can't do on the 365 admin site. I already have a PS script to do most of this, but I'm looking into a GUI interface and python seems to be the better choice for GUI (so far as what I've seen and played with).

If this isn't possible, or if it's more pain than it's worth I'd appreciate some suggestions. Should I look into C# or something like that? Python seemed easier to understand.

Thanks.

1
  • So the general consensus I'm seeing from the answers below is that it's either not possible, or not practical. Is this correct? Commented Jun 28, 2016 at 18:01

3 Answers 3

2

If you Want to embed PowerShell script in exe, vc++ is probably the way to go.

You can use system function in <stdlib> to run commands in cmd

here is the code:

#include "stdafx.h"
#include <stdlib.h>
using namespace std;

int main()
{
    system("@powershell -NoProfile -ExecutionPolicy Bypass <your PSScript path>");
    return 0;
}

but this code will require you have the ps1 file on your local box.


my suggestion is to run if from the remote. host you ps1 file on GitHub or gist

#include "stdafx.h"
#include <stdlib.h>
using namespace std;

int main()
{
    system("@powershell -NoProfile -ExecutionPolicy Bypass -command (new-object net.webclient).downloadstring('<url to your script>")');
    return 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I'll look into that!
1

Just start powershell.exe (untested)

os.system("powershell.exe", "script.ps1")

You may need an additional parameters to specify path and execution policy.

1 Comment

Thanks, Esben. However, it looks like that's just calling a separate powershell script. I'm looking to embed the powershell code inside the python script itself which I'd then convert\package into an EXE file making it a lot easier to share.
0

Couldn't you just write a powershell script file and then call it? (untested)

f = open("script.ps1")
f.write(...) #Script you want to run
f.close()
os.system("powershell.exe", "script.ps1")

1 Comment

Yes, but not for I want to do. Thanks.

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.