chris combe

chris combe

  • 1.7k
  • 22
  • 2.4k

System.Management.Automation : Not Found : 'Enable-NetFirewallRule'

Mar 7 2024 12:34 PM
using System.Management.Automation;

PowerShell ps = PowerShell.Create();
//ps.AddCommand("Get-Process");  // if uncommented, works.
ps.AddCommand("Enable-NetFirewallRule").AddParameter("DisplayName","firewall_rule_1");
ps.Invoke();

My configuration :

OS: Windows 11 Pro x64 build 22621.3007 (22H2)
.NET SDK x64 8.1.224.6930
Visual Studio Code 1.87.0
C# 2.22.2
.NET Install Tool 2.0.2

> dotnet add package Microsoft.PowerShell.SDK --version 7.4.1
==> package has been added, no problem.

> dotnet run

Unhandled exception. System.Management.Automation.CommandNotFoundException:
The term 'Enable-NetFirewallRule' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
   at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
   at System.Management.Automation.Runspaces.Pipeline.Invoke()
   at System.Management.Automation.PowerShell.Worker.ConstructPipelineAndDoWork(Runspace rs, Boolean performSyncInvoke)
   at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
   at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TOutput](IEnumerable input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke()
   at Program.<Main>$(String[] args) in C:\c#\fw\Program.cs:line 6

I have unsucessfully tried "dotnet add package System.Management.Automation --version 7.4.1", same result.

Environment variable :
PSModulePath=C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules;

Thanks!


Answers (2)