and*_*eas 4 powershell exchange-server
我正在开发一个将部署在Exchange 2013服务器上的C#Web服务.此服务将负责运行powershell命令以配置Exchange.
我通过这样创建的运行空间连接
const string shellUri = "http://schemas.microsoft.com/powershell/microsoft.exchange";
var uri = new Uri(_exchangeConnectionUri);
var credentials = (PSCredential)null; // Windows authentication
var connectionInfo = new WSManConnectionInfo(uri, shellUri, credentials);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
var runspace = RunspaceFactory.CreateRunspace(connectionInfo);
Run Code Online (Sandbox Code Playgroud)
使用此运行空间,我可以在服务器上运行基本的powershell命令.
get-mailbox -ResultSize unlimited
Run Code Online (Sandbox Code Playgroud)
但运行更复杂的命令会给我带来错误(如果直接通过powershell运行,这确实有效)
get-mailbox -ResultSize unlimited | where {$_.emailaddresses -like "*test.com"}
At line:1 char:43
+ get-mailbox -ResultSize unlimited | where {$_.emailaddresses -like "*test.com ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Script block literals are not allowed in restricted language mode or a Data section.
At line:1 char:44
+ get-mailbox -ResultSize unlimited | where {$_.emailaddresses -like "*test.com ...
+ ~~~~~~~~~~~~~~~~~
Property references are not allowed in restricted language mode or a Data section.
At line:1 char:44
+ get-mailbox -ResultSize unlimited | where {$_.emailaddresses -like "*test.com ...
+ ~~
A variable that cannot be referenced in restricted language mode or a Data section is being referenced. Variables that can be referenced include the following: $PSCulture, $PSUICulture, $true, $false, and $null.
Run Code Online (Sandbox Code Playgroud)
搜索后,我发现我可能需要注册一个新的PSSessionConfiguration,并确保脚本在PSLanguageMode = FullLanguage下运行.看这篇文章
我尝试这样做,但是一旦我将shellUri更改为I,http://schemas.microsoft.com/powershell/MyConfigName我就会收到以下错误.
The WS-Management service cannot process the request.
Cannot find the MyConfigName session configuration in the WSMan: drive on the ComputerName computer.
Run Code Online (Sandbox Code Playgroud)
使用以下shelllUri给了我同样的错误 http://schemas.microsoft.com/powershell/Microsoft.Powershell
这导致我直接通过Exchange服务器上的powershell尝试以下操作
> Get-PSSessionConfiguration | format-list -property name
result:
Name : MyConfigName
Name : microsoft.powershell
Name : microsoft.powershell.workflow
Name : microsoft.powershell32
Name : microsoft.windows.servermanagerworkflows
> $session = New-PSSession -ConfigurationName MyConfigName -ConnectionUri $uri -Authentication Kerberos
result:
error "Cannot find the MyConfigName session configuration in the WSMan: drive on the ComputerName computer."
> $session = New-PSSession -ConfigurationName Microsoft.Powershell -ConnectionUri $uri -Authentication Kerberos
result:
error "Cannot find the Microsoft.Powershell session configuration in the WSMan: drive on the ComputerName."
> $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $uri -Authentication Kerberos
result:
nothing, meaning $session variable set correctly
Run Code Online (Sandbox Code Playgroud)
与C#代码一样,我只能使用Microsoft.Exchange配置,但此配置根据不存在Get-PSSessionConfiguration,并且该列表中存在的配置将不起作用.
我现在想知道如何使用FullLanguage添加一个配置,我可以在从代码调用powershell时使用它.我可能也完全错了,我的问题根本与PSSessionConfigurations无关,但后来我仍然想知道为什么我无法在任何地方看到Microsoft.Exchange配置.
and*_*eas 11
我最终获得了微软的支持,他们提供了以下解决方案.可以连接到本地PowerShell,然后导入远程会话,而不是通过远程PowerShell会话进行连接.这样做可以解决问题.
错误处理不包括在下面的示例中
var runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
object psSessionConnection;
// Create a powershell session for remote exchange server
using (var powershell = PowerShell.Create())
{
var command = new PSCommand();
command.AddCommand("New-PSSession");
command.AddParameter("ConfigurationName", "Microsoft.Exchange");
command.AddParameter("ConnectionUri", new Uri(_exchangeConnectionUri));
command.AddParameter("Authentication", "Kerberos");
powershell.Commands = command;
powershell.Runspace = runspace;
// TODO: Handle errors
var result = powershell.Invoke();
psSessionConnection = result[0];
}
// Set ExecutionPolicy on the process to unrestricted
using (var powershell = PowerShell.Create())
{
var command = new PSCommand();
command.AddCommand("Set-ExecutionPolicy");
command.AddParameter("Scope", "Process");
command.AddParameter("ExecutionPolicy", "Unrestricted");
powershell.Commands = command;
powershell.Runspace = runspace;
powershell.Invoke()
}
// Import remote exchange session into runspace
using (var powershell = PowerShell.Create())
{
var command = new PSCommand();
command.AddCommand("Import-PSSession");
command.AddParameter("Session", psSessionConnection);
powershell.Commands = command;
powershell.Runspace = runspace;
powershell.Invoke();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9024 次 |
| 最近记录: |