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

5

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

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个回答

5

通常我喜欢让死去的东西保持不变,但在这种情况下,我感到有必要重新启动这篇旧帖子,因为我遇到了确切的问题,需要一个地方来保存这段代码,并希望这可以防止其他人浪费太多时间。

我发现使用WSManConnectionInfo连接到O365存在问题,在浪费了太多时间试图使其正常工作后,除非没有其他选择,否则我将不会使用它。

话虽如此,以下代码适用于我,并且表现与我打开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) { /* 对结果进行操作 */ }
_power_shell.AddScript("Remove-PSSession $Session"); _power_shell.Invoke(); }

上述代码假定usernamepassword是已经在作用域中的字符串变量。还存在一个扩展方法ToSecureString并且在作用域内。

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

从我所发现的很少的东西来看,当您完成时,似乎需要删除会话,否则您可能会被限制并长时间锁定O365。


0

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接