Пример #1
0
        public async void RunScriptAsync_CorrectlyPassesVariablesToScript()
        {
            var lf = TestLoggerFactory.Create();

            var script = @"1..6 | % { Write-Output (Get-Variable -Name ""Var$_"").Value }";

            var entity = Entity.Create(
                ("Var1", "value1"),
                ("Var2", 2),
                ("Var3", 3.3),
                ("Var4", true),
                ("Var5", Core.SCLType.Enum),
                ("Var6", new DateTime(2020, 12, 12))
                );

            var result = await PwshRunner.RunScriptAsync(script, lf.CreateLogger("Test"), entity, null)
                         .ToListAsync();

            for (var i = 0; i < entity.Dictionary.Count; i++)
            {
                Assert.Equal(
                    entity.Dictionary[$"Var{i + 1}"].BestValue.ObjectValue,
                    result[i].BaseObject
                    );
            }
        }
Пример #2
0
        public async void RunScriptAsync_ReadsDataFromInputStream()
        {
            var lf       = TestLoggerFactory.Create();
            var script   = @"$Input | ForEach { Write-Output $_ }";
            var expected = new[] { 32, 120, 71, 89, 20 };

            var input = new PSDataCollection <PSObject>(5);

            _ = Task.Run(
                () =>
            {
                foreach (var num in expected)
                {
                    input.Add(num);
                    Thread.Sleep(num);
                }

                input.Complete();
            }
                );

            var result = await PwshRunner.RunScriptAsync(script, lf.CreateLogger("Test"), null, input)
                         .ToListAsync();

            var actual = result.Select(o => (int)o.BaseObject).ToArray();

            Assert.Equal(expected, actual);
        }
Пример #3
0
        public async void RunScriptAsync_ReadsDataFromOutputStream()
        {
            var logger = TestLoggerFactory.Create().CreateLogger("Test");
            var script = @"Write-Output 'one'; Write-Output 2";

            var result = await PwshRunner.RunScriptAsync(script, logger).ToListAsync();

            Assert.Equal(new List <PSObject> {
                "one", 2
            }, result);
        }
Пример #4
0
        public async void RunScriptAsync_LogsErrorsWarningsAndInformation()
        {
            var lf = TestLoggerFactory.Create();

            var script =
                @"Write-Output 'one'; Write-Error 'error'; Write-Output 'two'; Write-Warning 'warning'; Write-Information 'info'";

            _ = await PwshRunner.RunScriptAsync(script, lf.CreateLogger("Test")).ToListAsync();

            Assert.Equal(3, lf.Sink.LogEntries.Count());
            Assert.Contains(lf.Sink.LogEntries, o => o.Message != null && o.Message.Equals("error"));
            Assert.Contains(lf.Sink.LogEntries, o => o.Message != null && o.Message.Equals("warning"));
            Assert.Contains(lf.Sink.LogEntries, o => o.Message != null && o.Message.Equals("info"));
        }