Radim88
asked on
Powershell error handling
Hello,
Pls would you advice, I have service account, and in description is written samaccount name of the user. I am extracting it with regex. But how can I do it, that when user is not there it will write-output me that user is not there. I am trying it with try/catch or trap but no output. Please advice.
Many thanks
Pls would you advice, I have service account, and in description is written samaccount name of the user. I am extracting it with regex. But how can I do it, that when user is not there it will write-output me that user is not there. I am trying it with try/catch or trap but no output. Please advice.
Many thanks
Get-QADUser -Enabled 'xxxx' -LdapFilter "((!sAMAccountName=Vxx*)(!sAMAccountName=xxx*))" |
%{
try{
$descSamAccount = $_ |%{[regex]::Match($_.description,"\w\d{7}")}
$descNameMail = $descSamAccount.value | Get-QADUser `
-ErrorAction Stop | select -ExpandProperty email
$descNameMail
}
catch
{
Write-Output "user is not set or doesnt exists"
}
To answer your question, I do not suspect an error will occur when a lookup fails because no error is generated. It simply does not return a result, therefore the the catch block has nothing passed to it and the control falls through. Chris' script shifts from a try catch-block to the if-then pattern. This focuses on a boolean result instead an error result, which, in this case is what you would expect.
ASKER
thanks a lot, I was only curious about this: -ErrorAction Stop , it wont set it as a stop error?
Thx
Thx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thx a lot
Perhaps like this.
Open in new window
Chris