how to avoid error message being displayed

Welcome Forums General PowerShell Q&A how to avoid error message being displayed

Viewing 13 reply threads
  • Author
    Posts
    • #244658
      Participant
      Topics: 41
      Replies: 94
      Points: 560
      Rank: Major Contributor

      Hi All,

      I’ve got this code and want to check if a samaccount name already exists if yes then using the first 2 letters of the first name and try again.

      so basically my goal is check if samaccount exist if yes going for the alternative which is first 2 letters firstname + lastname and do the check again.
      if not found just show the text this samaccountname does not exist, and can be used
      thanks for your help
      best regards
      Paul
    • #244718
      Participant
      Topics: 4
      Replies: 7
      Points: 84
      Rank: Member

      You could use a filter to find the account which doesnt display an error if the result is null. The search filter may can take longer than directly searching for the account.

      • This reply was modified 1 month, 3 weeks ago by wbfick83.
      • This reply was modified 1 month, 3 weeks ago by wbfick83.
    • #244730
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      … or you could take a look at proper error handling with Powershell … 😉

      Get-Help about_Try_Catch_Finally -ShowWindow

    • #244796
      Participant
      Topics: 41
      Replies: 94
      Points: 560
      Rank: Major Contributor

      Hi Olaf thanks for your reaction

      What am I doing wrong with my try catch?

      I’ve modified it into this

      but the error message remains

       

    • #244808
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      You want to catch the error thrown by Get-ADUser but you call this cmdlet outside of the try block. 😉 AND … Get-ADUser does not trigger terminating errors by default. But that’s needed for the try catch block to work as intended. So you will have to force it to do so with the parameter -ErrorAction Stop and move the cmdlet inside the try block.

    • #244925
      Participant
      Topics: 41
      Replies: 94
      Points: 560
      Rank: Major Contributor

      Hi Olaf,
      sorry I have to disagree here

      in my First test

      I know the user exists so I want to go to round 2 by using this code

      where my try block is

      or am I missing something here?

    • #244934
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      Your first call of Get-ADUser is not wrapped in a try block. That’s what I meant. And because Get-ADUser does not throw terminating errrors by default you have to add -ErrorAction Stop to it inside the try block.

      EDIT:
      Oooops … my mistake … I miss read Get-User as Get-ADUser … so ignore this 😉

    • #244949
      Participant
      Topics: 8
      Replies: 558
      Points: 2,136
      Helping Hand
      Rank: Community Hero

      What is Get-User? The first call to it is still not in a try/catch. Why not handle it like this instead of all these checks? It’s simple, if it succeeds it will continue down the Try statement list. If it doesn’t, it will execute what’s in the Catch statement list.

    • #244967
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      What is Get-User?

      Get-User

      Anyway … to get the try block work properly you will have to add -ErrorAction Stop !!!!!!!

    • #244979
      Participant
      Topics: 8
      Replies: 558
      Points: 2,136
      Helping Hand
      Rank: Community Hero

      Anyway … to get the try block work properly you will have to add -ErrorAction Stop !!!!!!!

      Or as I did for the whole script

      • This reply was modified 1 month, 3 weeks ago by Doug Maurer.
    • #244997
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      Or as I did for the whole script

      Oooops …. I actually missed that … sorry … of course you’re right. 😉 😀

    • #245090
      Participant
      Topics: 41
      Replies: 94
      Points: 560
      Rank: Major Contributor

      HI Olaf and Doug thanks for your reactions was not aware that you could nest 2 try blocks together

       

       

    • #245175
      Participant
      Topics: 13
      Replies: 1753
      Points: 3,136
      Helping Hand
      Rank: Community Hero

      If the code is being executed in a remote Powershell session, Exchange doesn’t process -ErrorAction Stop properly and you need to define it like @Doug provided as globally set to ErrorAction to Stop. Bug that has bitten me, thought I would chime in. This code is unnecessary:

      Using Get-User is either going to find a user or not. If it finds the user, the variable will be set, otherwise you are going to the catch block. Even if you wanted to double check, it should just be:

      which is if the variable is NOT null, do X

    • #245250
      Participant
      Topics: 41
      Replies: 94
      Points: 560
      Rank: Major Contributor

      @rob Simmers
      thanks for the explanation

Viewing 13 reply threads
  • You must be logged in to reply to this topic.