gpt4 book ai didi

c# - 将 PS 脚本的输出存储在 C# 变量中

转载 作者:行者123 更新时间:2023-11-30 23:10:59 28 4
gpt4 key购买 nike

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

最佳答案

我使用以下代码进行测试来运行 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

如果可能,您可以创建一个控制台应用程序并使用我共享的代码进行测试,并检查它是否适用于您。

关于c# - 将 PS 脚本的输出存储在 C# 变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45027825/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com