none
Powershell script not activating RRS feed

  • Question

  • Currently, I'm using this code with the Src and Des as parameters to be passed to my powershell script:

    var SCRIPT_PATH = @"C:\MyTesting\Untitled2.ps1";
                var src = @"C:\MyTesting\Src";
                var des = @"C:\MyTesting\Des";
                var script = $@". ""{SCRIPT_PATH}""; MoveCompressFiles -Des ""{des}"" -Src ""{src}""";

                using (PowerShell ps = PowerShell.Create())
                {

                    ps.AddScript(script);
                    ps.Invoke();
                    Console.WriteLine(ps.HadErrors.ToString() + "--" + ps.Streams.Error.ToString());
                }

    however nothing seems to be happening but when i use the ps.Streams.Error.ToString() , it gives me the following error:

    System.Management.Automation.PSDataCollection`1[System.Management.Automation.ErrorRecord].

    Any ideas on what could be the cause?

    Wednesday, November 20, 2019 9:41 AM

All replies

  • The `Error` property is a collection containing ErrorRecord entries. You'll need to enumerate that collection to get the individual errors. That type contains everything you need to figure out what went wrong.

    Additionally be aware that Invoke can throw exceptions so you really need to be wrapping this in a try-catch as well. Refer to the docs for the possible errors.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, November 20, 2019 2:49 PM
    Moderator