从Powershell到Exchange 2013-受限语言模式错误 [英] Powershell to Exchange 2013 - Restricted language mode error

查看:95
本文介绍了从Powershell到Exchange 2013-受限语言模式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用将在Exchange 2013服务器上部署的C#Web服务.该服务将负责运行powershell命令来配置Exchange.

I'm working on a C# web service that will be deployed on an Exchange 2013 server. This service will be responsible for running powershell commands to configure Exchange.

我通过这样创建的运行空间进行连接

I connect via a runspace created like this

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命令.

Using this runspace I am able to run basic powershell commands on the server.

get-mailbox -ResultSize unlimited

但是运行更复杂的命令会给我带来错误(如果直接通过powershell运行,这确实可以工作)

But running more complicated commands gives me errors (this does work if run directly through 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下运行. 查看此帖子

After searching I found that I might have to register a new PSSessionConfiguration and make sure the scripts are running under PSLanguageMode = FullLanguage. See this post

我试图这样做,但是一旦将shellUri更改为http://schemas.microsoft.com/powershell/MyConfigName,我就会收到以下错误消息.

I tried to do this, but as soon as I change the shellUri to http://schemas.microsoft.com/powershell/MyConfigName I get the following error.

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

Using the following shelllUri gave me the same error http://schemas.microsoft.com/powershell/Microsoft.Powershell

这导致我直接通过Exchange Server上的Powershell尝试以下操作

This led me to try the following directly through powershell on the exchange server

> 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,此配置不存在,并且该列表中确实存在的配置将不起作用.

Like from the C# code, I can only use the Microsoft.Exchange configuration, but this configuration doesn't exist according to Get-PSSessionConfiguration, and the configurations that do exist in that list won't work.

我现在想知道如何添加具有FullLanguage的配置,当从代码中调用Powershell时可以使用该配置. 我可能也完全错了,我的问题根本与PSSessionConfigurations无关,但是我仍然想知道为什么我在任何地方都看不到Microsoft.Exchange配置.

I am now wondering how to add a configuration with FullLanguage that I can use when calling powershell from code. I might also be completely wrong, and my issue is not related to PSSessionConfigurations at all, but then I still wonder why I can't see the Microsoft.Exchange configuration anywhere.

推荐答案

我最终获得了Microsoft的支持,他们提供了以下可行的解决方案.除了通过远程Powershell会话进行连接之外,还可以连接到本地Powershell,然后导入远程会话.这样做可以解决问题.

I ended up getting support from Microsoft and they provided the following solution which works. Instead of connection via a remote powershell session, it is possible to connect to the local powershell and then import the remote session. Doing this fixes the issue.

错误处理未包含在下面的示例中

Error handling not included in example below

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();
}

这篇关于从Powershell到Exchange 2013-受限语言模式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆