/*
         *  Runs a script on the given PowerShell instance and prints the messages written to info,
         *  error and warning streams asynchronously.
         */
        public static void RunScriptAndPrintOutput(this PowerShell psInstance, string script)
        {
            // Streams can be used by the caller to check for errors in the current script execution
            psInstance.Streams.ClearStreams();

            // By default PowerShell spawns one thread for each command, we must avoid it
            if (psInstance.Runspace.ThreadOptions != PSThreadOptions.UseCurrentThread)
            {
                psInstance.CreateNewSingleThreadedRunspace();
            }

            psInstance.AddOutputStreamsEventHandlers();
            psInstance.AddScript(script);
            psInstance.Invoke();
            psInstance.RemoveOutputStreamsEventHandlers();

            // Clear PowerShell pipeline to avoid the script being re-executed the next time we use this instance
            psInstance.Commands.Clear();
        }