简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM