Welcome › Forums › General PowerShell Q&A › If Statement
This topic contains 7 replies, has 7 voices, and was last updated by
-
AuthorPosts
-
August 8, 2018 at 10:38 am #107854
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 #107857
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 #107858
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 #107864
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:32 pm #107869
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:26 pm #107867
Hugely Appreciated guys
-
August 8, 2018 at 12:46 pm #107876
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' }
-
August 30, 2018 at 9:46 pm #110569
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
-
AuthorPosts
The topic ‘If Statement’ is closed to new replies.