invoke a powershell script with parameters from c# in javascript

To invoke a PowerShell script with parameters from C# in JavaScript, you can use the PowerShell class in C# and the System.Management.Automation namespace. Here's an example code snippet:

main.cs
// Create a new PowerShell instance
using (PowerShell ps = PowerShell.Create())
{
    // Add a script to the PowerShell instance
    ps.AddScript(@"C:\Scripts\MyScript.ps1");

    // Add parameters to the script
    ps.AddParameter("Param1", "Value1");
    ps.AddParameter("Param2", "Value2");

    // Execute the script and wait for it to finish
    Collection<PSObject> results = ps.Invoke();

    // Display the results of the script
    foreach (PSObject result in results)
    {
        Console.WriteLine(result.ToString());
    }
}
538 chars
20 lines

This code will invoke the PowerShell script located at C:\Scripts\MyScript.ps1 with two parameters (Param1 and Param2) and display the results of the script in the console window.

Make sure to add the System.Management.Automation assembly as a reference to your project in Visual Studio before using the PowerShell class.

gistlibby LogSnag