如何通过Python脚本在Microsoft Exchange Server Powershell中运行命令? [英] How can I run command in Microsoft Exchange Server Powershell through Python script?

查看:302
本文介绍了如何通过Python脚本在Microsoft Exchange Server Powershell中运行命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查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.

请参阅此线程,并且RemoteSignedRemoteSigned好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.

我发现这是

32位应用程序可以通过以下方式访问本机系统目录 将%windir%\ Sysnative替换为%windir%\ System32. WOW64 将Sysnative识别为用于表示该文件的特殊别名 系统不应重定向访问.这种机制是灵活的, 易于使用,因此,推荐的机制是绕过文件 系统重定向.请注意,64位应用程序不能使用 系统别名,因为它是虚拟目录,而不是真实目录.

32-bit applications can access the native system directory by substituting %windir%\Sysnative for %windir%\System32. WOW64 recognizes Sysnative as a special alias used to indicate that the file system should not redirect the access. This mechanism is flexible and easy to use, therefore, it is the recommended mechanism to bypass file system redirection. Note that 64-bit applications cannot use the Sysnative alias as it is a virtual directory not a real one.

这篇关于如何通过Python脚本在Microsoft Exchange Server Powershell中运行命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆