Run Only signed powershell scripts from c#

Question:

I have a windows service that downloads a script and then runs it.

I’ve been trying to make my windows service more secure, making it accept only signed power-shell scripts.

I have ran the Set-ExecutionPolicy AllSigned command on the server, and this works in the windows power shell command prompt.

However, my code still runs both signed and unsigned scripts, even if the set-executionpolicy is set to restricted.

I have tried two approaches:

RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();

And another approach:

In both situations the code runs unsigned scripts as well.

Have I missed something?

Answer:

I found the solution. The only way to restrict the code from running unsigned scripts was to check the scripts myself with Get-AuthenticodSignature:

Thanks,

Dan

Source:

Run Only signed powershell scripts from c# by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply