Example #1
0
        public void VariablePlain_Test__Equal()
        {
            VariablePlain vp  = new VariablePlain("Name One");
            VariablePlain vp2 = new VariablePlain("NameOne");

            Assert.Equal(true, vp.Equals(vp2));
        }
Example #2
0
        public void VariablePlain_Test__Name()
        {
            string test = "NameFromConstructor";

            VariablePlain vp = new VariablePlain(test);

            Assert.Equal(test, vp.Name);

            vp.Name = " Special \r\n  รถ$()[]- Case ?  ";
            Assert.Equal("SpecialCase", vp.Name);
        }
Example #3
0
        //MTH: This is done in a constructor and not a function to make sure the consumer either gets a working instance or no instance (null) at all.
        protected internal ExecutionResult(PowerShell psInstance, HashSet<VariablePlain> vars, PSDataCollection<PSObject> psOutput, Exception additionalException)
        {
            Successful = false;
            SuccessfulNoError = false;

            //It can happen that PSInstance is NULL
            if (psInstance != null)
            {
                _pipelineState = psInstance.InvocationStateInfo.State;

                //Check if we have an inner or outer exception
                if (additionalException == null && psInstance.InvocationStateInfo.Reason == null)
                {
                    FailedException = null;
                }
                else
                {
                    //At least one excpetion has been detected. The additonal exception is raised by the the call of .EndInvoke() and is therefore considered to be more important.
                    if (additionalException != null)
                    {
                        FailedException = additionalException;
                    }
                    else
                    {
                        FailedException = psInstance.InvocationStateInfo.Reason;
                    }
                }

            }
            else
            {
                //PSInstance is null - execution can not be successful in this case
                _pipelineState = PSInvocationState.NotStarted;

                if (additionalException != null)
                {
                    FailedException = additionalException;
                }
                else
                {
                    //OK, this is strange. We do not have a PSInstance but also no AdditonalException. Set a default value.
                    FailedException = new Exception("No detailed exception available");
                }
            }

            //If FailedException is not set, we can check if the execution was successful
            if (FailedException == null)
            {
                //No exception detected, if State is Complete we have a succesful execution.
                if (psInstance.InvocationStateInfo.State == PSInvocationState.Completed)
                {
                    Successful = true;

                    //Now also check the ERROR stream. If there are no error reported, we also have a SuccessfulNoError status
                    if (psInstance.Streams.Error != null)
                    {
                        if (psInstance.Streams.Error.Count == 0)
                        {
                            SuccessfulNoError = true;
                        }

                    }
                    else
                    {
                        //Error stream is NULL, hence no problem
                        SuccessfulNoError = true;
                    }

                }
            }

            //MTH: Assign the stream collections to our internal properties or create empty one if they are null.
            //This will make using this object easier since the consumer does not need to check if the collections are null.
            StreamOutput = psOutput ?? new PSDataCollection<PSObject>();

            //MTH: I didn't created a default value for all the StreamXXX properties here because PSDataCollection implements IDisposable. Therefore this odd looking code.
            if (psInstance == null)
            {
                //When PS Instance is NULL, set all values to default
                StreamError = new PSDataCollection<ErrorRecord>(); //http://msdn.microsoft.com/en-us/library/System.Management.Automation.ErrorRecord%28v=vs.85%29.aspx
                StreamWarning = new PSDataCollection<WarningRecord>();  //http://msdn.microsoft.com/en-us/library/system.management.automation.warningrecord%28v=vs.85%29.aspx
                StreamVerbose = new PSDataCollection<VerboseRecord>(); //http://msdn.microsoft.com/en-us/library/system.management.automation.verboserecord%28v=vs.85%29.aspx
                StreamDebug = new PSDataCollection<DebugRecord>();  //http://msdn.microsoft.com/en-us/library/system.management.automation.debugrecord%28v=vs.85%29.aspx

                //Set the variables to the same collection which was passed in
                Variables = vars;
            }
            else
            {
                //PSInstance is not null, so we can check the streams
                StreamError = psInstance.Streams.Error ?? new PSDataCollection<ErrorRecord>();
                StreamWarning = psInstance.Streams.Warning ?? new PSDataCollection<WarningRecord>();
                StreamVerbose = psInstance.Streams.Verbose ?? new PSDataCollection<VerboseRecord>();
                StreamDebug = psInstance.Streams.Debug ?? new PSDataCollection<DebugRecord>();

                //Return the current value of the Variables that were passed in.
                Variables = new HashSet<VariablePlain>();

                if (vars.Count > 0)
                {
                    foreach (VariablePlain var in vars)
                    {
                        //If the value was marked as ReadOnly, we simply take it from the original hashset because PowerShell can't change it anyway
                        if (var.ReadOnly)
                        {
                            Variables.Add(var);
                        }
                        else
                        {
                            //The variable could have been changed by the script, so retrieve it from PowerShell

                            /* MTH: There are two functions to read variables from PowerShell:
                             * PSVariable psv = PSInstance.Runspace.SessionStateProxy.PSVariable.Get("aTestVar"); //Docs: http://msdn.microsoft.com/en-us/library/system.management.automation.runspaces.sessionstateproxy.psvariable%28v=vs.85%29.aspx
                             * Object obj = PSInstance.Runspace.SessionStateProxy.GetVariable("aTestVar"); //Docs: http://msdn.microsoft.com/en-us/library/system.management.automation.runspaces.sessionstateproxy.getvariable%28v=vs.85%29.aspx
                             * I assume that the second one is the short-circuit version of the first one. Since we only want the value, we use it.
                             */

                            Object value = psInstance.Runspace.SessionStateProxy.GetVariable(var.Name);
                            VariablePlain new_var = new VariablePlain(var.Name, value);
                            Variables.Add(new_var);
                        }

                    }
                }

            }
        }