c# - Opening runspaces multiple times -
i want execute powershell command on remote computer c#. have achieved same using
public collection<psobject> runscript(string command) { collection<psobject> results = null; using (var powershell = powershell.create()) { runspace runspace = runspacefactory.createrunspace(connection); runspace.open(); powershell.runspace = runspace(); powershell.addscript(command); results = powershell.invoke(); runspace.close(); } return results; } }
i able execute first time. when try executr function second time gives me error, runspace closed.actually did close @ last why doesnt reopen when function called.
you closed didn't dispose of it:
using (var powershell = powershell.create()) using (runspace runspace = runspacefactory.createrunspace(connection)) { runspace.open(); powershell.runspace = runspace(); powershell.addscript(command); results = powershell.invoke(); runspace.close(); }
Comments
Post a Comment