Accepted way to return a value

Welcome Forums General PowerShell Q&A Accepted way to return a value

Viewing 7 reply threads
  • Author
    Posts
    • #260688
      Participant
      Topics: 1
      Replies: 2
      Points: 18
      Rank: Member

      Hi there,

      My co worker one time did this kind of stuff :

      I like this code and wanted to use it in a function that returns true or false for an existing aduser. But it could be anything.

      Could we use this kind or am I missing something or would an other way be better ? We also want to use this kind of thing if resourcegroups are missing in Azure.

      Thx!

      Jona

      • This topic was modified 3 weeks ago by grokkit. Reason: code formatting - please read the guide
    • #260901
      Moderator
      Topics: 5
      Replies: 244
      Points: 1,368
      Helping Hand
      Rank: Community Hero

      Just google it, there are plenty on the web and even one-liners.

      https://www.google.com/search?q=check+if+ad+user+exists+powershell

      Thank you.

      • This reply was modified 2 weeks, 6 days ago by Kiran.
    • #262472
      Participant
      Topics: 1
      Replies: 2
      Points: 18
      Rank: Member

      Hi thank you but my question is not whether it works it does, my question is : Is this a valid way. It’s more of a question like. Is this an ugly or not ugly solution.

       

      Thank you in advance anyone!

       

      Jona

    • #262532
      Participant
      Topics: 8
      Replies: 617
      Points: 2,393
      Helping Hand
      Rank: Community Hero

      That’s a very explicit and verbose way to do it, yes. I personally would simplify it like this.

      or if you want to check against samaccountname

      Or if you wanted to check either

      If there is a value produced (meaning a user was found) then the cast to [bool] will be $true otherwise it will be $false

    • #262571
      Senior Moderator
      Topics: 9
      Replies: 1373
      Points: 5,061
      Helping Hand
      Rank: Community MVP

      @JonaUK – $Error is an in-built variable in PowerShell and we should not set value to it.

      Using Try Catch is a good option, but we should be checking the exception as well before setting the Flag, There can be many reasons why Get-ADUser fails.

    • #262697
      Participant
      Topics: 16
      Replies: 1794
      Points: 3,300
      Helping Hand
      Rank: Community Hero

      Check this out:

      #PSTip Validate if a user exists in Active Directory

      Also, I’d just mention that wrapping this is a function is just unnecessary code. There are multiple one line checks for a boolean, so just make a descriptive variable:

    • #263882
      Participant
      Topics: 1
      Replies: 2
      Points: 18
      Rank: Member

      Yes thank you both of you this so much clarifies things for me!

      How would we check a specific error? Of course I can google it, but I like some expert answer.

      Thx so much!

       

      Jona

    • #263921
      Senior Moderator
      Topics: 9
      Replies: 1373
      Points: 5,061
      Helping Hand
      Rank: Community MVP

      Errors do print the exception by default until PS 5.1 and with Get-Error in PowerShell core we will get all the necessary information from errors.
      So when catching the exception you can specify the exception type to catch block.

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