Reputation: 205
We are updating our SIP for email addresses and we are going to use a PowerShell script to monitor when AD is updated and then update the local machine.
I'm to the part where I split the email address at the @
symbol and I'm looking to the left of the @
symbol.
Example:
[email protected]
I can split it correctly where I just get
FirstName.LastName
but when I go to check the condition if it contains a .
, I can't get a correct true of false.
Example:
$sipaddress = "[email protected]"
$splitname = $sipaddress.Split("@")[0]
# at this point, $splitname varible will contain "FirstName.LastName"
if ($splitname -match '.') {
Write-Host "TRUE"
} else {
Write-Host "False"
}
# this returns TRUE which is perfect
BUT, if I change the variable to like this for testing
$sipaddress = "[email protected]"
the $splitname
variable will contain FirstNameLastName
, and it still returns TRUE when that is not correct. It should return false because there is no .
.
What am I doing wrong? I tried to use -contains
in the if
statement but that does not work either.
Is there a way to try and check for a -match
for the .
?
Upvotes: 0
Views: 3181
Reputation: 200503
The -match
operator does a regular expression match, so matching .
will match any character except newlines, not just dots. If you want to match a literal dot you need to escape it (\.
) or do a different kind of comparison, for instance a wildcard match with the -like
operator (-like '*.*'
) or by using the Contains()
method of the string object.
The -contains
operator checks if an array contains a particular element. To be able to use that operator you'd need to convert your string to a character array:
[char[]]$splitname -contains '.'
I wouldn't recommend doing this, though. The other methods are more convenient.
Modified code:
$sipaddress = "[email protected]"
$localpart, $domain = $sipaddress -split '@'
if ($localpart.Contains('.')) {
Write-Host 'True'
} else {
Write-Host 'False'
}
Upvotes: 3