简体   繁体   English

使用 Python 运行 PowerShell 脚本时出现未知的 cmdlet 错误

[英]Unknown cmdlets error when running PowerShell script with Python

I have a PS script that creates a new user in Office 365. I'm using the MSOnline and AzureAD modules in order to perform everything I need.我有一个在 Office 365 中创建新用户的 PS 脚本。我使用 MSOnline 和 AzureAD 模块来执行我需要的一切。

I need to run the PS script from my Python script but it doesn't work, I get the "not recognized cmdlet" error.我需要从我的 Python 脚本运行 PS 脚本,但它不起作用,我收到“无法识别的 cmdlet”错误。

The PS script is pretty basic, it fails at the connection to the Msol service and just continues on as if everything is ok. PS 脚本非常基本,它在连接到 Msol 服务时失败,只是继续,好像一切正​​常。

$LiveCred = New-Object System.Management.Automation.PSCredential $Admin_User, $Admin_Password
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $LiveCred -Authentication Basic -AllowRedirection
Import-PsSession $Session -AllowClobber
Connect-MsolService -Credential $LiveCred
Connect-AzureAD -Credential $LiveCred

I tried adding import-module for both MSOnline and AzureAD but it didn't work.我尝试为 MSOnline 和 AzureAD 添加import-module ,但没有奏效。 It's worth mentioning that the script, on it's own, does work and everything is fine.值得一提的是,该脚本本身可以运行,并且一切正常。 I'll add another piece of info: I have another PS script that my Python one executes and it's the creation of a new user in our local AD.我将添加另一条信息:我有另一个 PS 脚本,我的 Python 脚本执行它,它是在我们的本地 AD 中创建一个新用户。 That one works flawlessly and I'm not sure why.那一个完美无缺,我不知道为什么。

I'm executing the PS with this simple test:我正在通过这个简单的测试执行 PS:

import subprocess
import sys

subprocess.Popen(['powershell.exe', '.\\O365UserCreation.ps1'], stdout=sys.stdout)

I also checked $env:PSModulePath which seems fine and returns this: C:\\Program Files (x86)\\VMware\\Infrastructure\\vSphere PowerCLI\\Modules;C:\\Program Files\\WindowsPowerShell\\Modules;C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\Modules我还检查了$env:PSModulePath ,它看起来不错并返回: C:\\Program Files (x86)\\VMware\\Infrastructure\\vSphere PowerCLI\\Modules;C:\\Program Files\\WindowsPowerShell\\Modules;C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\Modules

Also, I have the right modules installed:另外,我安装了正确的模块:

. .

Any help would be great.任何帮助都会很棒。 I can't seem to find anything online regarding this.我似乎无法在网上找到任何关于此的信息。

I found out the I had Python 32 bit installed.我发现我安装了 Python 32 位。 Installing the 64 bit version fixed this.安装 64 位版本解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM