How can I run command in Microsoft Exchange Server Powershell through Python script?

邮差的信 提交于 2019-12-13 04:47:50

问题


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"

Output is

...
Count    : 3
Average  :
Sum      :
Maximum  :
Minimum  :
Property :

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)

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

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.

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.

Refer to this thread, and RemoteSigned is better than Unrestricted. Both of them work in cmd.exe, but not work in Python script.


回答1:


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.

I found this to be an elegant solution for my situation:

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.



来源:https://stackoverflow.com/questions/13266159/how-can-i-run-command-in-microsoft-exchange-server-powershell-through-python-scr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!