5

我正在尝试使用集成到 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
                    }
                }
            }
        }
}

对此有何建议?我该如何克服这个问题?

4

2 回答 2

5

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

我发现连接到 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&password作为字符串变量已经在范围内。也存在扩展方法ToSecureString并且在范围内。

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

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

于 2017-07-18T10:30:25.687 回答
0

这是 Exchange Online 的限制,在这种情况下您需要使用命令而不是脚本。例如,运行 cmdletGet-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();

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

于 2017-10-03T10:47:14.933 回答