简体   繁体   English

使用 Powershell 在 VS Code 中调试 Windows 防火墙规则

[英]Debuging Windows firewall rules in VS Code with Powershell

I'm working on powershell scripts whose purpose is to add rules to Widnows firewall.我正在研究 powershell 脚本,其目的是向 Widnows 防火墙添加规则。 Using VSCode, powershell 5.1 and powershell extension for VSCode.使用 VSCode、powershell 5.1 和 VSCode 的 powershell 扩展。

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.其次,如果 VSCode 不是以管理员身份运行,我将无法调试,显然是因为我正在修改防火墙。

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?现在,如果无法在非提升模式下“试运行”脚本,那么如何在不以管理员身份运行 VSCode 的情况下调试这些脚本?

because otherwise I got "Permission denied" error.因为否则我会收到“权限被拒绝”错误。

Here is my launch.json :这是我的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 :这是示例脚本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.您正在寻找的是-WhatIf开关。 The WhatIf switch will show you what would happen if you ran the command, but it does not run it. WhatIf开关将显示如果您运行该命令会发生什么,但它不会运行它。

Microsoft has the New-NetFirewallRule information online which also shows the -WhatIf switch details. Microsoft 在线提供了New-NetFirewallRule信息,其中还显示了-WhatIf开关的详细信息。

So try out the below.所以试试下面的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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