I am new to C# and PowerShell.
I am working on Azure Powershell. I have tried numerous ways of extracting metrics, but sadly none of them have worked out. I want to display the metrics obtained through Get-AzureRMMetricDefinition in a text box or in a message box (will filter it later).
The code has been attached and it does not give out any output apart from the login page from Microsoft.
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;
}
}
}

