我正在开发一个将部署在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);
使用此运行空间,我可以在服务器上运行基本的PowerShell命令。
get-mailbox -ResultSize unlimited
但是运行更复杂的命令会给我带来错误(如果直接通过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.
搜索后,我发现我可能需要注册一个新的PSSessionConfiguration,并确保脚本在PSLanguageMode = FullLanguage下运行。 See this post
我尝试这样做,但是一旦我将shellUri更改为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.
使用以下shelllUri给了我同样的错误http://schemas.microsoft.com/powershell/Microsoft.Powershell
这使我直接通过交换服务器上的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
与C#代码类似,我只能使用Microsoft.Exchange配置,但根据Get-PSSessionConfiguration
,此配置不存在,并且该列表中存在的配置将不起作用。
我现在想知道如何使用FullLanguage添加一个配置,我可以在从代码调用powershell时使用它。 我可能也完全错了,我的问题根本与PSSessionConfigurations无关,但后来我仍然想知道为什么我无法在任何地方看到Microsoft.Exchange配置。
答案 0 :(得分:9)
我最终得到了微软的支持,他们提供了以下解决方案。可以连接到本地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();
}