If Statement

Welcome Forums General PowerShell Q&A If Statement

This topic contains 7 replies, has 7 voices, and was last updated by

js
 
Participant
3 months, 1 week ago.

  • Author
    Posts
  • #107854

    Participant
    Points: 20
    Rank: Member

    After some help, i have a bit of a complicated If Statement

    $fN = $Firstname.text
    
    $ln = $Lastname.Text
    
    $CN = $CompanyName.Text
    
    $Email = $Emailaddress.text

    FN – Cannot be Null or empty

    LN – Cannot be Null or empty

    CN – Cannot be Null or empty

    Email – cannot be null or empty and cannot contain spaces.

    if any or all of those are present then stop, other wise do stuff

     

     

    in all honesty im a bit lost with it 🙁

     

  • #107857

    Participant
    Points: 10
    Rank: Member

    You can use a foreach loop or switch statement. Example is below.

    $fN = 'firstname'
    $ln = 'lastname'
    $CN = 'yourcompanyname'
    $Email = 'youremail'
    $all = $fn,$ln,$cn,$Email
    
    foreach ($a in $all){
    If ($a){"Stop Something"}Else{"Do Something"}
    }
    
  • #107858

    Participant
    Points: 144
    Helping Hand
    Rank: Participant

    Another approach could be this

    IF( -not [string]::IsNullOrEmpty($fN) -and 
        -not [string]::IsNullOrEmpty($ln) -and 
        -not [string]::IsNullOrEmpty($CN) -and 
        -not [string]::IsNullOrEmpty($Email) -and
        $email -notmatch '\s' 
    ){
        'cool'
    }
    Else {
        'uncool'
    }
  • #107864

    Participant
    Points: 165
    Helping Hand
    Rank: Participant

    I'd wrap this in a function that takes these values as input. Then, you can apply parameter validation to stop it running if a value isn't supplied:

    function Do-Thing {
      param(
        [ValidateNotNullOrEmpty()]
        $FirstName,
    
        [ValidateNotNullOrEmpty()]
        $LastName,
    
        [ValidateNotNullOrEmpty()]
        $Company,
    
        [ValidateNotNullOrEmpty()]
        $EmailAddress,
      )
      # Do things here
    }
    $Params = @{
      Firstname    = $Firstname.Text
      LastName     = $Lastname.Text
      Company      = $CompanyName.Text
      EmailAddress = $EmailAddress.Text
    }
    Do-Thing @Params
    • #107869

      Participant
      Points: 573
      Helping Hand
      Rank: Major Contributor

      IMO, Joel's example is a wise approach.

      Same function with check for white spaces as well.

      function Do-Thing {
        param(
          [ValidateNotNullOrEmpty()]
          $FirstName,
      
          [ValidateNotNullOrEmpty()]
          $LastName,
      
          [ValidateNotNullOrEmpty()]
          $Company,
      
          [ValidateNotNullOrEmpty()]
          [Validatepattern('^\S*$')]
          $EmailAddress,
        )
        # Do things here
      }
      
  • #107867

    Participant
    Points: 20
    Rank: Member

    Hugely Appreciated guys

  • #107876
    js

    Participant
    Points: 217
    Helping Hand
    Rank: Participant

    Aww, I thought the css colored text would work (copy and paste from vscode). And these windows are quite narrow (62 characters). Anyway, here's my version, with all my tricks to bear.

    #23456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 
    
    if ($fN -and $ln -and $CN -and $Email -replace ' ') {
      'all good'
    }
  • #110569

    Participant
    Points: 3
    Rank: Member

    I hate to be a stickler, but an email address can have a space in it. What will happen is that the space is read as a delimiter between the DisplayName and the other components of the email address (User, Host, Address)

    My suggestion is to use the function I wrote Test-IsValidEmailAddress taken from inspiration I found on the Internet with a whole lot more logic and comment based help.

    Function Test-IsValidEmailAddress {
    
    
        [CmdletBinding()]
        [Outputtype([bool])]
        Param (
            [parameter(Mandatory=$True,Position=0,ValueFromPipeLine=$True,ValueFromPipeLineByPropertyName=$True)]
            [Alias("Address")]
            [string] $EmailAddress
        )
        Process {
            Write-Verbose -message "You entered email address: [$($EmailAddress)]"
            Try {
                $temp = [System.Net.Mail.MailAddress] $EmailAddress
                write-verbose -message "Address resolved to: [$($temp.Address)]"
                if ($temp.Address -ne $EmailAddress) {
                    write-verbose -message "[$($temp.Address)] does not match [$($EmailAddress)]"
                    write-ouput $false
                } else {
                    Write-Verbose -message "Address valid, no guarantee that address [$($EmailAddress)] exists."
                    Write-Output $True
                }
            } Catch {
                Write-Verbose -message "The address is NOT valid."
                Write-Output -inputobject $False
            }
        }
    } #EndFunction Test-IsValidEmailAddress
    

The topic ‘If Statement’ is closed to new replies.