Exemplo n.º 1
0
        public void Value_StringLoad()
        {
            var script = @" $s1 = 'ABC' ";
            var dpsc   = DynamicPowerShellContext.Create().Run(script);

            Assert.AreEqual("ABC", dpsc.s1);
        }
        private Dictionary <string, object> LoadPowerShellFiles()
        {
            var     PsPath                 = System.Web.HttpContext.Current.Server.MapPath("~/Dictionary");
            var     psFiles                = System.IO.Directory.GetFiles(PsPath, "*.ps1");
            dynamic powerShellContext      = DynamicPowerShellContext.Create();
            var     psDictionaryVarName    = @"$Dic = @{";
            var     psDictionaryNewVarName = @"$Dic{0} = @{{";
            var     psDictionaryCounter    = 0;

            foreach (var psFile in psFiles)
            {
                var psText = System.IO.File.ReadAllText(psFile);
                psText = psText.Replace(psDictionaryVarName, psDictionaryNewVarName.format(psDictionaryCounter));
                psDictionaryCounter++;
                powerShellContext.Load(psText);
            }
            powerShellContext.Run();

            var dic = new Dictionary <string, object>();

            for (var i = 0; i < psDictionaryCounter; i++)
            {
                var psDic = powerShellContext["Dic{0}".format(i)];
                var keys  = psDic.Keys;
                foreach (var key in keys)
                {
                    if (dic.ContainsKey(key))
                    {
                        dic.Remove(key);
                    }
                    dic.Add(key, psDic [key]);
                }
            }
            return(dic);
        }
        static void Main(string[] args)
        {
            var script = @"
                $IntArray = 1,2,3,4,5,6
                $dic = @{
                    a   = 1  
                    b   = 2 
                    c   = 'aa','bb','cc'
                }
";
            var dpsc   = DynamicPowerShellContext.Create().Run(script);

            Console.WriteLine("Array:");
            Console.WriteLine(dpsc.IntArray[0]);
            Console.WriteLine(dpsc.IntArray.Length);

            foreach (var o in dpsc.IntArray)
            {
                Console.WriteLine(o);
            }

            Console.WriteLine("Dictionary:");
            Console.WriteLine(dpsc.dic.Count);
            Console.WriteLine(dpsc.dic["c"].ToString());
            Console.WriteLine(dpsc.dic.ToString());

            foreach (var k in dpsc.dic)
            {
                Console.WriteLine(String.Format("{0} = {1}", k, dpsc.dic[k]));
            }

            Console.WriteLine("Hit enter");
            Console.ReadLine();
        }
Exemplo n.º 4
0
        public void Value_DoubleLoad()
        {
            var script = @" $d1 = 123.456 ";
            var dpsc   = DynamicPowerShellContext.Create().Run(script);

            Assert.AreEqual(123.456, dpsc.d1);
        }
Exemplo n.º 5
0
        public void Value_IntLoad()
        {
            var script = @" $i1 = 123 ";
            var dpsc   = DynamicPowerShellContext.Create().Run(script);

            Assert.AreEqual(123, dpsc.i1);
        }
Exemplo n.º 6
0
        public void DynamicPowerShellContextWay()
        {
            var script = @" $i1 = 123 ";
            var dpsc   = DynamicPowerShellContext.Create().Run(script);

            int i1 = dpsc.i1;

            Assert.AreEqual(123, i1);
        }
        public void Dictionary_IntLoadFromRessource()
        {
            var script = DS.Resources.GetTextResource(@"PowerShell.IntDictionary.ps1", Assembly.GetExecutingAssembly());
            var dpsc   = DynamicPowerShellContext.Create().Load(script).Run();

            Assert.AreEqual(3, dpsc.Dic.Count);
            Assert.AreEqual(1, dpsc.Dic["a"]);
            Assert.AreEqual(2, dpsc.Dic["b"]);
            Assert.AreEqual(3, dpsc.Dic["c"]);
            Assert.AreEqual(6, dpsc.Dic["a"] + dpsc.Dic["b"] + dpsc.Dic["c"]);
        }
        public void Array_IntLoad()
        {
            var script = @" $IntArray = 0,1,2,3,4,5 ";
            var dpsc   = DynamicPowerShellContext.Create().Run(script);

            Assert.AreEqual(6, dpsc.IntArray.Length);

            foreach (var i in DS.Range(5))
            {
                Assert.AreEqual(i, dpsc.IntArray[i]);
            }
        }
Exemplo n.º 9
0
        public void SetAndResetInt()
        {
            var script = @" $i1 = 123 ";
            var dpsc   = DynamicPowerShellContext.Create().Run(script);

            Assert.AreEqual(123, dpsc.i1);

            script = @" $i1 = 124 ";
            dpsc.Run(script);

            Assert.AreEqual(124, dpsc.i1);
        }
Exemplo n.º 10
0
        public void Dictionary_IntLoadFromFile()
        {
            var ps1ConfigFile = @"{0}\IntDictionary.ps1".format(powerShellScriptFolder);
            var dpsc          = DynamicPowerShellContext.Create();

            dpsc.LoadFile(ps1ConfigFile).Run();

            Assert.AreEqual(3, dpsc.Dic.Count);
            Assert.AreEqual(1, dpsc.Dic["a"]);
            Assert.AreEqual(2, dpsc.Dic["b"]);
            Assert.AreEqual(3, dpsc.Dic["c"]);
            Assert.AreEqual(6, dpsc.Dic["a"] + dpsc.Dic["b"] + dpsc.Dic["c"]);
        }
Exemplo n.º 11
0
        public void Value_BooleanLoad()
        {
            var script = @"
                $bTrue  = $true
                $bFalse = $false
             ";
            var dpsc   = DynamicPowerShellContext.Create().Run(script);

            Assert.AreEqual(true, dpsc.bTrue);
            Assert.AreEqual(false, dpsc.bFalse);
            Assert.AreEqual(!true, !dpsc.bTrue);
            Assert.AreEqual(!false, !dpsc.bFalse);
        }
        public void Dictionary_Keys()
        {
            var script = @"
                $Dic = @{
                    a = 1  
                    b = 2 
                    c = 3
                }
            ";
            var dpsc   = DynamicPowerShellContext.Create().Run(script);

            Assert.AreEqual(3, dpsc.Dic.Keys.Count);

            DS.ListHelper.AssertListEqual(
                DynamicPowerShellContext.MakeList <string>(dpsc.Dic.Keys),
                DS.List("a", "b", "c")
                );
        }
Exemplo n.º 13
0
        public void Dictionary_IntLoad2()
        {
            var script = @"
                function F1([int] $i1, [int] $i2) {	            
	                return $i1 + $i2;
                }
                $Dic = @{
                    a = 1  
                    b = 2 
                    c = (F1 2 1)
                }
            ";
            var dpsc   = DynamicPowerShellContext.Create().Run(script);

            Assert.AreEqual(3, dpsc.Dic.Count);
            Assert.AreEqual(1, dpsc.Dic["a"]);
            Assert.AreEqual(2, dpsc.Dic["b"]);
            Assert.AreEqual(3, dpsc.Dic["c"]);
            Assert.AreEqual(6, dpsc.Dic["a"] + dpsc.Dic["b"] + dpsc.Dic["c"]);
        }
        public void Dictionary_StringLoad()
        {
            var script = @"
                function F1($i1) {	            
	                return $i1;
                }
                $Dic = @{
                    a = 'A'
                    b = 'B' 
                    c = (F1 'C')
                }
            ";
            var dpsc   = DynamicPowerShellContext.Create().Run(script);

            Assert.AreEqual(3, dpsc.Dic.Count);
            Assert.AreEqual("A", dpsc.Dic["a"]);
            Assert.AreEqual("B", dpsc.Dic["b"]);
            Assert.AreEqual("C", dpsc.Dic["c"]);
            Assert.AreEqual("ABC", dpsc.Dic["a"] + dpsc.Dic["b"] + dpsc.Dic["c"]);
        }