从Python调用PowerShell脚本失败,但可以通过PowerShell运行

时间:2016-10-26 12:11:52

标签: python python-3.x powershell office365

我遇到一个问题,一个简单的PowerShell脚本在从Python调用时无法找到模块 public class MyCutomWidget extends HttpSecureAppServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } ,但是当从CMD / PS运行PowerShell脚本时,它会按预期运行。

当PowerShell脚本独立运行时,它运行得很完美,我觉得Python脚本运行正常,因为它正在引入从PowerShell格式化的错误,因为我想要格式,但它可以'找到模块,我只是不知所措。

调用Powershell脚本并提取输出的Python脚本。

MSOnline

从Office 365 Online中提取用户的PowerShell脚本

import subprocess, sys

def powershell(cmd):
    out = []
    p = subprocess.Popen(["powershell.exe", cmd], stdout=sys.stdout)
    line = p.communicate()
    out.append(line)
    for line in out:
        print(line)

powershell(r"Path to Powershell script")

任何人都可以了解一下,当从Python调用PowerShell脚本时,无法导入Import-Module MSOnline $username = "username" $password = cat C:\Users\user\Documents\folder\securestring.txt | ConvertTo-SecureString $cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $password Connect-MsolService -Credential $cred Get-MsolUser 模块,本地计算机的执行策略设置为“Unrestricted”。

0 个答案:

没有答案