C# 发布web应用时Powershell脚本未按预期工作
webapp在webdeveloper中运行时会按预期工作,但一旦升级到IIS;问题开始出现,应用程序未按预期运行 aspx 这在DEV-box上完全按照预期工作,并愉快地将所有会话枚举到单个数组中。然而;在web服务器上升级和运行代码时;它似乎覆盖了第一个数组调用和第二个数组调用中的重复项 唯一的选择是运行单独的powershell脚本,这些脚本填充单独的GridView。它能工作,但有点笨拙C# 发布web应用时Powershell脚本未按预期工作,c#,powershell,iis,gridview,C#,Powershell,Iis,Gridview,webapp在webdeveloper中运行时会按预期工作,但一旦升级到IIS;问题开始出现,应用程序未按预期运行 aspx 这在DEV-box上完全按照预期工作,并愉快地将所有会话枚举到单个数组中。然而;在web服务器上升级和运行代码时;它似乎覆盖了第一个数组调用和第二个数组调用中的重复项 唯一的选择是运行单独的powershell脚本,这些脚本填充单独的GridView。它能工作,但有点笨拙 <asp:GridView ID="ResultGrid" runat="server" Da
<asp:GridView ID="ResultGrid" runat="server" DataKeyNames="Uid,DesktopGroupName" OnRowDataBound="ResultGrid_RowDataBound" OnRowCommand="ResultGrid_RowCommand" AutoGenerateColumns="False" OnSelectedIndexChanged="ResultGrid_SelectedIndexChanged" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black">
<AlternatingRowStyle BackColor="PaleGoldenrod" />
<Columns>
<asp:BoundField DataField="UserFullName" HeaderText="MUDID" />
<asp:BoundField DataField="BrokeringTime" HeaderText="SessionStart" />
<asp:BoundField DataField="ClientName" HeaderText="Client" />
<asp:BoundField DataField="DesktopGroupName" HeaderText="CTXApp" />
<asp:BoundField DataField="SessionState" HeaderText="State" />
<asp:BoundField DataField="Uid" HeaderText="Uid" />
<asp:BoundField DataField="MachineName" HeaderText="CTXServer" />
<asp:BoundField DataField="ENV" HeaderText="ENV" />
<asp:TemplateField HeaderText="Action" HeaderStyle-CssClass="Logoff" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton ID="Logoff" runat="server" OnClientClick="return confirm('Are you sure you want to forcibly log off this session? Any unsaved work will be lost!')"
CommandName="Logoff" CommandArgument='<%# Eval("Uid") + ";" +Eval("DesktopGroupName")%>'><img src="Images\logoffmin.png" alt="Logoff" /></asp:LinkButton>
</ItemTemplate>
<HeaderStyle CssClass="Logoff"></HeaderStyle> <ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="Tan" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<SortedAscendingCellStyle BackColor="#FAFAE7" />
<SortedAscendingHeaderStyle BackColor="#DAC09E" />
<SortedDescendingCellStyle BackColor="#E1DB9C" />
<SortedDescendingHeaderStyle BackColor="#C2A47B" />
</asp:GridView>
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Management.Automation;
using System.Text;
namespace PowerShellExecution
{
public partial class Default : System.Web.UI.Page
{
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");
shell.Commands.AddCommand("e:\\wwwroot\\ctxrep2csharp\\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");
}
var objects = results.Select(
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 = (SessionState)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());
//Page.Response.Redirect(Page.Request.Url.ToString(), true);
}
protected void ResultGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Get the value of column from the DataKeys using the RowIndex.
//int uid = Convert.ToInt32(ResultGrid.DataKeys[e.Row.RowIndex].Values[0]);
//string uid = ResultGrid.DataKeys[e.Row.RowIndex].Values[0].ToString();
//string DesktopGroupName = ResultGrid.DataKeys[e.Row.RowIndex].Values[1].ToString();
}
}
protected void ResultGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
//Response.Write(ResultGrid.SelectedValue.ToString());
if (e.CommandName == "Logoff")
{
//Your code
string[] arg = new string[2];
arg = e.CommandArgument.ToString().Split(';');
Session["uid"] = Convert.ToInt32(arg[0]);
Session["DesktopGroupName"] = arg[1];
int numVal = -1;
numVal = Convert.ToInt32(Session["uid"]);
Response.Write(e.CommandArgument.ToString());
Response.Write(arg[0].ToString() + "<br/>");
Response.Write(arg[1].ToString() + "<br/>");
Response.Write(Session["uid"] + " " + Session["DesktopGroupName"]);
// Initialize PowerShell engine
var shell2 = PowerShell.Create();
// Add the script to the PowerShell object
// shell.Commands.AddScript(Input.Text);
//shell2.Commands.AddCommand("C:\\Local_Scripts\\sessionkill.ps1");
shell2.Commands.AddCommand("e:\\wwwroot\\ctxrep2csharp\\Scripts\\sessionkill.ps1");
// Add Params
// shell.Commands.AddParameter(null,User.Identity.Name);
shell2.Commands.AddArgument(Convert.ToInt32(arg[0]));
//shell2.Commands.AddArgument(Session["uid"]);
//shell2.Commands.AddArgument(Session["uid"] + " " + Session["DesktopGroupName"]);
// Execute the script
var results = shell2.Invoke();
//Page.ClientScript.RegisterStartupScript(this.GetType(), "scriptkey", "alert('your message');");
//ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + Session["uid"] + "');", true);
System.Threading.Thread.Sleep(10000);
Page.Response.Redirect(Page.Request.Url.ToString(), true);
}
//Page.Response.Redirect(Page.Request.Url.ToString(), true);
}
}
internal class MyClass
{
public string UserFullName { get; set; }
public DateTime BrokeringTime { get; set; }
public string ClientName { get; set; }
public string DesktopGroupName { get; set; }
public SessionState SessionState { get; set; }
public Int64 Uid { get; set; }
public string MachineName { get; set; }
public string ENV { get; set; }
}
}
$SessionUser = "$($args[0])"
Add-PSSnapin cit*
Set-brokersite -AdminAddress ukserver
$a = @(Get-BrokerSession -BrokeringUserName $SessionUser | Select-Object UserFullName, BrokeringTime, ClientName,DesktopGroupName, @{Name="SessionState";Expression={$_.SessionState.ToString()}}, uid, machinename,@{Name='ENV';Expression={'UK'.ToString()}})
#Pull US Sessions into array
Set-brokersite -AdminAddress usserver
$a += @(Get-BrokerSession -BrokeringUserName $SessionUser | Select-Object UserFullName, BrokeringTime, ClientName,DesktopGroupName, @{Name="SessionState";Expression={$_.SessionState.ToString()}}, uid, machinename,@{Name='ENV';Expression={'US'.ToString()}})
If ($a -ne $null){
$a
}
Else {
$a
}