2

Im currently using C# to pass some objects to powershell and executing a method on them like this:

    var objs= new PSDataCollection<CustomObj> {obj};

    using (var ps = PowerShell.Create())
    {
        ps.Runspace.SessionStateProxy.SetVariable("objList", objs);
        ps.AddScript(@"$objList| ForEach { $_.Run()}");
        ps.AddCommand("Out-String");
        var output = ps.Invoke();

        var stringBuilder = new StringBuilder();
        foreach (var obj in output)
        {
            stringBuilder.AppendLine(obj.ToString());
        }

        var result = stringBuilder.ToString().Trim();

        //Evaluate result.
    }

Question:

Is there a simply way to save my ps object as an script file without doing a lot of writeline? I was thinking something in the lines of:

    ps.SaveAsScript(filename);

This would be a great help in a lot of my work with automation, if this is possible in a simple way.:)

Kind regards

1
  • any final solution with full source ? what is {obj} ? thx Commented May 24, 2012 at 10:03

1 Answer 1

3

No there is not. You will have to get the commands ( ps.Commands.Commands) and write to a file as needed.

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.