Returning AD Attribute Department from a user query

Home Forums Scripting PowerShell Returning AD Attribute Department from a user query

This topic contains 7 replies, has 3 voices, and was last updated by Avatar Dext 2 years, 6 months ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • Avatar
    5habbaranks
    Participant
    #166913

    Hi,

    Ive got the following PowerShell script to lookup the users department

    [FONT=Lucida Console][SIZE=1][COLOR=#ff4500][FONT=Lucida Console][SIZE=1][COLOR=#ff4500][FONT=Lucida Console][SIZE=1][COLOR=#ff4500]$Employeename[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1] [/SIZE][/FONT][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][COLOR=#a9a9a9][FONT=Lucida Console][SIZE=1][COLOR=#a9a9a9][FONT=Lucida Console][SIZE=1][COLOR=#a9a9a9]=[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1] [/SIZE][/FONT][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][COLOR=#0000ff][FONT=Lucida Console][SIZE=1][COLOR=#0000ff][FONT=Lucida Console][SIZE=1][COLOR=#0000ff]Read-host[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1] [/SIZE][/FONT][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][COLOR=#8b0000][FONT=Lucida Console][SIZE=1][COLOR=#8b0000][FONT=Lucida Console][SIZE=1][COLOR=#8b0000]”Please Enter The Employee Name e.g test.user”[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
    [FONT=Lucida Console][SIZE=1][COLOR=#ff4500][FONT=Lucida Console][SIZE=1][COLOR=#ff4500][FONT=Lucida Console][SIZE=1][COLOR=#ff4500]$Department[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1] [/SIZE][/FONT][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][COLOR=#a9a9a9][FONT=Lucida Console][SIZE=1][COLOR=#a9a9a9][FONT=Lucida Console][SIZE=1][COLOR=#a9a9a9]=[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1] [/SIZE][/FONT][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][COLOR=#0000ff][FONT=Lucida Console][SIZE=1][COLOR=#0000ff][FONT=Lucida Console][SIZE=1][COLOR=#0000ff]Get-ADUser[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1] [/SIZE][/FONT][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][COLOR=#000080][FONT=Lucida Console][SIZE=1][COLOR=#000080][FONT=Lucida Console][SIZE=1][COLOR=#000080]-Identity[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1] [/SIZE][/FONT][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][COLOR=#ff4500][FONT=Lucida Console][SIZE=1][COLOR=#ff4500][FONT=Lucida Console][SIZE=1][COLOR=#ff4500]$Employeename[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1] [/SIZE][/FONT][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][COLOR=#000080][FONT=Lucida Console][SIZE=1][COLOR=#000080][FONT=Lucida Console][SIZE=1][COLOR=#000080]-Property[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1] [/SIZE][/FONT][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][COLOR=#8a2be2][FONT=Lucida Console][SIZE=1][COLOR=#8a2be2][FONT=Lucida Console][SIZE=1][COLOR=#8a2be2]Department[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1] [/SIZE][/FONT][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][COLOR=#a9a9a9][FONT=Lucida Console][SIZE=1][COLOR=#a9a9a9][FONT=Lucida Console][SIZE=1][COLOR=#a9a9a9]|[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1] [/SIZE][/FONT][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][COLOR=#0000ff][FONT=Lucida Console][SIZE=1][COLOR=#0000ff][FONT=Lucida Console][SIZE=1][COLOR=#0000ff]Select-Object[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1] [/SIZE][/FONT][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][COLOR=#8a2be2][FONT=Lucida Console][SIZE=1][COLOR=#8a2be2][FONT=Lucida Console][SIZE=1][COLOR=#8a2be2]Department[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
    [FONT=Lucida Console][SIZE=1][COLOR=#0000ff][FONT=Lucida Console][SIZE=1][COLOR=#0000ff][FONT=Lucida Console][SIZE=1][COLOR=#0000ff]write-host[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1] [/SIZE][/FONT][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][COLOR=#ff4500][FONT=Lucida Console][SIZE=1][COLOR=#ff4500][FONT=Lucida Console][SIZE=1][COLOR=#ff4500]$Department [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
    [/CODE]

    It returns @{Department=xxxxxxxx} which seems to work – but is that correct? How come it adds the @ and brackets?

    Thanks

    [CODE]$Employeename = Read-host “Please Enter The Employee Name e.g test.user”
    $Department = Get-ADUser -Identity $Employeename -Property Department | Select-Object Department
    write-host $Department
    [/CODE]

    It returns @{Department=xxxxxxxx} which seems to work – but is that correct? How come it adds the @ and brackets?

    Thanks

    JeremyW
    JeremyW
    Moderator
    #271450

    It’s because you’re getting an property in an object. To just set the property, use this code:
    $Department = (Get-ADUser -Identity $Employeename -Property Department).Department[/CODE][CODE]$Department = (Get-ADUser -Identity $Employeename -Property Department).Department[/CODE]

    Avatar
    Dext
    Member
    #379102

    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?
    [FONT=Lucida Console][SIZE=1][COLOR=#ff4500][FONT=Lucida Console][SIZE=1][COLOR=#ff4500][FONT=Lucida Console][SIZE=1][COLOR=#ff4500]$Department[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1] [/SIZE][/FONT][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][COLOR=#a9a9a9][FONT=Lucida Console][SIZE=1][COLOR=#a9a9a9][FONT=Lucida Console][SIZE=1][COLOR=#a9a9a9]=[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1] ([/SIZE][/FONT][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][COLOR=#0000ff][FONT=Lucida Console][SIZE=1][COLOR=#0000ff][FONT=Lucida Console][SIZE=1][COLOR=#0000ff]Get-ADUser[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1] [/SIZE][/FONT][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][COLOR=#000080][FONT=Lucida Console][SIZE=1][COLOR=#000080][FONT=Lucida Console][SIZE=1][COLOR=#000080]-Identity[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1] [/SIZE][/FONT][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][COLOR=#ff4500][FONT=Lucida Console][SIZE=1][COLOR=#ff4500][FONT=Lucida Console][SIZE=1][COLOR=#ff4500]$Employeename[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1] [/SIZE][/FONT][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][COLOR=#000080][FONT=Lucida Console][SIZE=1][COLOR=#000080][FONT=Lucida Console][SIZE=1][COLOR=#000080]-Property[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1] [/SIZE][/FONT][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][COLOR=#8a2be2][FONT=Lucida Console][SIZE=1][COLOR=#8a2be2][FONT=Lucida Console][SIZE=1][COLOR=#8a2be2]Department[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1])[/SIZE][/FONT][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][COLOR=#a9a9a9][FONT=Lucida Console][SIZE=1][COLOR=#a9a9a9][FONT=Lucida Console][SIZE=1][COLOR=#a9a9a9].[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1]Department[/SIZE][/FONT][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][COLOR=#a9a9a9][FONT=Lucida Console][SIZE=1][COLOR=#a9a9a9][FONT=Lucida Console][SIZE=1][COLOR=#a9a9a9],[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1] ([/SIZE][/FONT][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][COLOR=#0000ff][FONT=Lucida Console][SIZE=1][COLOR=#0000ff][FONT=Lucida Console][SIZE=1][COLOR=#0000ff]Get-ADUser[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1] [/SIZE][/FONT][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][COLOR=#000080][FONT=Lucida Console][SIZE=1][COLOR=#000080][FONT=Lucida Console][SIZE=1][COLOR=#000080]-Identity[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1] [/SIZE][/FONT][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][COLOR=#ff4500][FONT=Lucida Console][SIZE=1][COLOR=#ff4500][FONT=Lucida Console][SIZE=1][COLOR=#ff4500]$Employeename[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1] [/SIZE][/FONT][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][COLOR=#000080][FONT=Lucida Console][SIZE=1][COLOR=#000080][FONT=Lucida Console][SIZE=1][COLOR=#000080]-Property[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1] [/SIZE][/FONT][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][COLOR=#8a2be2][FONT=Lucida Console][SIZE=1][COLOR=#8a2be2][FONT=Lucida Console][SIZE=1][COLOR=#8a2be2]DistinguishedName[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1])[/SIZE][/FONT][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][COLOR=#a9a9a9][FONT=Lucida Console][SIZE=1][COLOR=#a9a9a9][FONT=Lucida Console][SIZE=1][COLOR=#a9a9a9].[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Lucida Console][SIZE=1][FONT=Lucida Console][SIZE=1]DistinguishedName [/SIZE][/FONT][/SIZE][/FONT]
    [/CODE][CODE]$Department = (Get-ADUser -Identity $Employeename -Property Department).Department, (Get-ADUser -Identity $Employeename -Property DistinguishedName).DistinguishedName
    [/CODE]

    JeremyW
    JeremyW
    Moderator
    #271451

    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:

    $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.
    [/CODE][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.
    [/CODE]

    Avatar
    Dext
    Member
    #379106

    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}
    }

    JeremyW
    JeremyW
    Moderator
    #271455

    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?

    Avatar
    Dext
    Member
    #379109

    Hi sorry – this was a snippit. But its working now you said about the fact the distinguished name comes back anyway – thank you :)

    JeremyW
    JeremyW
    Moderator
    #271459

    Cool. Sounds good. Let me know if you have any more questions.

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.