1

I've been making a C# application that runs some Powershell stuff, the issue is that it doesn't seem to be be collecting the correct/all results of the Powershell commands.

The below code:

private void Button_Click(object sender, RoutedEventArgs e)
    {
        List<string> results = RunScript("Get-ADUser testuser2 -Properties *");
        this.ShowMessageAsync("OK",results[0]);
    }

    private List<string> RunScript(string comand)
    {
        var powerShell = PowerShell.Create();
        powerShell.Runspace = runspace;
        powerShell.AddScript(comand);

        Collection<PSObject> results = powerShell.Invoke();
        List<string> resultsList = new List<string>();

        if (powerShell.Streams.Error.Count > 0)
        {
            this.ShowMessageAsync("Something went wrong!", "Check error log for details!");
            runspaceBox.AppendText(powerShell.Streams.Error[0] + "\r\n");
        }

        else
        {
            foreach (PSObject obj in results)
            {
                runspaceBox.AppendText(obj.ToString() + "\r\n");
                resultsList.Add(obj.ToString());
            }
        }
        return resultsList;
    }


Results in:

{CN=testuser2,DC=testdomain,DC=com}


Putting the command into Powershell directly results in the below:

PS C:\Users\Ultimate-V1> Get-ADUser testuser2 -Properties *

[A bunch of extra stuff above]

Description :

DisplayName : Test User2

DistinguishedName : CN=Test One,CN=Users,DC=testdomain,DC=com

Division :

DoesNotRequirePreAuth : False

[A lot of extra stuff below]


I've snipped a lot of the results out of the above command as it is very long.

What I can't work out is why it is only returning the Distinguished Name and not all of the results, this happens with some commands but not others. The command to return all AD groups on a user works fine for example.

If possible i'd love it to have EVERYTHING that the command returns, I will be formatting the resultsList later in the code.

Any help or pointers would be greatly appreciated!

4
  • 2
    powerShell.AddScript(comand); -> powerShell.AddScript(comand).AddCommand("Out-String"); Commented Dec 10, 2017 at 2:39
  • @PetSerAI Huh, that seems to result in one nice big block of text in position 0 of the resultsList (lines separated by \r\n), this will semi-work but will require a lot of extra time working out the results of different Powershell commands, I guess I can do checks to see if it put it in one big line, and if so to separate it into resultsList correctly. You wouldn't happen to know a more consistent way to do it though? Commented Dec 10, 2017 at 3:29
  • Consistent with what? If you want the same results as PowerShell print to the console, then you need to pass objects thru PowerShell formatting system, which is a bit more complicated then just obj.ToString(). Out-String is doing just that and return a string to you. If you want something else, then you need to say, what exactly you want. Commented Dec 10, 2017 at 3:52
  • Sorry, I was basically looking for a way to get all output of the Powershell after sending it the command, i've used your example and added a little to trim and such, works pretty well now thanks! The program will be running pre-defined scripts so the output should be consistent, if there's any issues I can debug it at that time. If you wanted to create your comment as the resolution i'll set it to Resolved. Commented Dec 10, 2017 at 5:03

1 Answer 1

2

Because you casted the result as a string, so you got a string representation of the complex object (whatever the result of the object's .ToString() method).

In PowerShell itself, it doesn't cast everything to a string to display it; it has various rules about how to display objects in general (usually a table or list format), and some types have specific formatters.

PetSerAl already answered in the comments, but the way to get a string representation of what PowerShell displays on the console is to send your output through Out-String:

powerShell.AddScript(comand).AddCommand("Out-String");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.