If Statement

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 :frowning:

 

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"}
}

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'
}

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

Hugely Appreciated guys

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
}

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'
}

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