Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I validate PowerShell script syntax in C#?

I have a C# method that generates powershell scripts.

Is there a way to automatically validate the syntax of the generated scripts directly in C# before running them on the end client?

like image 676
JohnB Avatar asked Sep 01 '26 10:09

JohnB


1 Answers

You could possibly use the Parser.ParseFile method from the PowerShell SDK, which might be found in the Microsoft.PowerShell.SDK nuget

Note : I have never used this and is totally untested, and truthfully i have no idea whether this will work for you.

like image 117
TheGeneral Avatar answered Sep 03 '26 02:09

TheGeneral



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!