• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1042
  • Last Modified:

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

0
Radim88
Asked:
Radim88
  • 2
  • 2
1 Solution
 
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
 
Radim88Author Commented:
thx a lot
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now