none
How to use IIF in Select Command? RRS feed

  • Question

  • I have a scenario in AD Database that I have an AD group which has both Users and Groups as its members. The names of both the ad objects are stored in different attributes. Name of User is in Name1 attribute and name of the group is in Name2 attribute. The thing is when I need to display both users & groups in same table I need to have both ad attributes in a single column Object Name. So, I want to check if the objectclass is 'user' pick text in Name1 & if 'group' pick text in Name2.

    Here's is the script I began with and I am finding a bit unavailability of knowledge as I m new to PowerShell scripting.

    Get-ADGroupMember -Identity $GroupGUID | where {($_.objectclass -eq 'user') -or ($_.objectclass -eq 'group') } | 
    
    Get-ADObject -Properties Name1, Name2 | Select IIF($_.objectclass -eq 'user' "Name1" "Name2")
    Even if it is possible with a normal if-else statement it would be okay because there are chances of having AD Objects other than 'users' & 'groups'.


    • Edited by Nikul Vyas Tuesday, June 25, 2019 9:33 PM Added Footer text
    Tuesday, June 25, 2019 9:31 PM

All replies

  • If I got you right it is much easier than you think:
    Get-ADGroupMember -Identity $GroupGUID |
        Select-Object -Property Name, objectclass

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Tuesday, June 25, 2019 9:40 PM
  • Thanks for responding, but no based on the object class I need to merge two properties into one column, name of user is in Name1 while Name of group is stored in Name2 how ever both types of objects has Name1, Name2 properties so I need to show them in one column.

    Suppose Group is 'Test Group'

    CN : …

    ObjectGUID : 123-456-XXXX

    ObjectClass: group

    Name1: 

    Name2: 'Test Group'

    And User Properties are 

    CN: …

    ObjectGUID : 789-456-XXXX

    ObjectClass: user

    Name1: 'Test User'

    Name2: 

    So when I fetch the property Name1 & Name2.

    The expected output need to be 

    DisplayName      ObjectGUID
    ----------------    ------------------
    Test Group         123-456-XXXX
    Test User           789-456-XXXX
    


    • Edited by Nikul Vyas Tuesday, June 25, 2019 9:49 PM Added Expected Output
    Tuesday, June 25, 2019 9:49 PM
  • The name of a group or a user is stored in the "Name" attribute of every object.  There is no name1 or name2 attribute.

    If you are trying to say you also have custom attributes then you will need to specify the properties of the objects explicitly.

    You can start with this to see how AD and PowerShell work.

    Get-ADGroupMember TestGrp | 
        ForEach-Object{
            if($_.ObjectClass -eq 'User'){
                # process user object
                $_ | Get-ADUser -Properties Name1
            }else{
                # process group
                $_ | Get-ADGroup -Properties Name2
            }
        } |
        Select-Object Name, ObjectClass, Name1, Name2


    \_(ツ)_/

    Tuesday, June 25, 2019 10:09 PM
  • Yes I do have custom attributes but lets say I am using Name storing groupName & DisplayName storing user's display name  how to show them in one column? I want to do it because an AD Group can have both group & users as members.

    so it can be like 

    Member Name

    ----------------

    Test Group --> Coming from Name property

    Test User   --> Coming from DisplayName property

    Tuesday, June 25, 2019 10:24 PM
  • Just use an "if" in a computed property.


    \_(ツ)_/

    Tuesday, June 25, 2019 11:12 PM
  • Tuesday, June 25, 2019 11:54 PM
  • I cannot believe how something so simple can be made so complicated.

    Just add a computed property. No other items required. No "switches" or "compound ifs" or other funcky things.

    Get-ADGroupMember TestGrp | 
        ForEach-Object{
            if($_.ObjectClass -eq 'User'){
                # process user object
                $_ | Get-ADUser -Properties Name1
            }else{
                # process group
                $_ | Get-ADGroup -Properties Name2
            }
        } |
        Select-Object Name, ObjectClass, @{n='Name_1_2';e={$_.Name1 + $_.Name2}}

    or, if you need a bigger hint:

    @{n='Name_1_2';e={$if($_.ObjectClass -eq 'User'){$_.Name1}else{$_.Name2}}}


    \_(ツ)_/


    • Edited by jrv Wednesday, June 26, 2019 12:10 AM
    Wednesday, June 26, 2019 12:08 AM
  • We can also do this assuming that the attributes you want can be discovered correctly:

    Get-ADGroupMember TestGrp |
        Get-ADObject -Properties Name1,Name2 |
        Select-Object ObjectClass, @{n='Name';e={if($_.ObjectClass -eq 'User'){$_.Name1}else{$_.Name2}}}


    \_(ツ)_/


    • Edited by jrv Wednesday, June 26, 2019 12:19 AM
    Wednesday, June 26, 2019 12:18 AM
  • This may make what is happening more understandable:

    $computedName = @{
        Name = 'Name'
        Expression={
            if ($_.ObjectClass -eq 'User') {
                $_.Name1
            } else {
                $_.Name2
            }
        }
    }
    Get-ADGroupMember TestGrp |
        Get-ADObject -Properties Name1, Name2 |
        Select-Object ObjectClass, $computedName


    \_(ツ)_/

    Wednesday, June 26, 2019 12:22 AM