Пример #1
0
        /// <summary>
        /// Gets the PowerShell credential.
        /// </summary>
        /// <returns>The PowerShell credential object.</returns>
        private PSCredential GetPowerShellCredential()
        {
            if (string.IsNullOrEmpty(this.PowerShellUser) || string.IsNullOrEmpty(this.PowerShellUserPassword))
            {
                return(null);
            }

            string[] userParts = this.PowerShellUser.Split(new string[] { @"\" }, StringSplitOptions.RemoveEmptyEntries);
            if (userParts.Length != 2)
            {
                throw Logger.Instance.ReportError(EventIdentifier.RunPowerShellScriptRunScriptExecutionFailedError, new WorkflowActivityLibraryException(Messages.RunPowerShellActivity_InvalidUserFormat, this.PowerShellUser));
            }

            SecureString password = ProtectedData.DecryptData(this.PowerShellUserPassword);

            return(new PSCredential(this.PowerShellUser, password));
        }
Пример #2
0
        /// <summary>
        /// Validates the inputs. Returns true if all of the UI controls contain valid values. Otherwise, returns false.
        /// </summary>
        /// <returns>true if all of the UI controls contain valid values. Otherwise, false.</returns>
        public override bool ValidateInputs()
        {
            Logger.Instance.WriteMethodEntry();

            try
            {
                if (!this.controller.ValidateInputs())
                {
                    return(false);
                }

                ExpressionEvaluator evaluator = new ExpressionEvaluator();
                switch (GetInputType(this.inputType.Value))
                {
                case PowerShellInputType.Parameters:
                    foreach (DefinitionListing parameter in this.parameters.DefinitionListings.Where(parameter => parameter.Active))
                    {
                        if (parameter.Definition == null)
                        {
                            // If a value is missing for parameter name or value expression, the definition
                            // will be null and the listing fails validation
                            this.controller.ValidationError = ActivitySettings.ScriptParameterDefintionValidationError;
                            return(false);
                        }

                        // Attempt to parse the value expression
                        try
                        {
                            evaluator.ParseExpression(parameter.Definition.Right);
                        }
                        catch (WorkflowActivityLibraryException ex)
                        {
                            this.controller.ValidationError = ex.Message;
                            return(false);
                        }
                    }

                    break;

                case PowerShellInputType.Arguments:
                    foreach (DefinitionListing argument in this.arguments.DefinitionListings.Where(argument => argument.Active))
                    {
                        if (string.IsNullOrEmpty(argument.State.Left))
                        {
                            // If a value is missing for the expression, fail validation
                            this.controller.ValidationError = ActivitySettings.ScriptArgumentValidationError;
                            return(false);
                        }

                        // Attempt to parse the value expression
                        try
                        {
                            evaluator.ParseExpression(argument.State.Left);
                        }
                        catch (WorkflowActivityLibraryException ex)
                        {
                            this.controller.ValidationError = ex.Message;
                            return(false);
                        }
                    }

                    break;
                }

                try
                {
                    if (!string.IsNullOrEmpty(this.activityExecutionCondition.Value))
                    {
                        evaluator.ParseExpression(this.activityExecutionCondition.Value);

                        // Verify that the activity execution condition resolves to a Boolean value
                        if (!evaluator.IsBooleanExpression(this.activityExecutionCondition.Value))
                        {
                            this.controller.ValidationError = ActivitySettings.ActivityExecutionConditionValidationError;
                            return(false);
                        }
                    }
                }
                catch (WorkflowActivityLibraryException ex)
                {
                    this.controller.ValidationError = ex.Message;
                    return(false);
                }

                if (this.impersonatePowerShellUser.Value)
                {
                    if (string.IsNullOrEmpty(this.powerShellUser.Value) ||
                        string.IsNullOrEmpty(this.powerShellUserPassword.Value))
                    {
                        this.controller.ValidationError = ActivitySettings.PowerShellImpersonationSettingsValidationError;
                        return(false);
                    }
                }

                if (!string.IsNullOrEmpty(this.powerShellUser.Value))
                {
                    if (!this.powerShellUser.Value.Contains(@"\"))
                    {
                        this.controller.ValidationError = ActivitySettings.PowerShellUserFormatValidationError;
                        return(false);
                    }

                    if (string.IsNullOrEmpty(this.powerShellUserPassword.Value))
                    {
                        this.controller.ValidationError = ActivitySettings.PowerShellImpersonationSettingsValidationError;
                        return(false);
                    }

                    try
                    {
                        ProtectedData.DecryptData(this.powerShellUserPassword.Value);
                    }
                    catch (WorkflowActivityLibraryException ex)
                    {
                        this.controller.ValidationError = ex.Message;
                        return(false);
                    }
                }

                // If no errors were found, clear any validation error and return true
                this.controller.ValidationError = string.Empty;
                return(true);
            }
            catch (Exception e)
            {
                Logger.Instance.ReportError(e);
                throw;
            }
            finally
            {
                Logger.Instance.WriteMethodExit();
            }
        }