PrincipalSource Property is blank in Get-LocalGroupMember by PowerShell Script

96 Views Asked by At

I am getting "PrincipalSource" property Blank while running following command by PowerShell Script in c#.

Command : Get-LocalGroupMember -Group Administrators

PowerShell Output: enter image description here

By C# Code passing Script path:

enter image description here

I have written output in text file.

Script is executing by c# Code because I can see output but PrincipalSource value blank.

System details:

Windows 10, PowerShell 5.1, .Net Version 4.8

Sample Code:

Runspace runspace = RunspaceFactory.CreateRunspace();
PowerShell powershell = PowerShell.Create();

powershell.Runspace = runspace 
powershell.AddScript(scriptPath);
powershell.Runspace.Open();
powershell.Invoke();

powershell.Runspace.Close();
powershell.Dispose();

==========================================================

        PowerShell powershell = PowerShell.Create();
        powershell.AddCommand("Get-LocalGroupMember");
        powershell.AddParameter("Name", "Administrators");

       
        Collection <PSObject> results = powershell.Invoke(); ;
       
        StringBuilder stringBuilder = new StringBuilder();
        foreach (PSObject obj in results)
        {
           
            Console.WriteLine("{0,-20}{1}",
            obj.Members["ObjectClass"].Value,
            obj.Members["Name"].Value,
            obj.Members["PrincipalSource"].Value);
        }
        Console.WriteLine("End");

enter image description here

0

There are 0 best solutions below