/// <summary> /// A callback that processes the results of the asynchronous PowerShell script. /// </summary> /// <param name="results">The results of the script run.</param> /// <param name="stateValues">State values that are passed through the script invocation.</param> protected static void ProcessResults(System.Management.Automation.PSDataCollection <System.Management.Automation.PSObject> results, Dictionary <string, Object> stateValues) { // Check that the state values made it through the script run. if (stateValues != null && stateValues.Count > 0 && stateValues.ContainsKey(STATE_VALUE_NAME_INVOCATION_TIME)) { // Check that results were retuned by the script. if (results != null) { // For our purposes this will only return a single result... but you should handle all possible results when used in your code. foreach (System.Management.Automation.PSObject result in results) { // Get the time of invocation from the state value. DateTime invocationTime = (DateTime)stateValues[STATE_VALUE_NAME_INVOCATION_TIME]; // We'll write the result of the script run, as well as the time that the script was invoked via the state values passed through to this callback. Console.WriteLine("Script Completed: " + result + " | Invocation Time: " + invocationTime.ToLongTimeString() + " " + invocationTime.Millisecond + "ms"); } } } else { // Indicate that the state values didn't make it through the script run. Console.WriteLine("Error: No state values were passed through the script."); } }
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); }