StUffz StUffz - 2 months ago 15
ASP.NET (C#) Question

Display values from powershell objects on asp.net site

I've been asked to create several PowerShell scripts which work together with asp.net sites.
However, asp.net sites together with C# is the completely new terrain for me.

Searching the www I've found some blogs which showcased how to run .ps1 scripts and how to display the output in a textbox.
This worked quite well and I've also been able to pass parameters to my test script.

Now I wanted to format my output a bit more to get a nicer and more "sophisticated" UI. I've created a quick and dirty WPF form in Sapiens PowerShell Studio which looks like this:

WPF Form

It's displaying the values of the following WmiObject which I'm pulling from an old SCCM test server

$SCCMinfo = Get-WmiObject -Namespace 'root\SMS' -Class 'SMS_ProviderLocation' -ComputerName "6.6.5.5.4.4"


It's a layout table with two columns. On the left, there are labels with "static" text and on the right, there are labels with the actual values from the $SCCMinfo object (like:
$labelGenusVal.Text = $SCCMinfo.__GENUS
)

How can I achieve the same with asp.net forms? All I can find is how to show the complete output in one single element.

EDIT:



I have been able to achieve what I wanted with write-output in my .ps1 for every value I need and then indexing through results in the aspx.cs.

But I find this a very ugly solution. Is there no direct way to retrieve variables and their values from the PowerShell script?
I also tried
shell.Runspace.SessionStateProxy.PSVariable.GetValue("VarName");
and
shell.Runspace.SessionStateProxy.GetVariable("VarName");
but they always returned null.

Here's my current solution:

.aspx.cs

using System;
using System.Management.Automation;

namespace WPS_Test2
{
public partial class SCCM_info : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}


protected void ExecuteCode_Click(object sender, EventArgs e)
{
// Clean the Result TextBox
//ResultBox.Text = string.Empty;

// Initialize PowerShell engine
var shell = PowerShell.Create();

string pathToWPS = @"C:\Users\sudo\Desktop\WPS_projects\WPS_Test2\WPS_Test2\zzz_wps_scripts\SCCM_info.ps1";

// Add the script to the PowerShell object
shell.AddCommand(pathToWPS);

// Add some arguments / params
shell.AddParameter("sccm_server", SCCM_server.Text);


// Execute the script
var results = shell.Invoke();


LabelSelectedSCCMsrv.Text = "selected SCCM server: " + SCCM_server.Text;

var SCCMiGenus = results[0].ToString();
labelGenusVal.Text = SCCMiGenus;

var SCCMiClass = results[1].ToString();
labelClassVal.Text = SCCMiClass;

var SCCMiSuperclass = results[2].ToString();
labelSuperclassVal.Text = SCCMiSuperclass;

var SCCMiDynasty = results[3].ToString();
labelDynastyVal.Text = SCCMiDynasty;

var SCCMiRelpath= results[4].ToString();
labelRelpathVal.Text = SCCMiRelpath;

var SCCMiPropertyCount = results[5].ToString();
labelPropertyCountVal.Text = SCCMiPropertyCount;

var SCCMiDerivation = results[6].ToString();
labelDerivationVal.Text = SCCMiDerivation;

var SCCMiServer = results[7].ToString();
labelServerVal.Text = SCCMiServer;

var SCCMiNamespace = results[8].ToString();
labelNamespaceVal.Text = SCCMiNamespace;

var SCCMiPath = results[9].ToString();
labelPathVal.Text = SCCMiPath;

var SCCMiMachine = results[10].ToString();
labelMachineVal.Text = SCCMiMachine;

var SCCMiNamespacePath = results[11].ToString();
labelNamespacePathVal.Text = SCCMiNamespacePath;

var SCCMiProviderForLocalSite = results[12].ToString();
labelProviderForLocalSiteVal.Text = SCCMiProviderForLocalSite;

var SCCMiSiteCode = results[13].ToString();
labelSiteCodeVal.Text = SCCMiSiteCode;

var SCCMiPSComputerName = results[14].ToString();
labelPSComputerNameVal.Text = SCCMiPSComputerName;
}
}
}


.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SCCM_info.aspx.cs" Inherits="WPS_Test2.SCCM_info" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 145px;
}
.auto-style2 {
width: 145px;
height: 23px;
}
.auto-style3 {
height: 23px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
SCCM Info</div>
<div>
<table style="width:100%;">
<tr>
<td class="auto-style1">SCCM Server:</td>
<td>
<asp:TextBox ID="SCCM_server" runat="server" Width="248px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="ExecuteCode_Click" />
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="auto-style1">&nbsp;</td>
<td>
<asp:Label ID="LabelSelectedSCCMsrv" runat="server"></asp:Label>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="auto-style1">Genus</td>
<td>
<asp:Label ID="labelGenusVal" runat="server"></asp:Label>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="auto-style2">Class</td>
<td class="auto-style3">
<asp:Label ID="labelClassVal" runat="server"></asp:Label>
</td>
<td class="auto-style3"></td>
</tr>
<tr>
<td class="auto-style1">Superclass</td>
<td>
<asp:Label ID="labelSuperclassVal" runat="server"></asp:Label>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="auto-style1">
<asp:Label ID="Label13" runat="server" Text="Dynasty"></asp:Label>
</td>
<td>
<asp:Label ID="labelDynastyVal" runat="server"></asp:Label>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="auto-style2">
<asp:Label ID="Label14" runat="server" Text="Relpath"></asp:Label>
</td>
<td class="auto-style3">
<asp:Label ID="labelRelpathVal" runat="server"></asp:Label>
</td>
<td class="auto-style3"></td>
</tr>
<tr>
<td class="auto-style1">
<asp:Label ID="Label15" runat="server" Text="Property Count"></asp:Label>
</td>
<td>
<asp:Label ID="labelPropertyCountVal" runat="server"></asp:Label>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="auto-style1">
<asp:Label ID="Label16" runat="server" Text="Derivation"></asp:Label>
</td>
<td>
<asp:Label ID="labelDerivationVal" runat="server"></asp:Label>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="auto-style1">
<asp:Label ID="Label17" runat="server" Text="Server"></asp:Label>
</td>
<td>
<asp:Label ID="labelServerVal" runat="server"></asp:Label>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="auto-style1">
<asp:Label ID="Label18" runat="server" Text="Namespace"></asp:Label>
</td>
<td>
<asp:Label ID="labelNamespaceVal" runat="server"></asp:Label>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="auto-style1">
<asp:Label ID="Label19" runat="server" Text="Path"></asp:Label>
</td>
<td>
<asp:Label ID="labelPathVal" runat="server"></asp:Label>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="auto-style1">
<asp:Label ID="Label20" runat="server" Text="Machine"></asp:Label>
</td>
<td>
<asp:Label ID="labelMachineVal" runat="server"></asp:Label>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="auto-style1">
<asp:Label ID="Label21" runat="server" Text="Namespace Path"></asp:Label>
</td>
<td>
<asp:Label ID="labelNamespacePathVal" runat="server"></asp:Label>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="auto-style1">
<asp:Label ID="Label22" runat="server" Text="Provider for loca site"></asp:Label>
</td>
<td>
<asp:Label ID="labelProviderForLocalSiteVal" runat="server"></asp:Label>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="auto-style1">
<asp:Label ID="Label23" runat="server" Text="Site Code"></asp:Label>
</td>
<td>
<asp:Label ID="labelSiteCodeVal" runat="server"></asp:Label>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="auto-style1">
<asp:Label ID="Label24" runat="server" Text="PSComputerName"></asp:Label>
</td>
<td>
<asp:Label ID="labelPSComputerNameVal" runat="server"></asp:Label>
</td>
<td>&nbsp;</td>
</tr>
</table>
</div>
</form>
</body>
</html>


.ps1

param ([string]$sccm_server)


$SCCMinfo = Get-WmiObject -Namespace 'root\SMS' -Class 'SMS_ProviderLocation' -ComputerName $sccm_server

Write-Output $SCCMinfo.__GENUS
Write-Output $SCCMinfo.__CLASS
# Superclass is NULL in this example, adding a char to display output
# Write-Output $SCCMinfo.__SUPERCLASS
Write-Output "--"
Write-Output $SCCMinfo.__DYNASTY
Write-Output $SCCMinfo.__RELPATH
Write-Output $SCCMinfo.__PROPERTY_COUNT
# Derivation is {} in this example, adding a char to display output
# Write-Output $SCCMinfo.__DERIVATION
Write-Output "--"
Write-Output $SCCMinfo.__SERVER
Write-Output $SCCMinfo.__NAMESPACE
Write-Output $SCCMinfo.__PATH
Write-Output $SCCMinfo.Machine
Write-Output $SCCMinfo.NamespacePath
Write-Output $SCCMinfo.ProviderForLocalSite
Write-Output $SCCMinfo.SiteCode
Write-Output $SCCMinfo.PSComputerName

Answer Source

I've finally been able to achieve an adequate solution. It's quite simple if you know how.

I used the same IDs for the labels as they are for the results.Members names and loop through them with an extra if statement to set their text to the appropiate value if it's not null. In the .ps1 I'm just returning the vars that I need (in this case only $SCCMinfo)

If there are even better solutions, I'm open to suggestions. I'm still learning after all.

Here's all together:

.ps1

param ([string]$sccm_server)

$SCCMinfo = Get-WmiObject -Namespace 'root\SMS' -Class 'SMS_ProviderLocation' -ComputerName $sccm_server

return $SCCMinfo

.aspx.cs

using System;
using System.Management.Automation;
using System.Web.UI.WebControls;

namespace WPS_Test2
{
    public partial class SCCM_info : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        protected void ExecuteCode_Click(object sender, EventArgs e)
        {
            // Initialize PowerShell engine
            var shell = PowerShell.Create();

            // Attention: No not use spaces!!! Script may cause problems or wont work at all.
            string pathToWPS = @"C:\Users\sudo\Desktop\WPS_projects\WPS_Test2\WPS_Test2\zzz_wps_scripts\SCCM_info.ps1";

            // Add the script to the PowerShell object
            shell.AddCommand(pathToWPS);

            // Add some arguments / params
            shell.AddParameter("sccm_server", SCCM_server.Text);

            // Execute the script
            var results = shell.Invoke();

            LabelSelectedSCCMsrv.Text = "selected SCCM server: " + SCCM_server.Text;

            foreach (var name in results[0].Members)
            {
                if (FindControl("lbl" + name.Name) != null)
                {
                    // Find label with the same name as the current property and display the value
                    // Encode the string in HTML (prevent security issue with 'dangerous' caracters like < >
                    if (name.Value != null)
                    {
                        (FindControl("lbl" + name.Name) as Label).Text = Server.HtmlEncode(name.Value.ToString());
                    }
                }
            }
        }
    }
}

.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SCCM_info.aspx.cs" Inherits="WPS_Test2.SCCM_info" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 145px;
        }
        .auto-style2 {
            width: 145px;
            height: 23px;
        }
        .auto-style3 {
            height: 23px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            SCCM Info</div>
        <div>
            <table style="width:100%;">
                <tr>
                    <td class="auto-style1">SCCM Server:</td>
                    <td>
                        <asp:TextBox ID="SCCM_server" runat="server" Width="248px"></asp:TextBox>
                        <asp:Button ID="Button1" runat="server" Text="Button"  OnClick="ExecuteCode_Click" />
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style1">&nbsp;</td>
                    <td>
                        <asp:Label ID="LabelSelectedSCCMsrv" runat="server"></asp:Label>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style1">Genus</td>
                    <td>
                        <asp:Label ID="lbl__GENUS" runat="server"></asp:Label>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style2">Class</td>
                    <td class="auto-style3">
                        <asp:Label ID="lbl__CLASS" runat="server"></asp:Label>
                    </td>
                    <td class="auto-style3"></td>
                </tr>
                <tr>
                    <td class="auto-style1">Superclass</td>
                    <td>
                        <asp:Label ID="lbl__SUPERCLASS" runat="server"></asp:Label>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style1">
                        <asp:Label ID="Label13" runat="server" Text="Dynasty"></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="lbl__DYNASTY" runat="server"></asp:Label>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style2">
                        <asp:Label ID="Label14" runat="server" Text="Relpath"></asp:Label>
                    </td>
                    <td class="auto-style3">
                        <asp:Label ID="lbl__RELPATH" runat="server"></asp:Label>
                    </td>
                    <td class="auto-style3"></td>
                </tr>
                <tr>
                    <td class="auto-style1">
                        <asp:Label ID="Label15" runat="server" Text="Property Count"></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="lbl__PROPERTY_COUNT" runat="server"></asp:Label>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style1">
                        <asp:Label ID="Label16" runat="server" Text="Derivation"></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="lbl__DERIVATION" runat="server"></asp:Label>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style1">
                        <asp:Label ID="Label17" runat="server" Text="Server"></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="lbl__SERVER" runat="server"></asp:Label>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style1">
                        <asp:Label ID="Label18" runat="server" Text="Namespace"></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="lbl__NAMESPACE" runat="server"></asp:Label>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style1">
                        <asp:Label ID="Label19" runat="server" Text="Path"></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="lbl__PATH" runat="server"></asp:Label>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style1">
                        <asp:Label ID="Label20" runat="server" Text="Machine"></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="lblMachine" runat="server"></asp:Label>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style1">
                        <asp:Label ID="Label21" runat="server" Text="Namespace Path"></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="lblNamespacePath" runat="server"></asp:Label>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style2">
                        <asp:Label ID="Label22" runat="server" Text="Provider for local site"></asp:Label>
                    </td>
                    <td class="auto-style3">
                        <asp:Label ID="lblProviderForLocalSite" runat="server"></asp:Label>
                    </td>
                    <td class="auto-style3"></td>
                </tr>
                <tr>
                    <td class="auto-style1">
                        <asp:Label ID="Label23" runat="server" Text="Site Code"></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="lblSiteCode" runat="server"></asp:Label>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style1">
                        <asp:Label ID="Label24" runat="server" Text="PSComputerName"></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="lblPSComputerName" runat="server"></asp:Label>
                    </td>
                    <td>&nbsp;</td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>