繁体   English   中英

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

[英]Syntax not supported by this runspace in Powershell and C#

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

有什么建议吗? 我怎样才能克服这个问题?

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

我发现连接到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(); }

上面的代码假设usernamepassword已经作为范围内的字符串变量。 此外,Extention方法ToSecureString存在且在范围内。

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

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

这是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();

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

暂无
暂无

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

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