public void ShouldReturnFalseIfItContainsAnError()
 {
     PowershellHelpers.Validate(@"param(a,)")
     .Should()
     .Be
     .False();
 }
 public void ShouldReturnTrueIfValid()
 {
     PowershellHelpers.Validate(@"Write-Host $PsVersion")
     .Should()
     .Be
     .True();
 }
 public void ShouldReturnParseErrorsIfPresent()
 {
     PowershellHelpers.GetParseErrors(@"param(a,)")
     .Count
     .Should()
     .Be
     .EqualTo(3);
 }
 public void ShouldReturnEmptyCollectionIfScriptCorrect()
 {
     PowershellHelpers.GetParseErrors(@"Write-Host $PsVersion")
     .Count
     .Should()
     .Be
     .EqualTo(0);
 }
            public void ShouldLogErrorIfExitCode()
            {
                var logger     = new StringLogger();
                var parameters = new Dictionary <string, string>();

                PowershellHelpers.ExecuteScript(".", "throw \"test\"", logger, parameters);

                logger.Logs.Count.Should().Be.EqualTo(1);
                logger.Logs.ElementAt(0).Error.Should().Be.True();
            }
Example #6
0
        public void SetUp()
        {
            InstallUtil.Install("Zookeeper.PSProvider");
            this.zookeeper = new ZookeeperHelpers();
            this.zookeeper.CleanZookeeper();

            this.powershell = new PowershellHelpers();
            this.powershell.AddScript("Add-PSSnapin ZookeeperPSSnap");
            this.powershell.AddScript("New-PSDrive -Name Zookeeper -PSProvider Zookeeeper -Root /");
            this.powershell.AddScript("cd Zookeeper:");
        }
            public void ShouldLogErrorIfWriteError()
            {
                var logger     = new StringLogger();
                var parameters = new Dictionary <string, string>();

                PowershellHelpers.ExecuteScript(".", "Write-Error 'test'", logger, parameters);

                logger.Logs.Count.Should().Be.EqualTo(1);
                logger.Logs.ElementAt(0).Text.Should().Be.EqualTo("test");
                logger.Logs.ElementAt(0).Error.Should().Be.True();
            }
            public void ShouldLogOutputofCommand()
            {
                var logger     = new StringLogger();
                var parameters = new Dictionary <string, string>();

                PowershellHelpers.ExecuteScript(".", "&echo 'test'", logger, parameters);

                logger.Logs.Count.Should().Be.EqualTo(1);
                logger.Logs.ElementAt(0).Text.Should().Be.EqualTo("test");
                logger.Logs.ElementAt(0).Error.Should().Be.False();
            }
            public void ShouldLogOutputIfWriteOutput()
            {
                var logger     = new StringLogger();
                var parameters = new Dictionary <string, string>();

                parameters.Add("username", "paolo");

                PowershellHelpers.ExecuteScript(".", "Write-Output $username", logger, parameters);

                logger.Logs.Count.Should().Be.EqualTo(1);
                logger.Logs.ElementAt(0).Text.Should().Be.EqualTo("paolo");
                logger.Logs.ElementAt(0).Error.Should().Be.False();
            }
            public void ShouldRunScriptAndError()
            {
                var logger     = new StringLogger();
                var parameters = new Dictionary <string, string>();

                parameters.Add("username", "paolo");
                parameters.Add("password", "240686pn");

                var path     = Path.GetTempPath();
                var fileName = Path.GetRandomFileName();

                File.WriteAllText(Path.Combine(path, fileName + ".ps1"), "Write-Error 'test'");
                PowershellHelpers.Execute(path, fileName + ".ps1", "test", logger, parameters);

                logger.Logs.Count.Should().Be.EqualTo(1);
                logger.Logs.ElementAt(0).Error.Should().Be.True();
            }
            public void ShouldLogOrderedOutputRecognizingLevel()
            {
                var logger     = new StringLogger();
                var parameters = new Dictionary <string, string>();
                var script     = @"Write-Output 'test'
Write-Error 'test2'
Write-Output 'test3'";

                PowershellHelpers.ExecuteScript(".", script, logger, parameters);

                logger.Logs.Count.Should().Be.EqualTo(3);
                logger.Logs.ElementAt(0).Text.Should().Be.EqualTo("test");
                logger.Logs.ElementAt(1).Text.Should().Be.EqualTo("test2");
                logger.Logs.ElementAt(2).Text.Should().Be.EqualTo("test3");
                logger.Logs.ElementAt(0).Error.Should().Be.False();
                logger.Logs.ElementAt(1).Error.Should().Be.True();
                logger.Logs.ElementAt(2).Error.Should().Be.False();
            }
Example #12
0
        private static void LocalScriptTask(int id, int deploymentId, string version, string config)
        {
            using (var dc = new ReadContext())
            {
                var unit   = dc.LocalScriptTasks.Single(x => x.Id == id);
                var cwd    = Environment.CurrentDirectory;
                var logger = new StringLogger();
                using (var session = Store.OpenSession())
                {
                    session.Save(new DataAccess.Write.LogEntry()
                    {
                        DeploymentId = deploymentId,
                        Error        = false,
                        TaskName     = unit.Name,
                        Text         = "Executing local script " + unit.Name,
                        TimeStamp    = DateTime.UtcNow
                    });

                    session.Flush();
                }

                PowershellHelpers.ExecuteScript(cwd, unit.Script, logger, new Dictionary <string, string>());

                using (var session = Store.OpenSession())
                {
                    session.Save(new DataAccess.Write.LogEntry()
                    {
                        DeploymentId = deploymentId,
                        Error        = false,
                        TaskName     = unit.Name,
                        Text         = String.Join("\r\n", logger.Logs.Select(x => x.Text)),
                        TimeStamp    = DateTime.UtcNow
                    });

                    session.Flush();
                }
            }
        }
Example #13
0
        public ExecutionResult ExecuteTemplatedTask(TemplatedTaskDto options)
        {
            var logger = new StringLogger();

            try
            {
                PowershellHelpers.ExecuteScript(".", options.Script, logger, options.Parameters.ToDictionary(x => x.Name, x => x.Value));
            }
            catch (Exception e)
            {
                return(new ExecutionResult()
                {
                    Exception = e.InnerException == null ? e.Message : e.InnerException.Message,
                    Success = false,
                    Log = logger.Logs
                });
            }
            return(new ExecutionResult()
            {
                Success = logger.Logs.All(x => !x.Error),
                Log = logger.Logs
            });
        }
Example #14
0
        public ExecutionResult ExecuteScript(RemoteScriptDto options)
        {
            var logger = new StringLogger();

            try
            {
                PowershellHelpers.ExecuteScript(options.Folder, options.Script, logger, new Dictionary <string, string>());
            }
            catch (Exception e)
            {
                return(new ExecutionResult()
                {
                    Exception = e.InnerException == null ? e.Message : e.InnerException.Message,
                    Success = false,
                    Log = logger.Logs
                });
            }
            return(new ExecutionResult()
            {
                Success = logger.Logs.All(x => !x.Error),
                Log = logger.Logs
            });
        }