2017-12-19 3 views
-1

私は、.NET System.Management.Automation *名前空間でPowershellクラスを使用しています。ここでC#のPowershell - アクセスするExchange

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection 

は、私はコマンドを構築してきた方法です:私はC#構文に変数を設定し、このコマンドレットのコールを変換するために必要なコマンドのCLISTて

List<Command> cList = new List<Command>(); 
Command cEx = new Command("Set-ExecutionPolicy"); 
     cEx.Parameters.Add(new CommandParameter("Scope", "CurrentUser")); 
     cEx.Parameters.Add(new CommandParameter("ExecutionPolicy", "Unrestricted")); 
     cList.Add(cEx); 

その後、Iループと実行します

Pipeline pipeline; 
     Collection<PSObject> exResults = null; 
     foreach (Command cmd in cList) 
     { 
      pipeline = ps.Runspace.CreatePipeline(); 
      pipeline.Commands.Add(cmd); 
      exResults = pipeline.Invoke(); 
      foreach (PSObject p in exResults) 
      { 
       Console.WriteLine(p); 
      } 
     } 

$セッション変数を設定するその最初のコマンドの構文を正しく取得するのに役立つ必要があります。これは私が試したが、私が正しいとは思わないものです:

Command c10 = new Command("Set-Variable"); 
     c10.Parameters.Add(new CommandParameter("Name", "Session")); 
     c10.Parameters.Add(new CommandParameter("Value", "New-PSSession")); 
     c10.Parameters.Add(new CommandParameter("ConfigurationName", "Microsoft.Exchange")); 
     c10.Parameters.Add(new CommandParameter("ConnectionUri", "https://outlook.office365.com/powershell-liveid")); 
     c10.Parameters.Add(new CommandParameter("Credential", pCredUser)); 
     c10.Parameters.Add(new CommandParameter("Authentication", "Basic")); 
     c10.Parameters.Add(new CommandParameter("AllowRedirection")); 
     cList.Add(c10); 

C10コマンドが呼び出されたとき、私は例外を取得:「パラメータが 『ConfigurationName』パラメータ名と一致していることを見つけることができない」

+1

* ...をhelpful-たを接続しようとしていたが、私は考えていないが正しいです。*なぜ?それは私に有効なコードのように見えます。それは動作しますか? –

+0

コマンド実行時にExcptionを取得しました - 「パラメータ名 'ConfigurationName'に一致するパラメータが見つかりません」 – paparush

+0

このエラーは、この行によって発生します: 'c10.Parameters.Add(new CommandParameter(" ConfigurationName "、" Microsoft.Exchange "));'。それを指摘するために質問を編集する必要があります。 –

答えて

関連する問題