如何通过Python脚本在Microsoft Exchange Server Powershell中运行命令? [英] How can I run command in Microsoft Exchange Server Powershell through Python script?
问题描述
我想检查Microsoft Exchange Server中的邮箱数.此命令在标准cmd.exe中可以正常工作:
I want to check the number of mailbox in Microsoft Exchange Server. This command works fine in standard cmd.exe:
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto ; Get-Mailbox | Measure-Object"
输出为
...
Count : 3
Average :
Sum :
Maximum :
Minimum :
Property :
然后,我将使用"-ExecutionPolicy RemoteSigned"在Python中进行编码:
Then I am going to code it in Python, using "-ExecutionPolicy RemoteSigned":
cmd = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe
-ExecutionPolicy RemoteSigned
-command \". 'C:\\Program Files\\Microsoft\\Exchange Server\\V14\\bin\\RemoteExchange.ps1'; Connect-ExchangeServer -auto; Get-Mailbox | Measure-Object\""
os.system(cmd)
关于加载RemoteExchange.ps1文件有很多错误.
There is lots of error about loading RemoteExchange.ps1 file.
Get-ItemProperty : Cannot find path 'HKLM:\SOFTWARE\Microsoft\ExchangeServer\v14\Setup' because it does not exist.
At C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1:46 char:34
+ $global:exbin = (get-itemproperty <<<< HKLM:\SOFTWARE\Microsoft\ExchangeServer\v14\Setup).MsiInstallPath + "bin\"
+ CategoryInfo : ObjectNotFound: (HKLM:\SOFTWARE\...erver\v14\Setup:String) [Get-ItemProperty], ItemNotFo
undException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemPropertyCommand
...
The Exchange types file wasn't loaded because not all of the required files could be found.
Update-TypeData : Cannot find path 'C:\Users\administrator.SCCM01\bin\Exchange.partial.Types.ps1xml' because it does no
t exist.
At C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1:104 char:16
+ Update-TypeData <<<< -PrependPath $partialTypeFile
+ CategoryInfo : InvalidOperation: (bin\Exchange.partial.Types.ps1xml:String) [Update-TypeData], ItemNotF
oundException
+ FullyQualifiedErrorId : TypesPrependPathException,Microsoft.PowerShell.Commands.UpdateTypeDataCommand
尽管出现了Exchange命令行管理程序的欢迎屏幕,但它无法加载RemoteExchange.ps1,并且"Get-Mailbox"命令根本不起作用.
Although the welcome screen of Exchange Management Shell appears, it failed to load RemoteExchange.ps1, and "Get-Mailbox" command is not working at all.
我想我一定错过了一些重要的事情.我怎么解决这个问题?请帮忙.
I guess I must have missed something important. How can I solve this problem? Please help.
编辑:为什么要在Python脚本中添加-ExecutionPolicy RemoteSigned
?如果我不这样做,将导致另一个错误:
Edit: Why should I add -ExecutionPolicy RemoteSigned
in Python script? If I do not do that, it will result in a different error:
File C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.
请参阅此线程,并且RemoteSigned
比RemoteSigned
好c2>.它们都可以在cmd.exe中工作,但不能在Python脚本中工作.
Refer to this thread, and RemoteSigned
is better than Unrestricted
. Both of them work in cmd.exe, but not work in Python script.
推荐答案
在稍有不同的情况下,我遇到了完全相同的问题,但错误消息是相同的.我试图通过 Puppet和Powershell脚本.您正遭受
I ran into the exact same problem under slightly different circumstances but the error message was identical. I was trying to provision MS Exchange via Puppet and Powershell scripts. You are suffering from the side effects of the File System Redirector (Windows) which runs silently. Apparently 64-bit registry keys can't be accessed by 32-bit programs and the File System Redirector will change things up on you without you even knowing it.