Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
test_HostingBasic.cs225 linesDownload Raw Back to hosting
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System;5using System.IO;6using System.Management.Automation;7using System.Security;8using Microsoft.Management.Infrastructure;9using Microsoft.PowerShell;10using Xunit;11 12namespace PowerShell.Hosting.SDK.Tests13{14    public static class HostingTests15    {16        [Fact]17        public static void TestCommandFromUtility()18        {19            using (System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create())20            {21                var results = ps.AddScript("Get-Verb -Verb get").Invoke();22 23                foreach (dynamic item in results)24                {25                    Assert.Equal("Get", item.Verb);26                }27            }28        }29 30        [Fact]31        public static void TestCommandFromManagement()32        {33            using (System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create())34            {35                var path = Environment.CurrentDirectory;36                var results = ps.AddCommand("Test-Path").AddParameter("Path", path).Invoke<bool>();37 38                foreach (dynamic item in results)39                {40                    Assert.True(item);41                }42            }43        }44 45        [Fact]46        public static void TestCommandFromCore()47        {48            using (System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create())49            {50                var results = ps.AddScript(@"$i = 0 ; 1..3 | ForEach-Object { $i += $_} ; $i").Invoke<int>();51 52                foreach (dynamic item in results)53                {54                    Assert.Equal(6, item);55                }56            }57        }58 59        [SkippableFact]60        public static void TestCommandFromMMI()61        {62            // Test is disabled since we do not have a CimCmdlets module released in the SDK.63            Skip.IfNot(Platform.IsWindows);64            using (System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create())65            {66                var results = ps.AddScript("[Microsoft.Management.Infrastructure.CimInstance]::new('Win32_Process')").Invoke();67                Assert.True(results.Count > 0);68            }69        }70 71        [SkippableFact]72        public static void TestCommandFromDiagnostics()73        {74            Skip.IfNot(Platform.IsWindows);75            using (System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create())76            {77                var results = ps.AddScript("Get-WinEvent -ListLog Application").Invoke();78 79                foreach (dynamic item in results)80                {81                    Assert.Equal("Application", item.LogName);82                }83            }84        }85 86        [SkippableFact]87        public static void TestCommandFromSecurity()88        {89            Skip.IfNot(Platform.IsWindows);90            using (System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create())91            {92                var results = ps.AddScript("ConvertTo-SecureString -String test -AsPlainText -Force").Invoke<SecureString>();93                Assert.IsType<SecureString>(results[0]);94            }95        }96 97        [SkippableFact]98        public static void TestCommandFromWSMan()99        {100            Skip.IfNot(Platform.IsWindows);101            using (System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create())102            {103                var results = ps.AddScript("Test-WSMan").Invoke();104 105                foreach (dynamic item in results)106                {107                    Assert.Equal("Microsoft Corporation", item.ProductVendor);108                }109            }110        }111 112        [Fact]113        public static void TestCommandFromNative()114        {115            var fs = File.Create(Path.GetTempFileName());116            fs.Close();117 118            string target = fs.Name;119            string path = Path.GetTempFileName();120 121            using (System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create())122            {123                // New-Item -ItemType SymbolicLink uses libpsl-native, hence using it for validating native dependencies.124                string command = $"New-Item -ItemType SymbolicLink -Path {path} -Target {target}";125                var results = ps.AddScript(command).Invoke<FileInfo>();126 127                foreach (var item in results)128                {129                    Assert.Equal(path, item.FullName);130                }131            }132 133            if (File.Exists(path))134            {135                File.Delete(path);136            }137 138            if (File.Exists(target))139            {140                File.Delete(target);141            }142        }143 144        /// <summary>145        /// Reference assemblies should be handled correctly so that Add-Type works in the hosting scenario.146        /// </summary>147        [Fact]148        public static void TestAddTypeCmdletInHostScenario()149        {150            string code = @"151                using System;152                public class Foo153                {154                    public Foo(string name, string path)155                    {156                        this.Name = name;157                        this.Path = path;158                    }159 160                    public string Name;161                    public string Path;162                }163            ";164 165            using (System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create())166            {167                ps.AddCommand("Add-Type").AddParameter("TypeDefinition", code).Invoke();168                ps.Commands.Clear();169 170                var results = ps.AddScript("[Foo]::new('Joe', 'Unknown')").Invoke();171                Assert.Single(results);172 173                dynamic foo = results[0];174                Assert.Equal("Joe", foo.Name);175                Assert.Equal("Unknown", foo.Path);176            }177        }178 179        [Fact]180        public static void TestConsoleShellScenario()181        {182            int ret = ConsoleShell.Start("Hello", string.Empty, new string[] { "-noprofile", "-c", "exit 42" });183            Assert.Equal(42, ret);184        }185 186        /* Test disabled because CommandLineParser is static and can only be initialized once (above in TestConsoleShellScenario)187        /// <summary>188        /// ConsoleShell cannot start with both InitialSessionState and -ConfigurationFile argument configurations specified.189        /// </summary>190        [Fact]191        public static void TestConsoleShellConfigConflictError()192        {193            var iss = System.Management.Automation.Runspaces.InitialSessionState.CreateDefault2();194            int ret = ConsoleShell.Start(iss, "BannerText", string.Empty, new string[] { @"-ConfigurationFile ""noneSuch""" });195            Assert.Equal(70, ret);  // ExitCodeInitFailure.196        }197        */198 199        [Fact]200        public static void TestBuiltInModules()201        {202            var iss = System.Management.Automation.Runspaces.InitialSessionState.CreateDefault2();203            if (System.Management.Automation.Platform.IsWindows)204            {205                iss.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.RemoteSigned;206            }207 208            using var runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(iss);209            runspace.Open();210 211            using var ps = System.Management.Automation.PowerShell.Create(runspace);212            var results_1 = ps.AddScript("Write-Output Hello > $null; Get-Module").Invoke<System.Management.Automation.PSModuleInfo>();213            Assert.Single(results_1);214 215            var module = results_1[0];216            Assert.Equal("Microsoft.PowerShell.Utility", module.Name);217 218            ps.Commands.Clear();219            var results_2 = ps.AddScript("Join-Path $PSHOME 'Modules' 'Microsoft.PowerShell.Utility' 'Microsoft.PowerShell.Utility.psd1'").Invoke<string>();220            var moduleManifestPath = results_2[0];221            Assert.Equal(moduleManifestPath, module.Path, ignoreCase: true);222        }223    }224}225