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




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"
}

Open in new window

Radim88Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris DentPowerShell DeveloperCommented:
Hey :)

Perhaps like this.
Get-QADUser -Enabled 'xxxx' -LdapFilter "((!sAMAccountName=Vxx*)(!sAMAccountName=xxx*))" | ForEach-Object {
  If ($_.Description -Match '\w\d{7}') {
    $User = Get-QADUser -SamAccountName $Matches[0]
    If ($User) {
      $User | Select-Object -ExpandProperty Email
    } Else {
      Write-Host "Could not find user"
    }
  } Else {
    Write-Host "Username not set"
  }
}

Open in new window

Chris
0
wls3Commented:
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.
0
Radim88Author Commented:
thanks a lot, I was only curious about this:  -ErrorAction Stop  , it wont set it as a stop error?

Thx
0
Chris DentPowerShell DeveloperCommented:

There are two types of error, Terminating and Non-Terminating. ErrorAction Stop turns Non-Terminating Errors into Terminating Errors, but nothing beyond that.

However, the code you posted will not throw an error of any kind, for the reason wls3 flagged. For example:

"" | Get-QADUser

You won't get an error, you'll get everyone.

My example does away with this, for a start it won't pass "", because that wouldn't generate a RegEx Match. And it passes the value it does have as an explicit SamAccountName, shifting away from ambiguous name resolution.

Chris
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Radim88Author Commented:
thx a lot
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.