2

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;
        }
    }   
}

1 Answer 1

2

I do a test with the following code to run Get-AzureRmMetricDefinition, which works fine on my side, I can get account info and metric definitions after the code is executed.

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);
        }
    }
}

Output:

enter image description here

Same output if run it in Powershell:

enter image description here

If possible, you can create a console application and do a test with the code that I shared, and check whether it works on your side.

Sign up to request clarification or add additional context in comments.

2 Comments

It does help me out. The issue is that the minute that it displays the metrics, it exits out of the console. Is it supposed to work like that?
Excuse me for being naive. I just googled that as well, it turns out that it is the debugger. Apologies for bothering you! Once again, thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.