Running Powershell Command Remotely Using C#
C# has some really awesome nuget package if you want to run the PowerShell script or run the command. I am talking about the nuget package "Microsoft.PowerShell.SDK". Now running the batch file or any command or any executable from the local machine is quite easy and simple. You just need to create the PowerShell object, add the script you want to execute and invoke it. Above code is simple and self explanatory. But lately in my project we had a requirement in which we need to execute the command on the remote machine and fetch the output. It's as good as you are remoting to that machine and executing the command on that machine and fetching the output. Now you would be surprised to know that, you can actually achieve that using the PowerShell SDK and with only few lines of code you can create the magic. PowerShell SDK provides something called as "RunSpace". When you create the RunSpace object you need to provide the machine details and corresponding credential