metablaster
metablaster

Reputation: 2184

Debuging Windows firewall rules in VS Code with Powershell

I'm working on powershell scripts whose purpose is to add rules to Widnows firewall. Using VSCode, powershell 5.1 and powershell extension for VSCode.

now there are 2 problems: First I just want to run debugger to see if the script is executed with no errors, but what happens is that the rule is added to firewall for real.

Is there a way to avoid adding rule to firewall for real, just test if it works, ie. dry-run?

Secondly, I can't debug if VSCode is not run as Admin, obviously since I'm modifiying the firewall.

Now if there is no way to just "dry-run" the script in non elevated mode then how to debug these scripts without running VSCode as Admin?

because otherwise I got "Permission denied" error.

Here is my launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "PowerShell: Launch Current File",
            "type": "PowerShell",
            "request": "launch",
            "script": "${file}",
            "cwd": "${file}"
        }
    ]
}

and here is sample script test.ps1:

New-NetFirewallRule -DisplayName "Block Outbound Port 80" -Direction Outbound -LocalPort 80 -Protocol TCP -Action Block

Upvotes: 0

Views: 869

Answers (1)

Drew
Drew

Reputation: 4030

What you are looking for is the -WhatIf switch. The WhatIf switch will show you what would happen if you ran the command, but it does not run it.

Microsoft has the New-NetFirewallRule information online which also shows the -WhatIf switch details.

So try out the below.

New-NetFirewallRule -DisplayName "Block Outbound Port 80" -Direction Outbound `
    -LocalPort 80 -Protocol TCP -Action Block -WhatIf

Upvotes: 1

Related Questions