Announcement

Collapse
No announcement yet.

Returning AD Attribute Department from a user query

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Returning AD Attribute Department from a user query

    Hi,

    Ive got the following PowerShell script to lookup the users department

    Code:
    $Employeename=Read-host"Please Enter The Employee Name e.g test.user"
    $Department=Get-ADUser-Identity$Employeename-PropertyDepartment|Select-ObjectDepartment
    write-host$Department 
    It returns @{Department=xxxxxxxx} which seems to work - but is that correct? How come it adds the @ and brackets?

    Thanks


  • #2
    It's because you're getting an property in an object. To just set the property, use this code:
    Code:
    $Department = (Get-ADUser -Identity $Employeename -Property Department).Department
    Regards,
    Jeremy

    Network Consultant/Engineer
    Baltimore - Washington area and beyond
    www.gma-cpa.com

    Comment


    • #3
      Thanks for this, can this method be used to get multiple properties such as DistinguishedName also?

      Ive written it like this but is there a shorter version?
      Code:
      $Department= (Get-ADUser-Identity$Employeename-PropertyDepartment).Department, (Get-ADUser-Identity$Employeename-PropertyDistinguishedName).DistinguishedName 
      Last edited by 5habbaranks; 17th March 2017, 11:13.

      Comment


      • #4
        Yes, you can get multiple properties in a single object. There's tons of ways to do this. Can you share what you're trying to do with the info and I'd be able to help you a bit better. But here's a general example:

        Code:
        $userobj = Get-ADUser $Employeename -Property Department
        $userobj.Department
        $userobj.DistinguishedName #Note that the distinguished name is returned by default and you don't need to specify it.
        Regards,
        Jeremy

        Network Consultant/Engineer
        Baltimore - Washington area and beyond
        www.gma-cpa.com

        Comment


        • #5
          Hi thanks for this (that aspect works fine using your code) what Im trying to do now is use an if else if within a switch

          switch ($CompanyName)
          {
          Site {$AllStaffDistro = "OU Details"
          Add-ADGroupMember -Identity $AllStaffDistro $Username
          }
          $Department = (Get-ADUser -Identity $Employeename -Property Department).Department

          if ($Department -Match 'RoleA') {Add-ADGroupMember -Identity $RoleA $Employeename}
          Elseif ($Department -Match 'RoleB') {Add-ADGroupMember -Identity $RoleB $Employeename}
          Elseif ($Department -Match 'RoleC') {Add-ADGroupMember -Identity $RoleC $Employeename}
          Elseif ($Department -Match 'RoleD') {Add-ADGroupMember -Identity $RoleD $Employeename}
          Elseif ($Department -Match 'RoleE') {Add-ADGroupMember -Identity $RoleE $Employeename}
          Elseif ($Department -Match 'RoleF') {Add-ADGroupMember -Identity $RoleF $Employeename}
          }

          Comment


          • #6
            Is there a question you had about the code? Looks like you're missing some things from your switch statement. Is this a snipit of the entire switch statement?
            Regards,
            Jeremy

            Network Consultant/Engineer
            Baltimore - Washington area and beyond
            www.gma-cpa.com

            Comment


            • #7
              Hi sorry - this was a snippit. But its working now you said about the fact the distinguished name comes back anyway - thank you

              Comment


              • #8
                Cool. Sounds good. Let me know if you have any more questions.
                Regards,
                Jeremy

                Network Consultant/Engineer
                Baltimore - Washington area and beyond
                www.gma-cpa.com

                Comment

                Working...
                X