C# 在asp.net c中将powershell脚本的输出绑定到gridview#
我对c#非常陌生,我希望我尝试做的很简单,但我无法找到或遵循其他示例,其中powershell数组的输出填充gridview以进一步操作/执行另一个脚本。页面加载的过程是运行powershell脚本,该脚本创建一个会话详细信息数组,用于填充gridview。然后,通过选择gridview行,可以启动第二个脚本与该会话交互(例如,强制注销) 使用其他示例,我成功地启动了第一次powershell执行,它通过以下方式将数据抛出表单:C# 在asp.net c中将powershell脚本的输出绑定到gridview#,c#,asp.net,powershell,gridview,data-binding,C#,Asp.net,Powershell,Gridview,Data Binding,我对c#非常陌生,我希望我尝试做的很简单,但我无法找到或遵循其他示例,其中powershell数组的输出填充gridview以进一步操作/执行另一个脚本。页面加载的过程是运行powershell脚本,该脚本创建一个会话详细信息数组,用于填充gridview。然后,通过选择gridview行,可以启动第二个脚本与该会话交互(例如,强制注销) 使用其他示例,我成功地启动了第一次powershell执行,它通过以下方式将数据抛出表单: <%@ Page Title="Home Page" Lan
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="PowerShellExecution.Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<div>
<h1>PowerShell Harness<asp:Label ID="Label1" runat="server" Text="Label" Visible="False"></asp:Label>
</h1>
<asp:TextBox ID="ResultBox" TextMode="MultiLine" Width="1000px" Height="400px" runat="server"></asp:TextBox>
</div>
</asp:Content>
当前,输出作为输出字符串抛出到文本框。我甚至在如何开始将该数组输出绑定为gridview中的行方面都遇到了困难。只需要一点点的手就可以开始了
提前谢谢!
Paul。我已经有一段时间没有涉足WebForms了,但我找到了一种方法来完成您所追求的 首先,让我们稍微更改一下PowerShell脚本。我们可以简单地返回对象,而不是返回字符串(这就是
|out string
所做的事情)。C#代码中的shell.Invoke()
方法知道如何从脚本的输出中提取完全成熟的对象,因此我们不需要在PowerShell脚本中序列化为字符串,然后再次尝试将其反序列化回C#代码中的对象
暂时忽略您的业务线逻辑,我的脚本只返回一个PSCustomObjects数组,如下所示:
MyScript.ps1
写入输出@(
(新对象PSCustomObject-属性([已排序]@{
“MyProperty1”=“MyValue1.1”
“MyProperty2”=“MyValue2.1”
“MyProperty3”=“MyValue3.1”
})),
(新对象PSCustomObject-属性([已排序]@{
“MyProperty1”=“MyValue1.2”
“MyProperty2”=“MyValue2.2”
“MyProperty3”=“MyValue3.2”
}))
);
现在,我的C#Page#u加载方法执行以下操作:
Default.aspx.cs
受保护的无效页面加载(对象发送方,事件参数e)
{
//初始化PowerShell引擎
var powershell=powershell.Create();
//将脚本添加到PowerShell对象
var script=“c:\\temp\\MyScript.ps1”;
powershell.Commands.AddCommand(脚本);
//执行脚本
var results=powershell.Invoke();
...
而结果
包含一个系统.Collections.ObjectModel.Collection
。我们无法将其直接数据绑定到GridView,因为属性隐藏在每个PSObject
成员的属性
中的键值对中,但如果我们创建一个新类,则很容易将值提取到一些我们可以数据绑定的东西:
MyClass.cs
公共类MyClass
{
公共字符串MyProperty1{get;set;}
公共字符串MyProperty2{get;set;}
公共字符串MyProperty3{get;set;}
}
我们的页面加载可以将PSObject转换为此类的实例:
Default.aspx.cs
。。。
变量对象=结果。选择(
r=>newmyclass
{
MyProperty1=(字符串)r.Properties[“MyProperty1”].Value,
MyProperty2=(字符串)r.Properties[“MyProperty2”].Value,
MyProperty3=(字符串)r.Properties[“MyProperty3”].Value,
}
);
this.ResultGrid.DataSource=对象;
this.ResultGrid.DataBind();
}
然后,要显示数据,只需在Default.aspx中添加一个GridView,其中包含要定义的任何列和格式:
Default.aspx
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<div>
<h1>PowerShell Harness<asp:Label ID="Label1" runat="server" Text="Label" Visible="False"></asp:Label></h1>
<asp:GridView ID="ResultGrid" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="MyProperty1" HeaderText="My Property 1" />
<asp:BoundField DataField="MyProperty2" HeaderText="My Property 2" />
<asp:BoundField DataField="MyProperty3" HeaderText="My Property 3" />
</Columns>
</asp:GridView>
</div>
</asp:Content>
注意
您可能会发现您的Get-BrokerSession
cmdlet已经返回了特定类型对象的集合,而不是PSCustomObject,在这种情况下,您可能会跳过转换步骤,直接将数据绑定到results
对象,因此您可能需要使用它来查看。如果有任何差异,希望上面的内容能给你一些提示
希望这能有所帮助。非常感谢您的指导。Gridview现在已填充
protected void Page_Load(object sender, EventArgs e)
{
// Gets the name if authenticated.
if (User.Identity.IsAuthenticated)
Label1.Text = User.Identity.Name;
else
Label1.Text = "No user identity available.";
// Clean the Result TextBox
// Initialize PowerShell engine
var shell = PowerShell.Create();
// Add the script to the PowerShell object
// shell.Commands.AddScript(Input.Text);
// shell.Commands.AddScript("D:\\Local_Scripts\\sessioncall.ps1");
shell.Commands.AddCommand("c:\\Local_Scripts\\sessioncall.ps1");
// Add Params
// shell.Commands.AddParameter(null,User.Identity.Name);
// shell.Commands.AddParameter("Username", Label1.Text);
shell.Commands.AddArgument(User.Identity.Name);
// Execute the script
var results = shell.Invoke();
// display results, with BaseObject converted to string
// Note : use |out-string for console-like output
if (results.Count > 0)
{
// We use a string builder ton create our result text
var results2 = shell.Invoke();
foreach (var psObject in results)
{
// Convert the Base Object to a string and append it to the string builder.
// Add \r\n for line breaks
var UserFullName = (psObject.Members["UserFullName"]);
var BrokeringTime = (psObject.Members["BrokeringTime"]);
var ClientName = (psObject.Members["ClientName"]);
var DesktopGroupName = (psObject.Members["DesktopGroupName"]);
var SessionState = (psObject.Members["SessionState"]);
var Uid = (psObject.Members["Uid"]);
var MachineName = (psObject.Members["MachineName"]);
var ENV = (psObject.Members["ENV"]);
// builder.Append(psObject.BaseObject.ToString() + "\r\n");
}
this.ResultGrid.DataSource = results2;
this.ResultGrid.DataBind();
}
}
返回
[![在此处输入图像描述][1][1]
但是,当您随后定义datakeyname时,此方法会引发异常错误
<asp:GridView ID="ResultGrid" runat="server" DataKeyNames="uid" AutoGenerateColumns="False" OnSelectedIndexChanged="ResultGrid_SelectedIndexChanged">
<Columns>
<asp:buttonfield buttontype="Button"
commandname="Select"
headertext="View"
text="View"/>
<asp:BoundField DataField="UserFullName" HeaderText="UserFullName" />
<asp:BoundField DataField="BrokeringTime" HeaderText="BrokeringTime" />
<asp:BoundField DataField="ClientName" HeaderText="ClientName" />
<asp:BoundField DataField="DesktopGroupName" HeaderText="DesktopGroupName" />
<asp:BoundField DataField="SessionState" HeaderText="SessionState" />
<asp:BoundField DataField="Uid" HeaderText="Uid" />
<asp:BoundField DataField="MachineName" HeaderText="MachineName" />
<asp:BoundField DataField="ENV" HeaderText="ENV" />
</Columns>
</asp:GridView>
这会在以下位置引发异常错误:
this.ResultGrid.DataBind()
“System.Web.HttpException:'DataBinding:'System.Management.Automation.PSObject'不包含名为'uid'的属性。”
我不清楚这个方法是现在的问题还是其他问题。我很困惑,因为它必须正确地看到PSObject内部,才能定义变量并填充gridview?!嗯
哇,;好啊我刚刚意识到这整个部分都被忽略了!恰当的例子;它可以被注释掉!因此,请清楚地调整powershell脚本的输出
foreach (var psObject in results)
{
// Convert the Base Object to a string and append it to the string builder.
// Add \r\n for line breaks
//var UserFullName = (psObject.Members["UserFullName"]);
//var BrokeringTime = (psObject.Members["BrokeringTime"]);
//var ClientName = (psObject.Members["ClientName"]);
//var DesktopGroupName = (psObject.Members["DesktopGroupName"]);
//var SessionState = (psObject.Members["SessionState"]);
//var Uid = (psObject.Members["Uid"]);
//var MachineName = (psObject.Members["MachineName"]);
//var ENV = (psObject.Members["ENV"]);
// builder.Append(psObject.BaseObject.ToString() + "\r\n");
}
原谅我,我就快到了
r => new MyClass
{
UserFullName = (string)r.Properties["UserFullName"].Value,
BrokeringTime = (DateTime)r.Properties["BrokeringTime"].Value,
ClientName = (string)r.Properties["ClientName"].Value,
DesktopGroupName = (string)r.Properties["DesktopGroupName"].Value,
//SessionState = (string)r.Properties["SessionState"].Value,
Uid = (Int64)r.Properties["Uid"].Value,
//MachineName = (string)r.Properties["MachineName"].Value,
//ENV = (string)r.Properties["ENV"].Value,
}
);
this.ResultGrid.DataSource = objects;
this.ResultGrid.DataBind();
}
}
protected void ResultGrid_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write(ResultGrid.SelectedValue.ToString());
}
}
internal class MyClass
{
public string UserFullName { get; set; }
public DateTime BrokeringTime { get; set; }
public string ClientName { get; set; }
public string DesktopGroupName { get; set; }
public String SessionState { get; set; }
public Int64 Uid { get; set; }
public string MachineName { get; set; }
public string ENV { get; set; }
}
所以我现在正确地填充了gridview;有些列仍然存在问题,没有被视为字符串,但我几乎做到了
r => new MyClass
{
UserFullName = (string)r.Properties["UserFullName"].Value,
BrokeringTime = (DateTime)r.Properties["BrokeringTime"].Value,
ClientName = (string)r.Properties["ClientName"].Value,
DesktopGroupName = (string)r.Properties["DesktopGroupName"].Value,
//SessionState = (string)r.Properties["SessionState"].Value,
Uid = (Int64)r.Properties["Uid"].Value,
//MachineName = (string)r.Properties["MachineName"].Value,
//ENV = (string)r.Properties["ENV"].Value,
}
);
this.ResultGrid.DataSource = objects;
this.ResultGrid.DataBind();
}
}
protected void ResultGrid_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write(ResultGrid.SelectedValue.ToString());
}
}
internal class MyClass
{
public string UserFullName { get; set; }
public DateTime BrokeringTime { get; set; }
public string ClientName { get; set; }
public string DesktopGroupName { get; set; }
public String SessionState { get; set; }
public Int64 Uid { get; set; }
public string MachineName { get; set; }
public string ENV { get; set; }
}
看起来像是获取成员类型:
BrokeringTime NoteProperty datetime BrokeringTime=28/02/2020 06:56:39
ClientName NoteProperty string ClientName=clientname
DesktopGroupName NoteProperty string DesktopGroupName=desktopgroupname
ENV NoteProperty System.String ENV=UK
MachineName NoteProperty string MachineName=machinename
SessionState NoteProperty SessionState SessionState=Active
Uid NoteProperty long Uid=12345678
UserFullName NoteProperty string UserFullName=username
C#似乎确实喜欢system.string 非常感谢!这个方法现在对我来说更清楚了,我已经成功地进行了更多的步骤!
r => new MyClass
{
UserFullName = (string)r.Properties["UserFullName"].Value,
BrokeringTime = (DateTime)r.Properties["BrokeringTime"].Value,
ClientName = (string)r.Properties["ClientName"].Value,
DesktopGroupName = (string)r.Properties["DesktopGroupName"].Value,
//SessionState = (string)r.Properties["SessionState"].Value,
Uid = (Int64)r.Properties["Uid"].Value,
//MachineName = (string)r.Properties["MachineName"].Value,
//ENV = (string)r.Properties["ENV"].Value,
}
);
this.ResultGrid.DataSource = objects;
this.ResultGrid.DataBind();
}
}
protected void ResultGrid_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write(ResultGrid.SelectedValue.ToString());
}
}
internal class MyClass
{
public string UserFullName { get; set; }
public DateTime BrokeringTime { get; set; }
public string ClientName { get; set; }
public string DesktopGroupName { get; set; }
public String SessionState { get; set; }
public Int64 Uid { get; set; }
public string MachineName { get; set; }
public string ENV { get; set; }
}
BrokeringTime NoteProperty datetime BrokeringTime=28/02/2020 06:56:39
ClientName NoteProperty string ClientName=clientname
DesktopGroupName NoteProperty string DesktopGroupName=desktopgroupname
ENV NoteProperty System.String ENV=UK
MachineName NoteProperty string MachineName=machinename
SessionState NoteProperty SessionState SessionState=Active
Uid NoteProperty long Uid=12345678
UserFullName NoteProperty string UserFullName=username