我是C#和PowerShell的新手。
我正在使用Azure Powershell
。我已经尝试了很多方法来提取指标,但遗憾的是没有一种方法可以解决。我想在文本框或消息框中显示通过Get - AzureRMMetricDefinition
获得的指标(稍后会对其进行过滤)。
代码已附加,除了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;
}
}
}
答案 0 :(得分: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:
Same output if run it in Powershell:
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.