To invoke a PowerShell script with parameters using user credentials in C#, you can use the Runspace class from the System.Management.Automation namespace. Here is an example code block:
main.cs866 chars33 lines
In this example, we create a SecureString object to store the user's password, and then use it to create a PSCredential object. We then set up the command and parameters to pass to the PowerShell script.
Next, we create a Runspace object and open it, and create a pipeline to add our command to. We then add the credentials to the pipeline using the SessionStateProxy property of the pipeline's runspace.
Finally, we invoke the pipeline and store the results, and then close the runspace.
gistlibby LogSnag