我想检查 Microsoft Exchange Server 中的邮箱数量。此命令在标准 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 中对其进行编码:
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 文件有很多错误。
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”命令根本不起作用。
我想我一定错过了一些重要的事情。我怎么解决这个问题?请帮忙。
编辑:为什么要添加-ExecutionPolicy RemoteSigned
Python 脚本?如果我不这样做,将导致不同的错误:
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
比Unrestricted
。它们都可以在 cmd.exe 中工作,但不能在 Python 脚本中工作。