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.