Cheries
Cheries

Reputation: 892

How to handle some function with if statement using Powershell?

I want to handle my function with if statement. I tried this code, but it always return me the value of $End_F which is "BB" even my file contains of "@AB@CD" . Anyone can help, please.

The file that I look for "@AB@CD" is like this.

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
}

Upvotes: 0

Views: 45

Answers (1)

AdminOfThings
AdminOfThings

Reputation: 25001

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. As a result, I swapped out if ($Get_SKU -eq $true) in favor of if ($Get_SKU). This change will result in a $true evaluation if $Get_SKU is not $null.
  • I moved the functions to the top of the script because PowerShell executes the code starting from top to bottom. 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 you are comparing values, the proper operator here is -eq. Regarding $FE_UB, the same explanation applies as in the $Get_SKU changes.
  • $FE_UB -eq $false was changed to !$FE_UB. The removal of the -eq $false operator is based on the explanation given for $Get_SKU. The ! character is used to effectively -not the result. This will turn the value into a boolean value and then output the opposite boolean response. For example, !"string data" will output $False. !$null will output $True. I hope this part is clear.

Further Insight:

  • $True and $False evaluations

    • You can make just about anything return a boolean value. Three such ways include using casting, the -as operator, and !. There are many other ways and hacks to do this.

      • Casting:

        $get_sku = "data"
        [boolean]$get_sku
        True
        
      • -as Operator:

        $get_sku = $null
        $get_sku -as [boolean]
        False
        
      • Fun with !:

        $get_sku = 4
        !$get_sku
        False
        !!$get_sku
        True
        

Upvotes: 2

Related Questions