Run PowerShell script from C#

Viewed 642

I have a requirement to run a PS file from C#. PS1 file content is $ADStatus=Test-ComputerSecureChannel -Server "xyz.com";.

When I run this PS file from C# I get the following error, however when running the same PS file from a PowerShell window it runs fine. What should I do?

System.Management.Automation.CommandNotFoundException , System.Management.Automation.CommandNotFoundException: The term 'Test-ComputerSecureChannel' is not recognized as a name of a cmdlet, function, script file, or executable program.

I am using package Microsoft.Powershell.SDK v7.1.5.
C# code:

    using System.Management.Automation;
    using (var ps = PowerShell.Create(string scriptFilePath))
    {
        var result = ps.Invoke();
    
        var results = ps.AddScript(File.ReadAllText(scriptFilePath)).Invoke();
    }
0 Answers
Related