Powershell和C#中此运行空间不支持的语法

时间:2012-09-07 11:11:09

标签: c# powershell office365

我正在尝试使用集成到C#中的power shell获取有关用户邮箱的信息,但我收到以下错误:

This syntax is not supported by this run space. This might be because it is no-language mode.

以下是我正在使用的代码:

using System;
using System.Collections.ObjectModel;
using System.Collections;
using System.Linq;
using System.Text;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Threading;
using System.Globalization;

namespace Office365
{
    class Program
    {
        static void Main(string[] args)
        {
            CultureInfo oldCI = Thread.CurrentThread.CurrentCulture;
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");

            System.Security.SecureString secureString = new System.Security.SecureString();
            string myPassword = "mySecretPassword";
            foreach (char c in myPassword)
                secureString.AppendChar(c);
            PSCredential credential = new PSCredential("my@maildomain.com", secureString);
            Console.WriteLine("Forbinder....");
            WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://ps.outlook.com/PowerShell-LiveID?PSVersion=2.0"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
            connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
            connectionInfo.SkipCACheck = true;
            connectionInfo.SkipCNCheck = true;

            string cmd2 = @"Get-Mailbox | Select-object Identity, displayname, ProhibitSendReceiveQuota, @{n='size';e={$MBXSTAT=Get-MailboxStatistics $_.Identity; $MBXSTAT.TotalItemSize}}";

            using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
            {

                    runspace.Open();

                using (Pipeline pipeline = runspace.CreatePipeline(cmd2))
                {
                    pipeline.Commands.AddScript(cmd2);
                    Collection<PSObject> results = pipeline.Invoke();
                    foreach (PSObject obj in results)
                    {

                        // Do something with each result object
                    }
                }
            }
        }
}

对此有何建议?我怎样才能克服这个问题?

2 个答案:

答案 0 :(得分:4)

通常情况下,我喜欢独自离开死人,但在这种情况下,我觉得有必要复活这个旧帖子,因为我遇到了确切的问题,需要一个地方来保存这段代码并希望这可以防止其他人浪费太多时间。

我发现连接到O365时遇到WSManConnectionInfo问题,在浪费了太多时间试图让它工作之后,除非我别无选择,否则我不会使用它。

那说以下代码对我有用,并且表现得像我打开PowerShell提示输入我想要运行的命令一样:)它使测试变得更加简单。

using (var _power_shell = PowerShell.Create()) {
  _power_shell.AddScript("Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process");
  _power_shell.Invoke();
var credential = new PSCredential(username, password.ToSecureString()); _power_shell.Runspace.SessionStateProxy.SetVariable("Credential", credential);
_power_shell.AddScript("$Session = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $Credential -Authentication Basic -AllowRedirection"); _power_shell.AddScript("Import-PSSession $Session"); _power_shell.Invoke();
_power_shell.AddScript("Get-Mailbox -RecipientTypeDetails RoomMailbox | Select-Object DisplayName, PrimarySmtpAddress"); var results = _power_shell.Invoke();
foreach (var obj in results) { /* Do something with the results */ }
_power_shell.AddScript("Remove-PSSession $Session"); _power_shell.Invoke(); }

以上代码假定为username&amp; password作为字符串变量已在范围内。此外,还存在一个扩展方法ToSecureString,并且在范围内。

据我所知PowerShell.Creste()提供了一个设置为Restricted的执行环境,因此您首先需要更改它。在这种情况下,因为我们将Set-ExecutionPolicy范围限定为流程管理员权限不是必需的。

从我在这个主题上发现的一点点来看,你似乎需要在完成后删除会话,否则你可能会受到限制并将自己锁定在O365之外一段时间。

答案 1 :(得分:0)

这是Exchange在线限制,在这种情况下您需要使用命令而不是脚本。 例如,运行cmdlet Get-Mailbox -Identity user@mydomain.onmicrosoft.com将如下所示:

  WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://ps.outlook.com/PowerShell-LiveID?PSVersion=2.0"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
  connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
  Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
  PowerShell shellInstance = PowerShell.Create();
  shellInstance.Runspace = runspace;

  PSObject mailbox = shellInstance.Runspace.AddCommand("Get-Mailbox").AddParameter("Identity","user@mydomain.onmicrosoft.com").Invoke().First();

我没有在项目中使用复杂的脚本和管道,所以如果你需要运行一些相当棘手的东西,我不确定它是如何工作的。