Reputation: 11
i need help with PowerShell
I need help with my script in logical comparison using a Csv:
SamAccountName
,Othertelephone
,employeeId
,PreferredLanguage
I have developed a script which update AD user details based on certain condition.
EmployeeID
should be ABC and XYZ only, no other ID should be taken. and this field can be blank.Othertelephone
cannot be blankPreferredLanguage
should be EN|ES only.no other parameter should be taken and this field can be blank.script will update user if above conditions are met.
I am stuck at point 1, it is not ignoring the blank field. if I leave it blank it executes the code block.
IF($_.employeeId -notmatch 'ABC|XYZ' or $_EmployeeId.trim() -eq "")
{ write-output " Id is not proper" }
Upvotes: 0
Views: 80
Reputation: 68331
You don't really need an -OR and another condition test. Just add another qualifier in your regex:
$_.EmployeeID -notmatch '^(ABC|XYZ|\s*)$'
That will test for whitespace or a null string, in addition to the ABC and XYZ values.
Edit: If the ABC and XYZ value tests need to be unbounded (they can appear anywhere in the attribute value), isolate the begin and end anchors to just the null/whitespace test:
$_.EmployeeID -notmatch 'ABC|XYZ|^\s*$'
Upvotes: 3
Reputation: 123
Can you try this
$_.employeeId -ne "ABC" -and $_.employeeId -ne "XYZ" -and $_.employeeId -ne ""
Upvotes: 0
Reputation: 28194
$_.employeeId -notmatch 'ABC|XYZ' or $_EmployeeId.trim() -eq ""
You need a -
before or
here and a .
after $_
in the second test.
$_.employeeId -notmatch 'ABC|XYZ' -or $_.EmployeeId.trim() -eq ""
However, this statement will still evaluate to $true
if EmployeeId
is empty because an empty ID also meets your first test's criteria - empty does not match ABC
or XYZ
I think you really want this:
$_.employeeId -notmatch 'ABC|XYZ' -and $_.EmployeeId.trim() -ne ""
You might want to test for $null
as well as the empty string there.
Upvotes: 0