繁体   English   中英

Powershell to Exchange 2013-受限语言模式错误

[英]Powershell to Exchange 2013 - Restricted language mode error

我正在使用将在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下运行。 看到这个帖子

我尝试执行此操作,但是将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

这导致我直接通过Exchange Server上的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配置。

我最终从Microsoft获得支持,他们提供了以下可行的解决方案。 除了通过远程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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM