Need help with Evaluating Strings

When the Get-MBX function runs, no matter how I setup the If statement. It only runs the If code - or the Else code, but never either one or the other depending on what's selected. So my question is, what's the best way to evaluate a "String" in a If Loop?

I've tried using the following formats:

($EmailAddress -is "String")

($EmailAddress -eq $True)

($EmailAddress -is "String" -or $EmailAddress -eq $True)

($EmailAddress -is "String" -and $EmailAddress -eq $True)

($EmailAddress -match $EmailAddress)

####################################
# Exchange Online PowerShell Tasks #
#       #
####################################

## Get-Mailbox Function ##

Function Get-MBX {

$EmailAddress = Read-Host "Are you look for a Specific Email Address? If not, just Press Enter. "

if ($EmailAddress -eq $True) {
Start-Transcript -Verbose
Get-Mailbox | Select EmailAddresses
Stop-Transcript
}
Else {
Start-Transcript -Verbose
Get-Mailbox
Stop-Transcript

 }
}


$task_selection = Read-Host "What Task would like to perform? Select the number you would to run.

1: Get Mailbox List
"

switch ($task_selection) {

1 {Get-MBX
break}
Default {"End of Task"}

April 23rd, 2015 6:00pm

So my question is, what's the best way to evaluate a "String" in a If Loop?

Here's two examples:

If/ElseIf/Else:
$in = Read-Host "Enter 'One', 'Two', or 'Three'"

If ($in -eq 'One') {

    Write-Host 'One was entered'

} ElseIf ($in -eq 'Two') {

    Write-Host 'Two was entered'

} ElseIf ($in -eq 'Three') {

    Write-Host 'Three was entered'

} Else {

    Write-Host 'You did not follow the directions.'

}


Switch:
$in = Read-Host "Enter 'One', 'Two', or 'Three'"

switch ($in) {
    One { Write-Host 'One was entered' }
    Two { Write-Host 'Two was entered' }
    Three {Write-Host 'Three was entered' }
    Default { Write-Host 'You did not follow the directions.' }
}

Free Windows Admin Tool Kit Click here and download it now
April 23rd, 2015 6:15pm

Thanks for the reply, we need to evaluate Email Address string or alias. This also under the assumption that we don't know what email address is being provided. The Switch portion is to create a simple menu option to determine which PowerShell Function to execute. I guess what I'm asking is more of a opinion, then help when evaluating Strings in a If-Else Loop.

$task_selection = Read-Host "What Task would like to perform? Select the number you would to run.

1: Get Mailbox List
"

switch ($task_selection) {

1 {Get-MBX
break}
Default {"End of Task"}

April 23rd, 2015 6:21pm

I'm not clear as to what you're asking.

$in = Read-Host "1, 2, or 3"

switch ($in) {
    1 { Write-Host "Do whatever for 1"}
    2 { Write-Host "Do whatever for 2"}
    3 { Write-Host "Do whatever for 3"}
    Default { Write-Host "Do whatever for unexpected input"}
}

Replace the Write-Host lines with whatever.

Free Windows Admin Tool Kit Click here and download it now
April 23rd, 2015 6:37pm

Not worry about the Switch Loop, need help with If-Else within the function. The If statement is not evaluating correctly to the Boolean. :

## Get-Mailbox Function ##

Function Get-MBX ([String]$EmailAddress) {

$EmailAddress = Read-Host "Are you look for a Specific Email Address? If not, just Press Enter. "

##If Statement is True, do this ## if ($EmailAddress -eq $True) {
Start-Transcript -Verbose
Get-Mailbox | Select EmailAddresses
Stop-Transcript
}
##If Statement is False, do this ## Else {
Start-Transcript -Verbose
Get-Mailbox
Stop-Transcript

 }
}

April 23rd, 2015 6:44pm

Ah, okay:

$in = Read-Host 'Enter something for if, nothing for else'

If ($in) {

    'You typed something'

} Else {

    'You typed nothing'

}

Free Windows Admin Tool Kit Click here and download it now
April 23rd, 2015 6:57pm

I wasn't able to believe it was that easy. All this time I was thinking you needed a operator for If-Else / Do-While...etc. Tx
April 23rd, 2015 11:55pm

Cheers, you're welcome. Sorry about my confusion at the outset, glad it worked out.
Free Windows Admin Tool Kit Click here and download it now
April 24th, 2015 8:47am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics