none
Powershell loop to cast value to variable from multiple possible sources RRS feed

  • Question

  • Hi,

    I'm struggling to work out the logic in this one. I need to query 3 or four sources for a particular value and cast the first resultt into a variable and cannot figure out how to do this.

    I am looking for a users Manager and it could be stored in three different value locations. This should give some idea:-

            DO
                {
                $manager = (get-aduser $user.LINE_MANAGER -Properties Displayname).displayname
                $manager = (get-aduser $user.ADDITIONAL_LINE_MANAGER -Properties Displayname).displayname
                $manager = (get-aduser $aduser.manager -Properties Displayname).displayname
                $manager = "No Line Manager Specified"
                }
            While ( $manager -eq $null)
    So I want the loop to query the sources until it finds a value and passes that into the $manager variable the break out the loop. I've thought about doing an If = $null function on each line, but it seems clumsy. Any pretty way of doing this?

    Friday, May 17, 2019 10:23 AM

Answers

  • OK,

    With all your input (Thanks, made me look at it differently) and a bit of playing this is as clean as I think I'm going to get it:-

        $sources = $user.LINE_MANAGER, $user.ADDITIONAL_LINE_MANAGER, $aduser.manager
        $manager = "No Line Manager Specified"
        foreach($source in $sources)
            {
            if (!($source -eq $null))
                {
                $manager = (get-aduser $source -Properties Displayname).displayname
                BREAK
                }
            }
    Which means I can easily add sources and it'll return the first result. Thanks again team, appreciate your time.

    • Marked as answer by NewbieNik73 Monday, May 20, 2019 10:22 AM
    Monday, May 20, 2019 10:21 AM

All replies

  • I have found the dirty way of doing this:-

            $adUser = get-aduser $user.staff_no -properties manager, displayname
            $manager = (get-aduser $user.LINE_MANAGER -Properties Displayname).displayname
            If ($manager -eq $null)
                {
                $manager = (get-aduser $user.ADDITIONAL_LINE_MANAGER -Properties Displayname).displayname
    	    If ($manager -eq $null)
                   {
                   $manager = (get-aduser $user.manager –properties Displayname).displayname
                   IF ($manager -eq $null)
                      {
                      $manager = "No Line Manager Specified"
                      } 
                   }
    	   }
    But am sure there's a more eloquent way of achieving this result. Especially if more sources need to be added.


    Friday, May 17, 2019 12:46 PM
  • Yes, I'd say this logic would be cleaner:

    if(something){
     do
    }
    elseif(somethingelse){
     doelse
    }
    elseif(somethingelse2){
     doelse2
    }
    else{
     dolast
    }

    Friday, May 17, 2019 1:08 PM
  • Marco, yes. That makes sense normally, that is until you apply it to what I'm trying to do...

     if ($manager -eq $null)
    {do
    { $manager = get-aduser $user.line_manager
    }
    Elseif($manager -eq $null)..........
    

    It falls over on the "ElseIf" because it's not an ELSE, it's still an IF..I think that's why I'm struggling. Also if I need to add a couple of more sources, it gets rather messy.

    I was looking at SWITCH, but can't see how that would work in this case as most of the loop operators seem to work on a definitive return (Like the "WHILE ($i -lt 10){Do {$i++}}" examples, that will have a definitive addition to $i in each loop)

    There may not be a more graceful way of doing it than the way I've posted, but I feel there is..somewhere 

    Friday, May 17, 2019 1:21 PM
  • One simple way to do this:

    if($manager = (get-aduser $user.LINE_MANAGER -Properties Displayname).displayname){
    }elseif($manager = (get-aduser $user.ADDITIONAL_LINE_MANAGER -Properties Displayname).displayname){
    }elseif($manager = (get-aduser $aduser.manager -Properties Displayname).displayname){
    }else{$manager = "No Line Manager Specified"}
    

    This will select the first manager found.


    \_(ツ)_/

    • Proposed as answer by jrvModerator Monday, May 20, 2019 7:33 AM
    • Unproposed as answer by NewbieNik73 Monday, May 20, 2019 9:02 AM
    Friday, May 17, 2019 6:59 PM
    Moderator
  • A "Perl-ish" way to do this is to use "||" (an "or" operator with a greater operator precedence than "or"). Something like this (without all the curly braces and elseif/else):

    $manager = ( (get-aduser $user.LINE_MANAGER -Properties Displayname).displayname -or
                 (get-aduser $user.ADDITIONAL_LINE_MANAGER -Properties Displayname).displayname -or
                 (get-aduser $aduser.manager -Properties Displayname).displayname -or
                 "No Line Manager Specified"
                )


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Proposed as answer by jrvModerator Friday, May 17, 2019 7:38 PM
    • Marked as answer by NewbieNik73 Monday, May 20, 2019 7:24 AM
    • Unmarked as answer by NewbieNik73 Monday, May 20, 2019 7:31 AM
    • Unproposed as answer by jrvModerator Monday, May 20, 2019 7:34 AM
    Friday, May 17, 2019 7:36 PM
  • Even easier and uses much the same approach.


    \_(ツ)_/

    Friday, May 17, 2019 7:39 PM
    Moderator
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Monday, May 20, 2019 6:16 AM
    Moderator
  • Apologies, it was the weekend and I try to avoid all work related things during leave periods. (Difficult, but totally worth it)

    I'm going to test Richs answer, it seems logical, although not as tidy as I thought we could get with Powershell. I'll post results here if it works.

    ## Update##

    Richs answer doesn't work. It only seems to return True or False. Most likely that's my fault, but I've tested it with a basic test ($a = "1". $b=$null, $c=$null, $var = {$a -or $b -or $c} and it also returns TRUE.

    Any further thoughts?


    Monday, May 20, 2019 7:26 AM
  • Actually Rich's method does not work. It only ever returns true or false.  My version does work.


    \_(ツ)_/

    Monday, May 20, 2019 7:33 AM
    Moderator
  • jrv,

    Unfortunately your answer didn't work either. Lookup fails on the first IF command as it cannot lookup a $Null AD user. It doesn't seem to attempt the ElseIfs at all.

    I'm not sure I can carry out the manager validation within the loop and may have to extract that out into its own function to wrap around the manager retrieval.

    Seemed so simple last week :)

    Monday, May 20, 2019 7:46 AM
  • OK,

    With all your input (Thanks, made me look at it differently) and a bit of playing this is as clean as I think I'm going to get it:-

        $sources = $user.LINE_MANAGER, $user.ADDITIONAL_LINE_MANAGER, $aduser.manager
        $manager = "No Line Manager Specified"
        foreach($source in $sources)
            {
            if (!($source -eq $null))
                {
                $manager = (get-aduser $source -Properties Displayname).displayname
                BREAK
                }
            }
    Which means I can easily add sources and it'll return the first result. Thanks again team, appreciate your time.

    • Marked as answer by NewbieNik73 Monday, May 20, 2019 10:22 AM
    Monday, May 20, 2019 10:21 AM