Author Posts

August 8, 2018 at 10:38 am

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 🙁

 

August 8, 2018 at 11:24 am

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

August 8, 2018 at 11:38 am

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

August 8, 2018 at 12:13 pm

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

August 8, 2018 at 12:26 pm

Hugely Appreciated guys

August 8, 2018 at 12:32 pm

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
}

August 8, 2018 at 12:46 pm

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