Add the following Powershell library as a reference:
C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0
Import the following libraries in C#:
Create a C# method:
public void ExecuteScript(string fileName)
{
try
{
string script = File.ReadAllText(fileName);
var shell = PowerShell.Create();
shell.Commands.AddScript(script);
shell.Invoke();
}
catch (Exception) {
// Exception Handling Code
}
}
C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0
Import the following libraries in C#:
- using System.Management.Automation;
- using System.Management.Automation.Runspaces;
Create a C# method:
public void ExecuteScript(string fileName)
{
try
{
string script = File.ReadAllText(fileName);
var shell = PowerShell.Create();
shell.Commands.AddScript(script);
shell.Invoke();
}
catch (Exception) {
// Exception Handling Code
}
}
No comments:
Post a Comment