I have what I believe to be a unique situation. I have about 15 PowerShell scripts that I want to run against a list of computers and have the scripts return back the output from each script on each host.
What I have works, however it does not appear run the scripts on each host simultaneously and is quite slow. Any help is appreciated.
for (int i = 0; i < hosts.Length; i++) { var remoteComputer = new Uri(String.Format("{0}://{1}:5985/wsman", "http", hosts[i])); var connection = new WSManConnectionInfo(remoteComputer); var runspace = RunspaceFactory.CreateRunspace(connection); runspace.Open(); for (int ii = 0; ii < powerShellfiles.ToArray().Length; ii++) { using (PowerShell ps = PowerShell.Create()) { ps.Runspace = runspace; //ps.AddScript(powerShellfiles[ii]); ps.AddScript(powerShellfiles[ii]); IAsyncResult async = ps.BeginInvoke(); List<string> aa = ps.EndInvoke(async).SelectMany(x => x.Properties.Where(y => y.Name == "rec_num").Select(z => z.Value.ToString())).ToList(); keysFromhost.AddRange(aa); } }; }; Each item within powerShellfiles is the text from the .ps1 file itself.