private void button1_Click(object sender, EventArgs e) { RunScript(LoadScript(@"d:\e\ps.ps1")); } private string RunScript(string scriptText) { PSConsoleLoadException x = null; ; RunspaceConfiguration rsconfig = RunspaceConfiguration.Create(@"d:\e\ConsoleSettings.psc1", out x); Runspace runspace = RunspaceFactory.CreateRunspace(rsconfig); runspace.Open(); RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runspace); runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted"); Pipeline pipeline = runspace.CreatePipeline(); pipeline.Commands.AddScript(scriptText); pipeline.Commands.Add("Get-RST -SearchRoot 'erd/user' -PasswordNeverExpires:$false -PasswordNotChangedFor 60 -enabled"); Collection<PSObject> results = pipeline.Invoke(); runspace.Close(); StringBuilder stringBuilder = new StringBuilder(); foreach (PSObject obj in results) { stringBuilder.AppendLine(obj.ToString()); } return stringBuilder.ToString(); } private string LoadScript(string filename) { try { using (StreamReader sr = new StreamReader(filename)) { StringBuilder fileContents = new StringBuilder(); string curLine; while ((curLine = sr.ReadLine()) != null) { fileContents.Append(curLine + "\n"); } return fileContents.ToString(); } } catch (Exception e) { string errorText = "The file could not be read:"; errorText += e.Message + "\n"; return errorText; } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)