2

Using Powershell Object and running foreach loop to get the results,

I got 2 correct IP Address results 144.68.205.19 and 144.68.205.22 but it printed in 1 line,

144.68.205.19144.68.205.22

Which it was suppose to be split new line like this,

144.68.205.19
144.68.205.22

Please advise, here a C# codes,

// Powershell
            Runspace runSpace = RunspaceFactory.CreateRunspace();
            runSpace.Open();
            Pipeline pipeline = runSpace.CreatePipeline();

            Command invokeScript = new Command("Invoke-Command");
            RunspaceInvoke invoke = new RunspaceInvoke();

            // invoke-command -computername compName -scriptblock { get-process }

            ScriptBlock sb = invoke.Invoke("{"+ PowerShellCodeBox.Text +"}")[0].BaseObject as ScriptBlock;
            invokeScript.Parameters.Add("scriptBlock", sb);
            invokeScript.Parameters.Add("computername", TextBoxServer.Text);

    string str = "";

    pipeline.Commands.Add(invokeScript);
    Collection<PSObject> output = pipeline.Invoke();
    foreach (PSObject psObject in output)
    {

        str = str + psObject;
    }

    if (str == ""){
        str = "Error";

        ResultBox.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FF0000");
    }

    ResultBox.Text = str;
10
  • P.s.w.g is correct. With this data you have no idea where the first IP ends and the second one starts.
    – HaukurHaf
    Nov 12, 2013 at 21:23
  • If you are sure that there will always be a 144, split the string at 144 and prefix the resulting string with it.
    – Abhinav
    Nov 12, 2013 at 21:24
  • No, str = str + psObject; won't split as a newline, you append them. Use an array or a list to store them, or append a newline delimiter if you still want that as only one string. Nov 12, 2013 at 21:24
  • It is 144.68.205.19 and 144.68.205.22. They just put it together in 1 line. I just want to to put new line instead of putting together.
    – StudentIT
    Nov 12, 2013 at 21:25
  • is psObject the entire IP Address? if so just add \r\n: str = str + psObject +"\r\n";
    – Sorceri
    Nov 12, 2013 at 21:26

2 Answers 2

2

You could just insert a new line as you're building up str:

foreach (PSObject psObject in output)
{
    str += "\n" + psObject;
}

Or use string.Join

string str = String.Join("\n", output);
4
  • It doesn't split at all
    – StudentIT
    Nov 12, 2013 at 21:59
  • What type is ResultBox?
    – p.s.w.g
    Nov 12, 2013 at 22:03
  • ResultBox is a asp Textbox with Textmode="MultiLine"
    – StudentIT
    Nov 12, 2013 at 22:05
  • My apologies. The reason it doesn't work is because I didn't rebuild the solution and it haven't updated for long time. I created a new file, copied the code then use your example and it worked. Thank you
    – StudentIT
    Nov 14, 2013 at 14:05
1

For starters, if you're planning on building a string in a loop, look at using StringBuilder. Secondly, you're never appending a new line in there, but that's easily solved (using a native method of stringbuilder, too!)

pipeline.Commands.Add(invokeScript);
Collection<PSObject> output = pipeline.Invoke();

StringBuilder sb = new StringBuilder();
foreach (PSObject psObject in output)
{
    sb.AppendLine(psObject.ToString());
}
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.