locked
Powershell-Asp.net-IIS RRS feed

  • Question

  • Hi,

    I have a Grid View in Asp.net Web page. Gridview populated from the Resultset of Powershell Script.in local its working fine. but its hosted in IIS, Grid view is not generating, I pasted the codes below please advice me on this,

            PowerShell powerShell;
            Runspace run space;
            #region PowerShell 
            protected void Runspace()
            {
                 runSpace = RunspaceFactory.CreateRunspace();
                 runSpace.Open();            
                 powerShell = PowerShell.Create();
                 RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
                 powerShell.Runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
              
            }
            #endregion
            string exmsg = "";
            
            protected void RunPSScript()
            {           
                Runspace();           
                powerShell.Runspace.Open();
                //Reading PowerShell Commands Directly From ps            
              
                powerShell.AddCommand(Request.PhysicalApplicationPath + "\\Powershell\\Sample.ps1", false);
                int count = 0;
                try
                {
                    label1.Text = "Count Label Init";
                    Collection<PSObject> results = powerShell.Invoke();
                    count = results.Count;
                    DataTable tempTable = new DataTable();
                    tempTable.Columns.Add("Name");
                    tempTable.Columns.Add("Location");
                    tempTable.Columns.Add("ResourceGroup");
                    tempTable.Columns.Add("Status");
                    foreach (PSObject psObject in results)
                    {
                        try
                        {

                        DataRow row = tempTable.NewRow();
                        row["Name"] = psObject.Properties["Name"].Value.ToString();
                        row["Location"] = psObject.Properties["Location"].Value.ToString();
                        row["ResourceGroup"] = psObject.Properties["resourcegroup"].Value.ToString();
                        row["Status"] = psObject.Properties["PowerState"].Value.ToString();
                        tempTable.Rows.Add(row);

                        }

                        catch(Exception e)
                        {
                            Console.WriteLine("Exception Message: " +e.Message);
                        }
                       
                      }
                    gridview_vms.DataSource = tempTable;
                    gridview_vms.DataBind();
                    runSpace.Close();
                }
                catch(Exception ex)
                {
                    count = -1;
                    exmsg = ex.Message;
                    Console.WriteLine("Exception Message: " + ex.Message);
                }
                finally
                {
                    label2.Text = exmsg;
                    label1.Text = "Count Label: " + count.ToString();
                }
              
            }
            string PssResult = "";



    • Edited by Muhasseena T U Tuesday, August 7, 2018 11:28 AM need to chnge
    Tuesday, August 7, 2018 8:43 AM

All replies

  • Please post code correctly using the code posting tool.  Also remove the HTML that is injected into you code.

    Posting colorized or formatted content will break the display.  Post only plain text.


    \_(ツ)_/

    Tuesday, August 7, 2018 10:09 AM
  • This is a scripting forum.  The code you have posted is C#.  C# issues should be posted the the c# developers forum on MSDN or in the asp.net forum at http://forums.asp.net

    PowerShell cannot be used in a web page without specific setups on the web server.  Post in ASP forum for assistance.


    \_(ツ)_/


    • Edited by jrv Tuesday, August 7, 2018 10:12 AM
    Tuesday, August 7, 2018 10:10 AM