locked
Add-ADGroupMemeber not working RRS feed

  • Question

  • Hi,

    Just trying to do something single :-) and query the DHCP DB for any clients with a hostname PC* and then add this to the AD security group "Group1" but the Add-ADGroupMember doesn't like the pipeline input it is getting to add the group :

    Get-DhcpServerv4Scope | Get-DhcpServerv4lease | where-object {$_.Hostname -like "PC*"} | ForEach-Object {Add-ADGroupMember -Identity "Group1" -Members $_.hostname}

    Any help would be great.

    Thanks

    Bill


    Tuesday, June 20, 2017 1:29 PM

All replies

  • Add-AdGroupMember takes a computer object or a DistinguishedName only.

    \_(ツ)_/

    Tuesday, June 20, 2017 1:38 PM
  • Hi,

    Ok, I added the select hostname to the pipeline but still fails. 

    Get-DhcpServerv4Scope | Get-DhcpServerv4lease | where-object {$_.Hostname -like "PC*"} | select hostname | ForEach-Object {Add-ADGroupMember -Identity "Group1" -Members $_.hostname}

    Add-ADGroupMember : Cannot find an object with identity: 'PC01.Bill.Local' under: 'DC=BILL,DC=Local'.

    Tuesday, June 20, 2017 2:04 PM
  • "hostname" will not work.  You must use a computer object or a computer DistinguishedName.

    $computer  = Get-AdComputer hostname
    Add-AdGroupMember group1 -Member $computer


    \_(ツ)_/

    Tuesday, June 20, 2017 2:27 PM
  • Hi,

    Ok, I added the select hostname to the pipeline but still fails. 

    Get-DhcpServerv4Scope | Get-DhcpServerv4lease | where-object {$_.Hostname -like "PC*"} | select hostname | ForEach-Object {Add-ADGroupMember -Identity "Group1" -Members $_.hostname}

    Add-ADGroupMember : Cannot find an object with identity: 'PC01.Bill.Local' under: 'DC=BILL,DC=Local'.

    Bill, this worked:

    $pc = Get-DhcpServerv4Scope | Get-DhcpServerv4lease | where-object {$_.Hostname -like "bos*"} | select -ExpandProperty hostname 
    
    foreach($name in $pc)
    {
        $name
        Add-ADGroupMember -Identity "DHCP Users" -Members "cn=$name,cn=computers,dc=test,dc=local"
    }

    Ensure that your hostname is reside in Computers contained or related OU. if it resides in ou you need to change this cn=$name into ou=$name

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    • Edited by Hello_2018 Wednesday, June 21, 2017 4:53 AM
    Wednesday, June 21, 2017 4:53 AM