none
Unable To Get PowerShell Output RRS feed

  • Question

  • I have searched the web & this forum in vain attempting to learn how to access the output of this PowerShell script using c#.

    Below is the code - notice the failing script.

    Any help will be much appreciated.  TIA, Joe

    private void LoadList(string server, List<SERVICE> list)
                {
                list.Clear();
                using (var ps = PowerShell.Create())
                    {
                    // script below works fine
                    //ps.AddScript("Get-Service -ComputerName" + " " + server);
    
                    // script below fails with error "object reference not set to instance of an object
                    ps.AddScript("get-wmiobject win32_service | format-list Name, Description, State, DisplayName ");
    
                    var results = ps.Invoke();
                                       
                    foreach (PSObject obj in results)
                        {
                        SERVICE s = new SERVICE();
                        s.Name = obj.BaseObject.ToString();
    
                        s.Name = obj.Members["Name"].Value.ToString();
                        s.Status = obj.Members["Status"].Value.ToString();
                        s.DName = obj.Members["DisplayName"].Value.ToString();
                        s.Found = false;
                        list.Add(s);
                        }  // end of foreach st'mt
    
                     }  // end of using st'mt
    
             }  // end of method

      


    • Edited by jbm417 Saturday, May 12, 2018 4:13 AM
    Saturday, May 12, 2018 4:13 AM

Answers

  • Try the next command:

       ps.AddScript( "get-wmiobject win32_service | Select Name, Description, Status, DisplayName " );


    • Marked as answer by jbm417 Sunday, May 13, 2018 2:26 AM
    Saturday, May 12, 2018 7:10 AM

All replies

  • Try the next command:

       ps.AddScript( "get-wmiobject win32_service | Select Name, Description, Status, DisplayName " );


    • Marked as answer by jbm417 Sunday, May 13, 2018 2:26 AM
    Saturday, May 12, 2018 7:10 AM
  • I'm not sure what you are suggesting.

    The script you posted is the script that fails as noted in the code.

    Saturday, May 12, 2018 1:41 PM
  • Change the command.

    This works for me :

    using (var ps = PowerShell.Create())
    {
        ps.AddScript("get-wmiobject win32_service | Select Name, Description, Status, DisplayName");
        Collection<PSObject> results = ps.Invoke();
        foreach (PSObject obj in results)
        {
            Console.WriteLine("Name : {0}", obj.Members["Name"].Value.ToString());
            Console.WriteLine("DisplayName : {0}", obj.Members["DisplayName"].Value.ToString());
            Console.WriteLine("Status : {0}\n", obj.Members["Status"].Value.ToString());                    
        }
    }


    Saturday, May 12, 2018 2:16 PM
  • Now I see the difference - duh! The script uses Select vs Format-list. With Select, it works fine for me now. This definitely solved my problem. Thanks, Joe
    Sunday, May 13, 2018 2:26 AM