2017-07-11 11 views
2

C#とPowerShellの新機能です。PSスクリプトの出力をC#変数に保存する

私はAzure Powershellで作業しています。私はメトリクスを抽出する方法を数多く試しましたが、残念ながらそれらのうちどれも解決していません。 Get-AzureRMMetricDefinitionで取得したメトリックをテキストボックスまたはメッセージボックスに表示したい(後でフィルタリングする)。

コードが添付されており、マイクロソフトのログインページとは別の出力はありません。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Collections.ObjectModel; 
using System.Management.Automation; 
using System.Management.Automation.Runspaces; 
namespace WindowsFormsApp1 
{ 
    public partial class Form1 : Form 
    { 
     String a; 
     public Form1() 
     { 
      InitializeComponent(); 
     } 
     public void scripts() 
     { 
      Runspace runspace = RunspaceFactory.CreateRunspace(); 
      runspace.Open(); 
      Pipeline pipeline = runspace.CreatePipeline(); 
      pipeline.Commands.AddScript("Login-AzureRMAccount"); 
      Collection<PSObject> results = pipeline.Invoke(); 
      runspace.Close(); 
      StringBuilder stringBuilder = new StringBuilder(); 
      foreach (PSObject obj in results) 
      { 
       stringBuilder.AppendLine(obj.ToString()); 
      } 
      Runspace runspace1 = RunspaceFactory.CreateRunspace(); 
      runspace1.Open(); 
      Pipeline pipeline1 = runspace1.CreatePipeline(); 
      String rid="/subscriptions/blah/blah/blah";//The ResourceID goes 
      here. 
      pipeline1.Commands.AddScript("Get-AzureRMMetricDefinition - 
      ResourceID \""+rid+"\""); 
      Collection<PSObject> results1 = pipeline1.Invoke(); 
      runspace1.Close(); 
      StringBuilder stringBuilder1 = new StringBuilder(); 
      foreach (PSObject obj in results1) 
      { 
       stringBuilder.AppendLine(obj.ToString()); 
      } 
      a=stringBuilder1.ToString(); 
     } 
     private void Form1_Load(object sender, EventArgs e) 
     { 
      scripts(); 
     } 


     private void InitializeComponent() 
     { 
      this.textBox2 = new System.Windows.Forms.TextBox(); 
      this.SuspendLayout(); 
      // 
      // textBox2 
      // 
      this.textBox2.Location = new System.Drawing.Point(12, 12); 
      this.textBox2.Multiline = true; 
      this.textBox2.Name = "textBox2"; 
      this.textBox2.Size = new System.Drawing.Size(729, 244); 
      this.textBox2.TabIndex = 0; 
      this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged); 
      // 
      // Form1 
      // 
      this.ClientSize = new System.Drawing.Size(753, 268); 
      this.Controls.Add(this.textBox2); 
      this.Name = "Form1"; 
      this.ResumeLayout(false); 
      this.PerformLayout(); 

     } 

     private void textBox2_TextChanged(object sender, EventArgs e) 
     { 
      scripts(); 
      this.textBox2.Text += a; 
     } 
    } 
} 

答えて

1

私は私の側に正常に動作しますGet-AzureRmMetricDefinitionを実行するには、次のコードを使用してテストを行うコードが実行された後、私は、アカウント情報やメトリックの定義を取得することができます。

using System; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.Linq; 
using System.Management.Automation; 
using System.Management.Automation.Runspaces; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApplication3 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      //run Login-AzureRmAccount 
      Runspace runspace = RunspaceFactory.CreateRunspace(); 

      runspace.Open(); 

      Pipeline pipeline = runspace.CreatePipeline(); 

      var scriptText = "Login-AzureRmAccount"; 
      pipeline.Commands.AddScript(scriptText); 

      pipeline.Commands.Add("Out-String"); 

      Collection<PSObject> results = pipeline.Invoke(); 


      runspace.Close(); 

      StringBuilder stringBuilder = new StringBuilder(); 
      foreach (PSObject obj in results) 
      { 
       stringBuilder.AppendLine(obj.ToString()); 
      } 

      var accountinfo = stringBuilder.ToString(); 

      Console.WriteLine(accountinfo); 


      //run Get-AzureRmMetricDefinition 

      Runspace runspace1 = RunspaceFactory.CreateRunspace(); 

      runspace1.Open(); 

      Pipeline pipeline1 = runspace1.CreatePipeline(); 

      var subscription = "xxxxxxxxxxxx"; 
      var resourcegroup = "xxxxx"; 
      var appname = "xxxxx"; 

      //Get metric definitions with detailed output 
      var MetricDefscriptText = $"Get-AzureRmMetricDefinition -ResourceId '/subscriptions/{subscription}/resourceGroups/{resourcegroup}/providers/microsoft.web/sites/{appname}' -DetailedOutput"; 
      pipeline1.Commands.AddScript(MetricDefscriptText); 


      pipeline1.Commands.Add("Out-String"); 

      Collection<PSObject> Metrics = pipeline1.Invoke(); 


      runspace1.Close(); 

      StringBuilder stringBuilder1 = new StringBuilder(); 
      foreach (PSObject obj in Metrics) 
      { 
       stringBuilder1.AppendLine(obj.ToString()); 
      } 

      var metricdefinitions = stringBuilder1.ToString(); 
      Console.WriteLine(metricdefinitions); 
     } 
    } 
} 

出力:

enter image description here

同じ出力PowerShellでそれを実行する場合:

enter image description here

可能であれば、コンソールアプリケーションを作成し、共有したコードを使用してテストを行い、自分の側で動作するかどうかを確認できます。

+0

それは私を助けます。問題は、メトリックが表示され、コンソールから抜け出るということです。それはそのように動作するはずですか? –

+0

すみません。私はちょうど同様に、それがデバッガであることがわかった。あなたを悩ます謝罪! もう一度、ありがとうございました! –

関連する問題