简体   繁体   English

Powershell 值为“False”的 WebAdministration IF 语句未按预期工作

[英]Powershell WebAdministration IF Statement with value "False" not working as expected

I'm not sure what I'm missing.我不确定我错过了什么。 This powershell seems to be working the opposite of what I expect.这个 powershell 似乎与我的预期相反。 Anyone know why?有谁知道为什么?

$loadUserProfileValue = Get-ItemProperty "IIS:\AppPools\.net v4.5" -Name processModel.loadUserProfile.Value
    Write-Host "Value: $loadUserProfileValue"
    IF ($loadUserProfileValue -eq "False") {
            Write-Host "Since Load User Profile is False, we will now set it to True"}

Here is my Output when Load User Profile is True当加载用户配置文件为真时,这是我的 Output

Value: True
Since Load User Profile is False, we will now set it to True

Here is my output when Load User Profile is False当加载用户配置文件为假时,这是我的 output

Value: False

The value is being picked up correctly.该值被正确提取。 The variable $loadUserProfileValue is correct.变量 $loadUserProfileValue 是正确的。 The IF Statement is working opposite of what I'm expecting. IF 声明与我的预期相反。

I can swap it to be -ne "True" and it seems to work... but why does -eq "False" NOT work?我可以将它交换为 -ne "True" 并且它似乎可以工作......但是为什么 -eq "False" 不起作用?

You're testing for a string value of false when the property is likely returning a boolean value.当属性可能返回 boolean 值时,您正在测试字符串值 false。 PowerShell's type-converter is likely what's responsible for this not throwing an error. PowerShell 的类型转换器可能是导致此错误的原因。

Change your test to use $false instead of 'false' and see if that resolves it.更改您的测试以使用 $false 而不是 'false' 并查看是否可以解决它。 Here is a great article on this:这是一篇很棒的文章:

https://devblogs.microsoft.com/powershell/boolean-values-and-operators/ https://devblogs.microsoft.com/powershell/boolean-values-and-operators/

In PowerShell you use the Boolean data type like this: True = $true and False = $false .在 PowerShell 中,您使用 Boolean 数据类型,如下所示: True = $trueFalse = $false

In your case you have to change False to $false在您的情况下,您必须将 False 更改为$false

$loadUserProfileValue = Get-ItemProperty "IIS:\AppPools\.net v4.5" -Name processModel.loadUserProfile.Value
Write-Host "Value: $loadUserProfileValue"
IF ($loadUserProfileValue -eq $false) {
        Write-Host "Since Load User Profile is False, we will now set it to True"}

There is already a question on that topic on Stack Overflow: Question Stack Overflow 上已经有关于该主题的问题: 问题

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

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