locked
I want to get PSObject result in a table, but I get error:System.NullReferenceException: 'Object reference not set to an instance of an object.' RRS feed

  • Question

  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Management.Automation;
    using System.Collections.ObjectModel;
    using System.Management.Automation.Runspaces;
    using System.Data.OleDb;
    using System.Data;

    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    RunScript s = new RunScript();
                    s.Script = "Get-Service";
                   
                    s.ScriptResult();
                    Console.WriteLine(s.ResultScript);
                    Console.ReadKey();
                }
                catch(Exception e)
                {
                    Console.WriteLine(e);
                    Console.ReadKey();
                }
            }
        }
    }

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Data;
    using System.Linq;
    using System.Management.Automation;
    using System.Management.Automation.Runspaces;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
        public class RunScript
        { 
            
            public string Script { get; set; }
            public object ResultScript { get; set; }
            
            public void ScriptResult()
            {
                Runspace runspace = RunspaceFactory.CreateRunspace();

                runspace.Open();

                Pipeline pipelineimport = runspace.CreatePipeline();
                Pipeline pipeline = runspace.CreatePipeline();
                pipeline.Commands.AddScript(Script);
                pipeline.Commands.Add("Out-String");

                Collection<PSObject> results = pipeline.Invoke();
                
                runspace.Close();

                DataTable tempTable = new DataTable();
                tempTable.Columns.Add("Status");
                tempTable.Columns.Add("Name");
                tempTable.Columns.Add("DisplayName");


                foreach (PSObject obj in results)
                {
                    //ResultScript = obj;

                    DataRow row = tempTable.NewRow();
                    row["Status"] = obj.Properties["Status"].Value.ToString();
                    row["Name"] = obj.Properties["Name"].Value.ToString();
                    row["DisplayName"] = obj.Properties["DisplayName"].Value.ToString();
                    tempTable.Rows.Add(row);

                }
                
            }

        }
               
     }
        

                    
    Friday, August 14, 2020 2:14 PM

Answers

  • Try removing this line: ‘pipeline.Commands.Add( "Out-String" )’.

    • Marked as answer by Protecteric Monday, August 17, 2020 5:37 AM
    Sunday, August 16, 2020 7:48 AM

All replies

  • Can somebody give me advice? I want to put the obj result in a table so I can work with that data.

    Friday, August 14, 2020 2:15 PM
  • Which line gives the exception?

    • Proposed as answer by Naomi N Friday, August 14, 2020 5:33 PM
    Friday, August 14, 2020 3:11 PM
  • It starts with this line   row["Status"] = obj.Properties["Status"].Value.ToString();
    Sunday, August 16, 2020 6:28 AM
  • Try removing this line: ‘pipeline.Commands.Add( "Out-String" )’.

    • Marked as answer by Protecteric Monday, August 17, 2020 5:37 AM
    Sunday, August 16, 2020 7:48 AM
  • Thank you I will try that tomorrow!

    Sunday, August 16, 2020 5:31 PM
  • It works, thank you! I will now try to put the result in a SQL table.
    Monday, August 17, 2020 5:38 AM