$true or $false in powershell function

Welcome Forums General PowerShell Q&A $true or $false in powershell function

Viewing 4 reply threads
  • Author
    Posts
    • #279420
      Participant
      Topics: 5
      Replies: 6
      Points: 62
      Rank: Member

      I came across one script and I’m puzzled about one simple function in a script. To my understanding, result should be always $false as it has return $false at the bottom of the script or I’m missing something? Can someone explains the logic here. Thank you

      # This function checks to see if the file should be ignored.
      Function Is_Temporary
      {
      $TheFile = "filename.txt"

      # Check that the full path is long enough to be applicable, then ask if the file's last few characters match a temporary extension.
      $Temp_Files | ForEach {If ($Object.length -ge $_.length -AND $Object.substring($Object.length - $_.length, $_.length) -eq $_) {Return $True}}

      If ($TheFile.substring(0,1) -eq "~") {Return $True}
      If ($TheFile -eq "thumbs.db") {Return $True}
      If ($TheFile.length -ge 4 -AND $TheFile.substring(0,4) -eq ".dat") {Return $True}

      Return $False
      }

      • This topic was modified 1 month ago by Adrianj59.
    • #279450
      Participant
      Topics: 5
      Replies: 249
      Points: 985
      Helping Hand
      Rank: Major Contributor

      Is this the entire function?  I don’t see any parameters and the function references variables that are not defined ($Temp_Files).  I reformatted your code so I can reference line numbers in my explanation.

      Line 4 – $TheFile is assigned to the string “filename.txt”  I don’t know why

      Line 7 – $Temp_Files is never assigned so doesn’t exist which means the ForEach loop will not happen.

      Line 10 – False because “filename.txt” does not start with “~”

      Line 11 – False because “filename.txt” is not “thumbs.db”

      Line12 – First part of logic statement is True because “filename.txt”.length is greater than 4, but second part is False because “filename.txt”.substring(0,4) is “file”

      Line 14 will return False

      Anytime a function hits the keyword “Return” it will return that object and stop execution.  That means if any of the conditions on lines 8, 10, 11, or 12 would have evaluated to True then the function would have returned True.

       

    • #279507
      Participant
      Topics: 5
      Replies: 6
      Points: 62
      Rank: Member

      Hi Mike. Thank you for your reply.

      It is just one simple function within the script.

      $thefile actually calls another function but I removed that for simplicity.

      $temp_file is a variable outside of function.

      But your last statement resolved my dilemma. I thought the same that it returns first $true but when I tested with “TheFile” variable (missed object variable”), the result was always $false and I was convinced that it always returns $false  as it is the last return in script.

      Thank you so much again for your explanation 🙂

       

    • #279510
      Participant
      Topics: 5
      Replies: 249
      Points: 985
      Helping Hand
      Rank: Major Contributor

      Hi Mike. Thank you for your reply.

      It is just one simple function within the script.

      $thefile actually calls another function but I removed that for simplicity.

      $temp_file is a variable outside of function.

      But your last statement resolved my dilemma. I thought the same that it returns first $true but when I tested with “TheFile” variable (missed object variable”), the result was always $false and I was convinced that it always returns $false as it is the last return in script.

      Thank you so much again for your explanation 🙂

      You can’t reference a variable defined outside of the function.  It is out of scope so regardless of how it was previously designed, inside the function definition it will be $null.  If you need the value, create a parameter and pass it in as an argument.

    • #279555
      Participant
      Topics: 5
      Replies: 6
      Points: 62
      Rank: Member

      It is a script scope variable so that is fine. Thanks again

Viewing 4 reply threads
  • You must be logged in to reply to this topic.