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;
}
}
}
それは私を助けます。問題は、メトリックが表示され、コンソールから抜け出るということです。それはそのように動作するはずですか? –
すみません。私はちょうど同様に、それがデバッガであることがわかった。あなたを悩ます謝罪! もう一度、ありがとうございました! –