示例#1
0
 protected override void ProcessRecord()
 {
     System.Management.Automation.ErrorRecord errorRecord = this.ErrorRecord;
     if (errorRecord != null)
     {
         errorRecord = new System.Management.Automation.ErrorRecord(errorRecord, null);
     }
     else
     {
         System.Exception exception = this.Exception;
         string message = this.Message;
         if (exception == null)
         {
             exception = new WriteErrorException(message);
         }
         string errorId = this.ErrorId;
         if (string.IsNullOrEmpty(errorId))
         {
             errorId = exception.GetType().FullName;
         }
         errorRecord = new System.Management.Automation.ErrorRecord(exception, errorId, this.Category, this.TargetObject);
         if ((this.Exception != null) && !string.IsNullOrEmpty(message))
         {
             errorRecord.ErrorDetails = new ErrorDetails(message);
         }
     }
     string recommendedAction = this.RecommendedAction;
     if (!string.IsNullOrEmpty(recommendedAction))
     {
         if (errorRecord.ErrorDetails == null)
         {
             errorRecord.ErrorDetails = new ErrorDetails(errorRecord.ToString());
         }
         errorRecord.ErrorDetails.RecommendedAction = recommendedAction;
     }
     if (!string.IsNullOrEmpty(this.CategoryActivity))
     {
         errorRecord.CategoryInfo.Activity = this.CategoryActivity;
     }
     if (!string.IsNullOrEmpty(this.CategoryReason))
     {
         errorRecord.CategoryInfo.Reason = this.CategoryReason;
     }
     if (!string.IsNullOrEmpty(this.CategoryTargetName))
     {
         errorRecord.CategoryInfo.TargetName = this.CategoryTargetName;
     }
     if (!string.IsNullOrEmpty(this.CategoryTargetType))
     {
         errorRecord.CategoryInfo.TargetType = this.CategoryTargetType;
     }
     InvocationInfo variableValue = base.GetVariableValue("MyInvocation") as InvocationInfo;
     if (variableValue != null)
     {
         errorRecord.SetInvocationInfo(variableValue);
         errorRecord.PreserveInvocationInfoOnce = true;
         errorRecord.CategoryInfo.Activity = "Write-Error";
     }
     base.WriteError(errorRecord);
 }
示例#2
0
        /// <summary>
        /// Write an error to the output pipe, or throw a terminating error.
        /// </summary>
        protected override void ProcessRecord()
        {
            ErrorRecord errorRecord = this.ErrorRecord;

            if (null != errorRecord)
            {
                // copy constructor
                errorRecord = new ErrorRecord(errorRecord, null);
            }
            else
            {
                Exception e   = this.Exception;
                string    msg = Message;
                if (null == e)
                {
                    e = new WriteErrorException(msg);
                }
                string errid = ErrorId;
                if (String.IsNullOrEmpty(errid))
                {
                    errid = e.GetType().FullName;
                }
                errorRecord = new ErrorRecord(
                    e,
                    errid,
                    Category,
                    TargetObject
                    );

                if ((null != this.Exception && !String.IsNullOrEmpty(msg)))
                {
                    errorRecord.ErrorDetails = new ErrorDetails(msg);
                }
            }

            string recact = RecommendedAction;

            if (!String.IsNullOrEmpty(recact))
            {
                if (null == errorRecord.ErrorDetails)
                {
                    errorRecord.ErrorDetails = new ErrorDetails(errorRecord.ToString());
                }
                errorRecord.ErrorDetails.RecommendedAction = recact;
            }

            if (!String.IsNullOrEmpty(CategoryActivity))
            {
                errorRecord.CategoryInfo.Activity = CategoryActivity;
            }
            if (!String.IsNullOrEmpty(CategoryReason))
            {
                errorRecord.CategoryInfo.Reason = CategoryReason;
            }
            if (!String.IsNullOrEmpty(CategoryTargetName))
            {
                errorRecord.CategoryInfo.TargetName = CategoryTargetName;
            }
            if (!String.IsNullOrEmpty(CategoryTargetType))
            {
                errorRecord.CategoryInfo.TargetType = CategoryTargetType;
            }

            /* 2005/01/25 removing throw-error
             * if (_terminating)
             * {
             *  ThrowTerminatingError(errorRecord);
             * }
             * else
             * {
             */

            // 2005/07/14-913791 "write-error output is confusing and misleading"
            // set InvocationInfo to the script not the command
            InvocationInfo myInvocation = GetVariableValue(SpecialVariables.MyInvocation) as InvocationInfo;

            if (null != myInvocation)
            {
                errorRecord.SetInvocationInfo(myInvocation);
                errorRecord.PreserveInvocationInfoOnce = true;
                if (!String.IsNullOrEmpty(CategoryActivity))
                {
                    errorRecord.CategoryInfo.Activity = CategoryActivity;
                }
                else
                {
                    errorRecord.CategoryInfo.Activity = "Write-Error";
                }
            }

            WriteError(errorRecord);

            /*
             * }
             */
        } //processrecord
        protected override void ProcessRecord()
        {
            System.Management.Automation.ErrorRecord errorRecord = this.ErrorRecord;
            if (errorRecord != null)
            {
                errorRecord = new System.Management.Automation.ErrorRecord(errorRecord, null);
            }
            else
            {
                System.Exception exception = this.Exception;
                string           message   = this.Message;
                if (exception == null)
                {
                    exception = new WriteErrorException(message);
                }
                string errorId = this.ErrorId;
                if (string.IsNullOrEmpty(errorId))
                {
                    errorId = exception.GetType().FullName;
                }
                errorRecord = new System.Management.Automation.ErrorRecord(exception, errorId, this.Category, this.TargetObject);
                if ((this.Exception != null) && !string.IsNullOrEmpty(message))
                {
                    errorRecord.ErrorDetails = new ErrorDetails(message);
                }
            }
            string recommendedAction = this.RecommendedAction;

            if (!string.IsNullOrEmpty(recommendedAction))
            {
                if (errorRecord.ErrorDetails == null)
                {
                    errorRecord.ErrorDetails = new ErrorDetails(errorRecord.ToString());
                }
                errorRecord.ErrorDetails.RecommendedAction = recommendedAction;
            }
            if (!string.IsNullOrEmpty(this.CategoryActivity))
            {
                errorRecord.CategoryInfo.Activity = this.CategoryActivity;
            }
            if (!string.IsNullOrEmpty(this.CategoryReason))
            {
                errorRecord.CategoryInfo.Reason = this.CategoryReason;
            }
            if (!string.IsNullOrEmpty(this.CategoryTargetName))
            {
                errorRecord.CategoryInfo.TargetName = this.CategoryTargetName;
            }
            if (!string.IsNullOrEmpty(this.CategoryTargetType))
            {
                errorRecord.CategoryInfo.TargetType = this.CategoryTargetType;
            }
            InvocationInfo variableValue = base.GetVariableValue("MyInvocation") as InvocationInfo;

            if (variableValue != null)
            {
                errorRecord.SetInvocationInfo(variableValue);
                errorRecord.PreserveInvocationInfoOnce = true;
                errorRecord.CategoryInfo.Activity      = "Write-Error";
            }
            base.WriteError(errorRecord);
        }
示例#4
0
文件: write.cs 项目: 40a/PowerShell
        /// <summary>
        /// Write an error to the output pipe, or throw a terminating error.
        /// </summary>
        protected override void ProcessRecord()
        {
            ErrorRecord errorRecord = this.ErrorRecord;
            if (null != errorRecord)
            {
                // copy constructor
                errorRecord = new ErrorRecord(errorRecord, null);
            }
            else
            {
                Exception e = this.Exception;
                string msg = Message;
                if (null == e)
                {
                    e = new WriteErrorException(msg);
                }
                string errid = ErrorId;
                if (String.IsNullOrEmpty(errid))
                {
                    errid = e.GetType().FullName;
                }
                errorRecord = new ErrorRecord(
                    e,
                    errid,
                    Category,
                    TargetObject
                    );

                if ((null != this.Exception && !String.IsNullOrEmpty(msg)))
                {
                    errorRecord.ErrorDetails = new ErrorDetails(msg);
                }
            }

            string recact = RecommendedAction;
            if (!String.IsNullOrEmpty(recact))
            {
                if (null == errorRecord.ErrorDetails)
                {
                    errorRecord.ErrorDetails = new ErrorDetails(errorRecord.ToString());
                }
                errorRecord.ErrorDetails.RecommendedAction = recact;
            }

            if (!String.IsNullOrEmpty(CategoryActivity))
                errorRecord.CategoryInfo.Activity = CategoryActivity;
            if (!String.IsNullOrEmpty(CategoryReason))
                errorRecord.CategoryInfo.Reason = CategoryReason;
            if (!String.IsNullOrEmpty(CategoryTargetName))
                errorRecord.CategoryInfo.TargetName = CategoryTargetName;
            if (!String.IsNullOrEmpty(CategoryTargetType))
                errorRecord.CategoryInfo.TargetType = CategoryTargetType;

            /* 2005/01/25 removing throw-error
            if (_terminating)
            {
                ThrowTerminatingError(errorRecord);
            }
            else
            {
            */

            // 2005/07/14-913791 "write-error output is confusing and misleading"
            // set InvocationInfo to the script not the command
            InvocationInfo myInvocation = GetVariableValue(SpecialVariables.MyInvocation) as InvocationInfo;
            if (null != myInvocation)
            {
                errorRecord.SetInvocationInfo(myInvocation);
                errorRecord.PreserveInvocationInfoOnce = true;
                errorRecord.CategoryInfo.Activity = "Write-Error";
            }

            WriteError(errorRecord);
            /*
            }
            */
        }//processrecord