2012-09-07 42 views
3

我试图获取有关使用集成到C#电源外壳我的用户邮箱信息,但我得到以下错误:语法不受此运行空间在PowerShell和支持C#

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("[email protected]", 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 
        } 
       } 
      } 
     } 
} 

对此的任何建议吗?我该如何克服这个问题?

回答

3

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

我发现连接到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()提供了执行环境设定限制,你首先需要改变这种状况。在这种情况下,因为我们将Set-ExecutionPolicy限定为过程,所以不需要管理员权限。

从小事我上看来,你需要当您完成删除会话,或者你可能会得到节流,并且将自己锁定O365了一段时间的主题发现了什么。

0

这是从Exchange限制网上,你需要在这种情况下,而不是使用脚本命令。 例如,运行cmdlet的Get-Mailbox -Identity [email protected]看起来像:

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","[email protected]").Invoke().First(); 

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

相关问题