[英]How to handle some function with if statement using Powershell?
I want to handle my function with if statement. 我想用if语句处理我的函数。 I tried this code, but it always return me the value of
$End_F
which is "BB"
even my file contains of "@AB@CD"
. 我尝试了这段代码,但是即使我的文件包含
"@AB@CD"
,它也总是返回$End_F
值为"BB"
"@AB@CD"
。 Anyone can help, please. 任何人都可以帮忙。
The file that I look for "@AB@CD" is like this. 我寻找“ @ AB @ CD”的文件就是这样。
Config
; Date="2019/06/12" Time="10:25:02" UTC="0"
;
Number
123456@AB@CD
$Get_SKU = Get-Content '.\Number.txt' | Where-Object {$_.Contains("@AB@CD")}
$Get_SKU
if($Get_SKU)
{$ML = "1"
AUTO_SELECT
}
else
{
END_Proc
}
Function AUTO_SELECT
{
$AT = "AA"
$AT
}
Function END_Proc
{
$End_F = "BB"
$End_F
}
$FE_UB = "4"
if($ML = "1" -and $FE_UB -eq $true)
{
G_BEGIN
}
if($ML = "1" -and $FE_UB -eq $false)
{
G_END
}
else
{
END_Proc
}
Function G_BEGIN
{
$begin = "Ready"
$begin
}
Function G_END
{
$ending = "Stop"
$ending
}
Some things need to be corrected to make your code work as expected. 为了使您的代码按预期工作,需要纠正一些问题。
Function AUTO_SELECT
{
$AT = "AA"
$AT
}
Function END_Proc
{
$End_F = "BB"
$End_F
}
Function G_BEGIN
{
$begin = "Ready"
$begin
}
Function G_END
{
$ending = "Stop"
$ending
}
$Get_SKU = Get-Content '.\Number.txt' | Where-Object {$_.Contains("@AB@CD")}
$Get_SKU
if($Get_SKU)
{
$ML = "1"
AUTO_SELECT
}
else
{
END_Proc
}
$FE_UB = "4"
if($ML -eq "1" -and $FE_UB)
{
G_BEGIN
}
if($ML -eq "1" -and !$FE_UB)
{
G_END
}
else
{
END_Proc
}
Explanation of Changes: 变更说明:
$Get_SKU
will store either $null
or a string depending on whether the Where-Object
condition finds a match. $Get_SKU
将存储$null
或字符串,具体取决于Where-Object
条件是否找到匹配项。 As a result, I swapped out if ($Get_SKU -eq $true)
in favor of if ($Get_SKU)
. if ($Get_SKU -eq $true)
,而选择了if ($Get_SKU)
。 This change will result in a $true
evaluation if $Get_SKU
is not $null
. $Get_SKU
不是$null
则此更改将导致$true
评估。 if ($ML = "1" -and $FE_UB -eq $true)
has been updated to if ($ML -eq "1" -and $FE_UB)
because variable assignment variable = value
should not happen in an if statement condition. if ($ML = "1" -and $FE_UB -eq $true)
已更新为if ($ML -eq "1" -and $FE_UB)
因为变量分配variable = value
不应在if语句条件下发生。 If you are comparing values, the proper operator here is -eq
. -eq
。 Regarding $FE_UB
, the same explanation applies as in the $Get_SKU
changes. $FE_UB
,与$Get_SKU
更改中的解释相同。 $FE_UB -eq $false
was changed to !$FE_UB
. $FE_UB -eq $false
已更改为!$FE_UB
。 The removal of the -eq $false
operator is based on the explanation given for $Get_SKU
. -eq $false
运算符的删除基于对$Get_SKU
给出的解释。 The !
!
character is used to effectively -not
the result. -not
不是结果。 This will turn the value into a boolean value and then output the opposite boolean response. !"string data"
will output $False
. !"string data"
将输出$False
。 !$null
will output $True
. !$null
将输出$True
。 I hope this part is clear. Further Insight: 进一步的见解:
$True
and $False
evaluations $True
和$False
评估
You can make just about anything return a boolean value. 您几乎可以使任何东西返回布尔值。 Three such ways include using casting, the
-as
operator, and !
三种方法包括使用强制转换,
-as
运算符和!
. 。 There are many other ways and hacks to do this.
还有许多其他方法和技巧可以做到这一点。
Casting: 铸件:
$get_sku = "data" [boolean]$get_sku True
-as
Operator: -as
运算符:
$get_sku = $null $get_sku -as [boolean] False
Fun with !
玩的开心
!
: :
$get_sku = 4 !$get_sku False !!$get_sku True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.