NuDeploy.Core.Services.Installation.PowerShell.PowerShellExecutor.ExecuteScript C# (CSharp) Method

ExecuteScript() public method

public ExecuteScript ( string scriptPath ) : IServiceResult
scriptPath string
return IServiceResult
        public IServiceResult ExecuteScript(string scriptPath, params string[] parameters)
        {
            if (string.IsNullOrWhiteSpace(scriptPath))
            {
                return new FailureResult(Resources.PowerShellExecutor.ScriptPathCannotBeNullOrEmpty);
            }

            using (var powerShellSession = this.powerShellSessionFactory.GetSession())
            {
                if (powerShellSession == null)
                {
                    return new FailureResult(
                        Resources.PowerShellExecutor.CannotCreatePowerShellSessionMessageTemplate, scriptPath, string.Join(", ", parameters));
                }

                try
                {
                    string scriptOutput = powerShellSession.ExecuteScript(scriptPath, parameters);
                    return new SuccessResult(
                        Resources.PowerShellExecutor.ScriptHasBeenSuccessfullyExecutedMessageTemplate, scriptPath, string.Join(", ", parameters), scriptOutput);
                }
                catch (Exception powerShellException)
                {
                    return new FailureResult(
                        Resources.PowerShellExecutor.ScriptExecutionFailedWithExceptionMessageTemplate,
                        scriptPath,
                        string.Join(", ", parameters),
                        powerShellException.Message);
                }
            }
        }

Usage Example

        public void ExecuteScript_ScriptPathIsInvalid_ResultIsFalse(string scriptPath)
        {
            // Arrange
            var powerShellSessionFactory = new Mock<IPowerShellSessionFactory>();
            var powerShellExecutor = new PowerShellExecutor(powerShellSessionFactory.Object);

            // Act
            var result = powerShellExecutor.ExecuteScript(scriptPath);

            // Assert
            Assert.AreEqual(result.Status, ServiceResultType.Failure);
        }
All Usage Examples Of NuDeploy.Core.Services.Installation.PowerShell.PowerShellExecutor::ExecuteScript