/// <summary>
        /// MisleadingBacktick: Checks that lines don't end with a backtick followed by a whitespace
        /// </summary>
        public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
        {
            if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);

            string[] lines = NewlineRegex.Split(ast.Extent.Text);

            if((ast.Extent.EndLineNumber - ast.Extent.StartLineNumber + 1) != lines.Length)
            {
                // Did not match the number of lines that the extent indicated
                yield break;
            }

            foreach (int i in Enumerable.Range(0, lines.Length))
            {
                string line = lines[i];

                Match match = TrailingEscapedWhitespaceRegex.Match(line);

                if(match.Success)
                {
                    int lineNumber = ast.Extent.StartLineNumber + i;

                    var start = new ScriptPosition(fileName, lineNumber, match.Index + 1, line);
                    var end = new ScriptPosition(fileName, lineNumber, match.Index + match.Length + 1, line);
                    var extent = new ScriptExtent(start, end);
                    yield return new DiagnosticRecord(
                        string.Format(CultureInfo.CurrentCulture, Strings.MisleadingBacktickError),
                            extent,
                            GetName(),
                            DiagnosticSeverity.Warning,
                            fileName,
                            suggestedCorrections: fileName == null ? null : GetCorrectionExtent(extent));
                }
            }
        }
Example #2
0
        internal static ScriptExtent FromPSObjectForRemoting(PSObject serializedScriptExtent)
        {
            ScriptExtent extent = new ScriptExtent();

            extent.PopulateFromSerializedInfo(serializedScriptExtent);
            return(extent);
        }
Example #3
0
        private void HandleWorkflowApplicationFaultedState(Exception e)
        {
            if (System.Threading.Interlocked.CompareExchange(ref terminalStateHandled, Handled, NotHandled) == Handled)
                return;

            Tracer.WriteMessage("Workflow Application is completed in Faulted state.");

            // there might be possible of race condition here in case of Winrm shutdown. if the activity is 
            // executing on loop back winrm process so there might be possibility that the winrm shuts down the 
            // activity process fist and causing the workflow to fail in the M3P process.
            // in order to avoid those situations we will ignore the remote exception if the shutdown is in progress
            if (WorkflowJobSourceAdapter.GetInstance().IsShutdownInProgress)
            {
                if (e.GetType() == typeof(RemoteException))
                {
                    Tracer.WriteMessage("PSWorkflowApplicationInstance", "HandleWorkflowApplicationFaultedState", id, "Since we are in shuting down mode so ignoring the remote exception");
                    Tracer.TraceException(e);

                    // no need to proceed any further
                    return;
                }
            }

            // before we start handling the faulted state we need to cancel
            // all running activities. Activities may be running if there is a 
            // parallel statement involved            
            StopAllAsyncExecutions();

            State = JobState.Failed;
            Exception reason = null;

            if (!_suppressTerminateError)
            {
                reason = e;

                ErrorRecord failureErrorRecord = GetInnerErrorRecord(reason);
                if (failureErrorRecord != null)
                {
                    reason = failureErrorRecord.Exception;

                    if (PSWorkflowJob.SynchronousExecution && Streams.ErrorStream.IsOpen)
                    {
                        // If we're running synchronously from the commandline, we want to see the error
                        // record from the workflow without the Receive-Job decoration. Receive-Job will
                        // decorate the reason, so do not send it as the reason.
                        // Note that if the error collection is closed, we do not want to lose this data, so
                        // pass the exception despite the bad decoration.
                        Streams.ErrorStream.Add(failureErrorRecord);
                        reason = null;
                    }
                }
                else
                {
                    // No error record, a raw exception was thrown. See if we have location
                    // information that we can wrap it in.
                    HostSettingCommandMetadata commandMetadata = null;

                    if (_paramDefaults != null)
                        commandMetadata = _paramDefaults.HostCommandMetadata;

                    if (commandMetadata != null)
                    {
                        ScriptPosition scriptStart = new ScriptPosition(
                            commandMetadata.CommandName,
                            commandMetadata.StartLineNumber,
                            commandMetadata.StartColumnNumber,
                            null);
                        ScriptPosition scriptEnd = new ScriptPosition(
                            commandMetadata.CommandName,
                            commandMetadata.EndLineNumber,
                            commandMetadata.EndColumnNumber,
                            null);
                        ScriptExtent extent = new ScriptExtent(scriptStart, scriptEnd);

                        reason = new JobFailedException(reason, extent);
                    }
                }
            }

            this.Error = reason;

            this.PerformTaskAtTerminalState();

            // do all cleanups here to save memory
            PerformCleanupAtTerminalState();

            if (this.OnFaulted != null)
                this.OnFaulted(reason, this);
        }
Example #4
0
        /// <summary>
        /// Creates an InformationalRecord from an instance serialized as a PSObject by ToPSObjectForRemoting.
        /// </summary>
        internal InvocationInfo(PSObject psObject)
        {
            CommandOrigin = (CommandOrigin)SerializationUtilities.GetPsObjectPropertyBaseObject(psObject, "InvocationInfo_CommandOrigin");
            ExpectingInput = (bool)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_ExpectingInput");
            _invocationName = (string)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_InvocationName");
            HistoryId = (long)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_HistoryId");
            PipelineLength = (int)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_PipelineLength");
            PipelinePosition = (int)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_PipelinePosition");

            string scriptName = (string)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_ScriptName");
            int scriptLineNumber = (int)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_ScriptLineNumber");
            int offsetInLine = (int)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_OffsetInLine");
            string line = (string)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_Line");
            var scriptPosition = new ScriptPosition(scriptName, scriptLineNumber, offsetInLine, line);

            ScriptPosition scriptEndPosition;
            if (!string.IsNullOrEmpty(line))
            {
                int endColumn = line.Length + 1;
                scriptEndPosition = new ScriptPosition(scriptName, scriptLineNumber, endColumn, line);
            }
            else
            {
                scriptEndPosition = scriptPosition;
            }
            _scriptPosition = new ScriptExtent(scriptPosition, scriptEndPosition);

            MyCommand = RemoteCommandInfo.FromPSObjectForRemoting(psObject);

            //
            // Arrays are de-serialized as ArrayList so we need to convert the deserialized 
            // object into an int[] before assigning to pipelineIterationInfo.
            //
            var list = (ArrayList)SerializationUtilities.GetPsObjectPropertyBaseObject(psObject, "InvocationInfo_PipelineIterationInfo");
            if (list != null)
            {
                PipelineIterationInfo = (int[])list.ToArray(typeof(Int32));
            }
            else
            {
                PipelineIterationInfo = Utils.EmptyArray<int>();
            }

            //
            // Dictionaries are de-serialized as Hashtables so we need to convert the deserialized object into a dictionary
            // before assigning to CommandLineParameters.
            //
            Hashtable hashtable = (Hashtable)SerializationUtilities.GetPsObjectPropertyBaseObject(psObject, "InvocationInfo_BoundParameters");

            Dictionary<string, object> dictionary = new Dictionary<string, object>();

            if (hashtable != null)
            {
                foreach (DictionaryEntry entry in hashtable)
                {
                    dictionary.Add((string)entry.Key, entry.Value);
                }
            }

            _boundParameters = dictionary;

            //
            // The unbound parameters are de-serialized as an ArrayList, which we need to convert to a List
            //
            var unboundArguments = (ArrayList)SerializationUtilities.GetPsObjectPropertyBaseObject(psObject, "InvocationInfo_UnboundArguments");

            _unboundArguments = new List<object>();

            if (unboundArguments != null)
            {
                foreach (object o in unboundArguments)
                {
                    _unboundArguments.Add(o);
                }
            }

            object value = SerializationUtilities.GetPropertyValue(psObject, "SerializeExtent");
            bool serializeExtent = false;

            if (value != null)
                serializeExtent = (bool)value;

            if (serializeExtent)
                DisplayScriptPosition = ScriptExtent.FromPSObjectForRemoting(psObject);
        }
Example #5
0
 internal static ScriptExtent FromPSObjectForRemoting(PSObject serializedScriptExtent)
 {
     ScriptExtent extent = new ScriptExtent();
     extent.PopulateFromSerializedInfo(serializedScriptExtent);
     return extent;
 }
		private void HandleWorkflowApplicationFaultedState(Exception e)
		{
			if (Interlocked.CompareExchange(ref this.terminalStateHandled, 1, 0) != 1)
			{
				this.Tracer.WriteMessage("Workflow Application is completed in Faulted state.");
				if (!WorkflowJobSourceAdapter.GetInstance().IsShutdownInProgress || !(e.GetType() == typeof(RemoteException)))
				{
					this.StopAllAsyncExecutions();
					this.State = JobState.Failed;
					Exception jobFailedException = e;
					ErrorRecord innerErrorRecord = this.GetInnerErrorRecord(jobFailedException);
					if (innerErrorRecord == null)
					{
						HostSettingCommandMetadata hostCommandMetadata = null;
						if (this._paramDefaults != null)
						{
							hostCommandMetadata = this._paramDefaults.HostCommandMetadata;
						}
						if (hostCommandMetadata != null)
						{
							ScriptPosition scriptPosition = new ScriptPosition(hostCommandMetadata.CommandName, hostCommandMetadata.StartLineNumber, hostCommandMetadata.StartColumnNumber, null);
							ScriptPosition scriptPosition1 = new ScriptPosition(hostCommandMetadata.CommandName, hostCommandMetadata.EndLineNumber, hostCommandMetadata.EndColumnNumber, null);
							ScriptExtent scriptExtent = new ScriptExtent(scriptPosition, scriptPosition1);
							jobFailedException = new JobFailedException(jobFailedException, scriptExtent);
						}
					}
					else
					{
						jobFailedException = innerErrorRecord.Exception;
						if (this.PSWorkflowJob.SynchronousExecution && this.Streams.ErrorStream.IsOpen)
						{
							this.Streams.ErrorStream.Add(innerErrorRecord);
							jobFailedException = null;
						}
					}
					this.Error = jobFailedException;
					this.PerformTaskAtTerminalState();
					this.PerformCleanupAtTerminalState();
					if (base.OnFaulted != null)
					{
						base.OnFaulted(jobFailedException, this);
					}
					return;
				}
				else
				{
					this.Tracer.WriteMessage("PSWorkflowApplicationInstance", "HandleWorkflowApplicationFaultedState", this.id, "Sicne we are in shuting down mode so ignoring the remote exception", new string[0]);
					this.Tracer.TraceException(e);
					return;
				}
			}
			else
			{
				return;
			}
		}
Example #7
0
 /// <summary>
 /// Creates a new JobFailedException
 /// </summary>
 /// <param name="innerException">The actual exception that caused this error.</param>
 /// <param name="displayScriptPosition">A ScriptExtent that describes where this error originated from.</param>
 public JobFailedException(Exception innerException, ScriptExtent displayScriptPosition)
 {
     _reason = innerException;
     _displayScriptPosition = displayScriptPosition;
 }
Example #8
0
		private static void PowerShellInvocation_ErrorAdding(object sender, DataAddingEventArgs e, HostSettingCommandMetadata commandMetadata, PSDataCollection<PSObject> output)
		{
			ErrorRecord itemAdded = e.ItemAdded as ErrorRecord;
			if (itemAdded != null)
			{
				if (commandMetadata != null)
				{
					ScriptPosition scriptPosition = new ScriptPosition(commandMetadata.CommandName, commandMetadata.StartLineNumber, commandMetadata.StartColumnNumber, null);
					ScriptPosition scriptPosition1 = new ScriptPosition(commandMetadata.CommandName, commandMetadata.EndLineNumber, commandMetadata.EndColumnNumber, null);
					ScriptExtent scriptExtent = new ScriptExtent(scriptPosition, scriptPosition1);
					if (itemAdded.InvocationInfo != null)
					{
						itemAdded.InvocationInfo.DisplayScriptPosition = scriptExtent;
					}
				}
				if (output != null)
				{
					output.Add(PSObject.AsPSObject(itemAdded));
				}
			}
		}
Example #9
0
 protected JobFailedException(SerializationInfo serializationInfo, StreamingContext streamingContext) : base(serializationInfo, streamingContext)
 {
     this.reason = (Exception) serializationInfo.GetValue("Reason", typeof(Exception));
     this.displayScriptPosition = (ScriptExtent) serializationInfo.GetValue("DisplayScriptPosition", typeof(ScriptExtent));
 }