Powershell New-NetFirewallRule 和 -LocalUser 示例

Lem*_*nek 1 windows powershell firewall rule

如何创建仅影响一个本地帐户的防火墙规则

理论上,下面的示例就足够了,但是我缺少参数“-LocalUser”的值

下面是 PowerShell 命令

New-NetFirewallRule -DisplayName "BLOCKWWW" -Direction Outbound -LocalPort 80,443 -Protocol TCP -Action Block -LocalUser **WHATGOESHERE**
Run Code Online (Sandbox Code Playgroud)

Mat*_*sen 6

从显示如何使用具有类似描述的其他参数(如)的示例RemoteUser来看,它将在 SDDL 中采用任意 ACL,每个用户只有一个条目。

您可以编写一个小辅助函数来根据用户名生成这些:

function Get-FirewallLocalUserSddl {
  param(
    [string[]]$UserName
  )

  $SDDL = 'D:{0}'

  $ACEs = foreach($Name in $UserName){
    try{
      $LocalUser = Get-LocalUser -Name $UserName -ErrorAction Stop
      '(A;;CC;;;{0})' -f $LocalUser.Sid.Value
    }
    catch{
      Write-Warning "Local user '$Username' not found"
      continue
    }
  }
  return $SDDL -f ($ACEs -join '')
}
Run Code Online (Sandbox Code Playgroud)

然后像这样使用它:

New-NetFirewallRule -DisplayName "BLOCKWWW" -LocalUser (Get-FirewallLocalUserSddl user1,user2) -Direction Outbound -LocalPort 80,443 -Protocol TCP -Action Block
Run Code Online (Sandbox Code Playgroud)