有没有办法在python中运行powershell代码
有没有办法在python中运行powershell代码?我一直在四处搜索,我发现的只是如何在python中运行单独的PS脚本文件,而与运行PS代码无关 示例:假设我想在Python脚本\程序中运行此命令有没有办法在python中运行powershell代码,python,powershell,Python,Powershell,有没有办法在python中运行powershell代码?我一直在四处搜索,我发现的只是如何在python中运行单独的PS脚本文件,而与运行PS代码无关 示例:假设我想在Python脚本\程序中运行此命令 Start-Process -filepath "Path\Filename.exe" -wait 我知道在那个示例中,我可以使用Python运行该文件。我正在考虑编写一个Python应用程序,但它会在幕后使用一些powershell代码来实际完成它需要做的事情。在我的应用程序idea中,它将
Start-Process -filepath "Path\Filename.exe" -wait
我知道在那个示例中,我可以使用Python运行该文件。我正在考虑编写一个Python应用程序,但它会在幕后使用一些powershell代码来实际完成它需要做的事情。在我的应用程序idea中,它将连接到Office 365,并操纵用户\帐户信息(添加、删除等)以及您在365管理站点上无法执行的操作。我已经有了一个PS脚本来完成大部分工作,但我正在研究GUI界面,python似乎是GUI的更好选择(就我所看到和使用的情况而言)
如果这是不可能的,或者如果它比它的价值更痛苦,我会感激一些建议。我应该看看C#或类似的东西吗?Python似乎更容易理解
谢谢。只需启动powershell.exe(未测试)
您可能需要一个附加参数来指定路径和执行策略。如果您想在exe中嵌入PowerShell脚本,vc++可能是最好的选择 您可以使用
中的系统
功能在cmd中运行命令
代码如下:
#include "stdafx.h"
#include <stdlib.h>
using namespace std;
int main()
{
system("@powershell -NoProfile -ExecutionPolicy Bypass <your PSScript path>");
return 0;
}
难道你不能写一个powershell脚本文件,然后调用它吗?(未经测试)
谢谢你,埃斯本。但是,看起来这只是调用一个单独的powershell脚本。我希望将powershell代码嵌入python脚本本身,然后将\package转换为EXE文件,使其更易于共享。是的,但不是因为我想这样做。谢谢,谢谢,我会调查的!因此,我从下面的答案中看到的普遍共识是,这要么是不可能的,要么是不可行的。这是正确的吗?
#include "stdafx.h"
#include <stdlib.h>
using namespace std;
int main()
{
system("@powershell -NoProfile -ExecutionPolicy Bypass <your PSScript path>");
return 0;
}
#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;
}
f = open("script.ps1")
f.write(...) #Script you want to run
f.close()
os.system("powershell.exe", "script.ps1")