[英]Error in IF statement powershell
I think there's a problem with this if
statement. 我认为
if
语句存在问题。
Function Get-IsFirewallProfileEnabled([string]$profile)
{
Return [bool](Get-NetFirewallProfile -name "Domain" | Where Enabled -eq "False")
}
# If the firewall for some of the 3 profiles is not enabled
if ( ((Get-IsFirewallProfileEnabled("Domain")) -or (Get-IsFirewallProfileEnabled("Private")) -or (Get-IsFirewallProfileEnabled("Public"))) -eq "False")
{
Write-Output "Enabling..."
# Enable Windows firewall for for all (the 3 profiles)
Set-NetFirewallProfile -Profile Domain,Private,Public -Enabled True
}
Whether I have activated a firewall or not, this script always does nothing. 无论我是否激活了防火墙,此脚本始终不执行任何操作。 What's happening?
发生了什么?
SOLUTION 解
# Now this function is dynamic
Function Get-IsFirewallProfileEnabled([string]$profile)
{
Return [bool](Get-NetFirewallProfile -name $profile | Where Enabled -eq "True")
}
# If the firewall for some of the 3 profiles is not enabled
if ( -not(Get-IsFirewallProfileEnabled("Domain")) -or -not(Get-IsFirewallProfileEnabled("Private")) -or -not(Get-IsFirewallProfileEnabled("Public")) )
{
Write-Output "Enabling..."
# Enable Windows firewall for for all (the 3 profiles)
Set-NetFirewallProfile -Profile Domain,Private,Public -Enabled True
}
As Robert mentioned, you are using the string False
, not the Boolean $false
in your if
statement. 如Robert所述,您在
if
语句中使用字符串False
,而不是布尔值$false
。
But the logic in your if
statement isn't working as expected: 但是
if
语句中的逻辑无法按预期工作:
if(((Boolean) -or (Boolean) -or (Boolean)) -eq $false)
will not produce the desired result in Powershell (it will only execute if all of the values are $false
). 将不会在Powershell中产生所需的结果(仅在所有值均为
$false
时才执行)。 Since if
always executes on $true
, you can achieve desired results by simply -not
-ing the values from Get-IsFirewallProfileEnabled
like so: 因为
if
总是在执行$true
,你可以通过简单地达到预期的效果-not
-ing从值Get-IsFirewallProfileEnabled
像这样:
if((-not(Get-IsFirewallProfileEnabled("Domain")) -or (-not(Get-IsFirewallProfileEnabled("Private")) -or (-not(Get-IsFirewallProfileEnabled("Public")))
This will execute the if
block when any of the values are $false
. 当任何一个值为
$false
时,它将执行if
块。
You want to use $false
not "False"
. 您要使用
$false
而不是"False"
。
"False" is a string literal and will never match if the boolean value returned by the condition ((Get-IsFirewallProfileEnabled("Domain")) -or (Get-IsFirewallProfileEnabled("Private")) -or (Get-IsFirewallProfileEnabled("Public")))
is false. “ False”是字符串文字,如果条件
((Get-IsFirewallProfileEnabled("Domain")) -or (Get-IsFirewallProfileEnabled("Private")) -or (Get-IsFirewallProfileEnabled("Public")))
为假。
For example: 例如:
if ("False") { write-host "true" }
The line above will always write "true" to the host. 上面的行将始终向主机写入“ true”。 This is because a non-empty string literal equates to $true.
这是因为非空字符串文字等于$ true。
The line below will never write "true" to the host. 下面的行永远不会向主机写入“ true”。
if ($false) { write-host "yes" }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.