简体   繁体   English

如何使用Powershell使用if语句处理某些功能?

[英]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评估。
  • I moved the functions to the top of the script because PowerShell executes the code starting from top to bottom. 我将函数移到脚本的顶部,因为PowerShell从上到下执行代码。 It is not compiled first. 它不是首先编译的。 So you can't make a function call BEFORE the function has been read into memory and defined. 因此, 将函数读入内存并进行定义之前 ,不能进行函数调用。
  • 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. 这会将值转换为布尔值,然后输出相反的布尔响应。 For example, !"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.

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