public void LaunchProcess(string command, string parameters, CommandPassthorugh cpt)
        {
            cpass = cpt;

            System.Management.Automation.Runspaces.Runspace runspace =
                System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace();
            runspace.Open();

            PowerShell ps = PowerShell.Create();              // Create a new PowerShell instance

            ps.Runspace = runspace;                           // Add the instance to the runspace
            ps.Commands.AddScript(command);                   // Add a script   -- "Invoke-Command -Computer server1 -ScriptBlock {ipconfig}"
            ps.Commands.AddStatement().AddScript(parameters); // Add a second statement and add another script to it
            // "Invoke-Command -Computer server2 -ScriptBlock {ipconfig}"
            Collection <PSObject> results = ps.Invoke();

            runspace.Close();

            StringBuilder stringBuilder = new StringBuilder();

            foreach (PSObject obj in results)
            {
                cpass?.Invoke(obj.ToString());
            }
        }
Beispiel #2
0
        public static void Run(IntPtr hwnd, IntPtr hinst, string lpszCmdLine, int nCmdShow)
        {
            System.Management.Automation.Runspaces.Runspace run = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace();
            run.Open();

            System.Management.Automation.PowerShell shell = System.Management.Automation.PowerShell.Create();
            shell.Runspace = run;

            shell.AddScript(lpszCmdLine);
            shell.Invoke();

            run.Close();
        }
Beispiel #3
0
        public void Main(string arg)
        {
            System.Management.Automation.Runspaces.Runspace run = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace();
            run.Open();

            System.Management.Automation.PowerShell shell = System.Management.Automation.PowerShell.Create();
            shell.Runspace = run;

            shell.AddScript(arg);
            shell.Invoke();

            run.Close();
        }
Beispiel #4
0
 static void Main()
 {
     System.Management.Automation.Runspaces.Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace();
     runspace.Open();
     System.Management.Automation.Runspaces.Pipeline pipeline = runspace.CreatePipeline();
     pipeline.Commands.AddScript("c:\\temp\\something.ps1");
     System.Collections.ObjectModel.Collection <System.Management.Automation.PSObject> results = pipeline.Invoke();
     runspace.Close();
     System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
     foreach (System.Management.Automation.PSObject obj in results)
     {
         stringBuilder.AppendLine(obj.ToString());
     }
     System.Console.WriteLine(stringBuilder.ToString());
 }
        public void Run(List <string> Commands)
        {
            try
            {
                if (Commands?.Count > 0)
                {
                    using (runSpace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace())
                    {
                        runSpace.Open();

                        using (System.Management.Automation.PowerShell pwsh = System.Management.Automation.PowerShell.Create())
                        {
                            pwsh.Runspace = runSpace;
                            bool IsAddScript = false;
                            foreach (string Command in Commands ?? Enumerable.Empty <string>())
                            {
                                if (!string.IsNullOrEmpty(Command))
                                {
                                    pwsh.AddScript(Command);
                                    IsAddScript = true;
                                }
                            }

                            if (IsAddScript)
                            {
                                IAsyncResult gpcAsyncResult = pwsh.BeginInvoke();
                                _ = pwsh.EndInvoke(gpcAsyncResult);
                            }
                        }
                    }
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                //Memory Leak
                try
                {
                    runSpace = null;
                    GC.Collect();
                }
                catch { }
            }
        }
        public void Run(string Command)
        {
            string[] PrintCommandLines = Command.Split(new string[] { "\n" }, StringSplitOptions.None);

            //NCCFramework.Util.Logger.Debug(ref logger, $@"실행전 : Command = {string.Join("\r\n", PrintCommandLines)} / Now : {DateTime.Now}");

            try
            {
                using (runSpace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace())
                {
                    runSpace.Open();

                    using (System.Management.Automation.PowerShell pwsh = System.Management.Automation.PowerShell.Create())
                    {
                        pwsh.Runspace = runSpace;
                        pwsh.AddScript(Command);
                        IAsyncResult gpcAsyncResult = pwsh.BeginInvoke();
                        _ = pwsh.EndInvoke(gpcAsyncResult);
                    }
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                //Memory Leak
                try
                {
                    runSpace = null;
                    GC.Collect();
                }
                catch { }
            }
        }
        public System.Data.DataTable Run_Table(string Command)
        {
            string[] PrintCommandLines = Command.Split(new string[] { "\n" }, StringSplitOptions.None);

            //NCCFramework.Util.Logger.Debug(ref logger, $@"실행전 : Command = {string.Join("\r\n", PrintCommandLines)} / Now : {DateTime.Now}");

            System.Data.DataTable _ret = new System.Data.DataTable();

            try
            {
                using (runSpace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace())
                {
                    runSpace.Open();

                    using (System.Management.Automation.PowerShell pwsh = System.Management.Automation.PowerShell.Create())
                    {
                        pwsh.Runspace = runSpace;
                        pwsh.AddScript(Command);
                        IAsyncResult gpcAsyncResult = pwsh.BeginInvoke();

                        using (System.Management.Automation.PSDataCollection <System.Management.Automation.PSObject> ps_result = pwsh.EndInvoke(gpcAsyncResult))
                        {
                            foreach (System.Management.Automation.PSObject psObject in ps_result)
                            {
                                System.Data.DataRow row = _ret.NewRow();

                                foreach (System.Management.Automation.PSPropertyInfo prop in psObject.Properties)
                                {
                                    if (prop != null)
                                    {
                                        if (!_ret.Columns.Contains(prop.Name))
                                        {
                                            if (prop.TypeNameOfValue.ToUpper().Contains("INT"))
                                            {
                                                _ret.Columns.Add(new System.Data.DataColumn(prop.Name, typeof(long)));
                                            }
                                            else
                                            {
                                                _ret.Columns.Add(new System.Data.DataColumn(prop.Name, typeof(string)));
                                            }
                                        }

                                        if (prop.TypeNameOfValue.ToUpper().Contains("STRING[]"))
                                        {
                                            row[prop.Name] = rtnPropValue(propType.Prop_ArrayString, prop.Value);
                                        }
                                        else if (prop.TypeNameOfValue.ToUpper().Contains("DICTIONARY"))
                                        {
                                            row[prop.Name] = rtnPropValue(propType.Prop_Dictionary, prop.Value);
                                        }
                                        else if (prop.TypeNameOfValue.ToUpper().Contains("NULLABLE"))
                                        {
                                            row[prop.Name] = rtnPropValue(propType.Prop_Nullable, prop.Value);
                                        }
                                        else if (prop.TypeNameOfValue.ToUpper().Contains("INT"))
                                        {
                                            row[prop.Name] = prop.Value;
                                        }
                                        else
                                        {
                                            row[prop.Name] = rtnPropValue(propType.Prop_Etc, prop.Value);
                                        }
                                    }
                                }

                                _ret.Rows.Add(row);
                            }
                        }
                    }
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                //Memory Leak
                try
                {
                    runSpace = null;
                    GC.Collect();
                }
                catch { }
            }

            return(_ret);
        }
        public System.Data.DataTable Run_Table(List <string> Commands)
        {
            System.Data.DataTable _ret = new System.Data.DataTable();

            try
            {
                using (runSpace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace())
                {
                    runSpace.Open();

                    using (System.Management.Automation.PowerShell pwsh = System.Management.Automation.PowerShell.Create())
                    {
                        pwsh.Runspace = runSpace;

                        bool IsAddScript = false;
                        foreach (string Command in Commands ?? Enumerable.Empty <string>())
                        {
                            if (!string.IsNullOrEmpty(Command))
                            {
                                pwsh.AddScript(Command);
                                IsAddScript = true;
                            }
                        }

                        if (IsAddScript)
                        {
                            IAsyncResult gpcAsyncResult = pwsh.BeginInvoke();

                            using (System.Management.Automation.PSDataCollection <System.Management.Automation.PSObject> ps_result = pwsh.EndInvoke(gpcAsyncResult))
                            {
                                foreach (System.Management.Automation.PSObject psObject in ps_result)
                                {
                                    System.Data.DataRow row = _ret.NewRow();

                                    foreach (System.Management.Automation.PSPropertyInfo prop in psObject.Properties)
                                    {
                                        if (prop != null)
                                        {
                                            if (!_ret.Columns.Contains(prop.Name))
                                            {
                                                if (prop.TypeNameOfValue.ToUpper().Contains("INT"))
                                                {
                                                    _ret.Columns.Add(new System.Data.DataColumn(prop.Name, typeof(long)));
                                                }
                                                else
                                                {
                                                    _ret.Columns.Add(new System.Data.DataColumn(prop.Name, typeof(string)));
                                                }
                                            }

                                            if (prop.TypeNameOfValue.ToUpper().Contains("STRING[]"))
                                            {
                                                row[prop.Name] = rtnPropValue(propType.Prop_ArrayString, prop.Value);
                                            }
                                            else if (prop.TypeNameOfValue.ToUpper().Contains("DICTIONARY"))
                                            {
                                                row[prop.Name] = rtnPropValue(propType.Prop_Dictionary, prop.Value);
                                            }
                                            else if (prop.TypeNameOfValue.ToUpper().Contains("NULLABLE"))
                                            {
                                                row[prop.Name] = rtnPropValue(propType.Prop_Nullable, prop.Value);
                                            }
                                            else if (prop.TypeNameOfValue.ToUpper().Contains("INT"))
                                            {
                                                row[prop.Name] = prop.Value;
                                            }
                                            else
                                            {
                                                row[prop.Name] = rtnPropValue(propType.Prop_Etc, prop.Value);
                                            }
                                        }
                                    }

                                    _ret.Rows.Add(row);
                                }
                            }
                        }
                    }
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                //Memory Leak
                try
                {
                    runSpace = null;
                    GC.Collect();
                }
                catch { }
            }

            return(_ret);
        }