简体   繁体   中英

How to fix ADD ADGroupMember error “cannot validate argument on a parameter 'Identity', the property is null”

I'm trying to import users and groups from a .CSV, get user's DistinguisedName from other domain and add to group in local domain. I keep getting

Cannot validate argument on parameter "Identity". The identity property is null or empty

The .CSV has ColumnA "NAMES" and contains SamAccountName, ColumnB "GROUPS" and contains the local domain's groups I want to add the users to. These are Domain Local groups.

I've tried this multiple ways and same result. I even write the variables to the console and they appear legitimate.

This works fine adding a single user to Group:

$dn = Get-ADUser samaccountname -Server "dc.domain.com"
Add-ADGroupMember -Identity "GroupName" -Members $dn

But not when I try to import data from .csv as below:

foreach ($Name in (Import-Csv $inputfile)) {
    Write-Host "ADGROUP = $Name.Groups"
    Get-ADUser $Name -Server "dc.domain.com" |
        Add-ADGroupMember -MemberOf $Name.Groups
}

Another example which produces same 'Identity' null error:

foreach ($Name in (Import-Csv $inputfile)) {
    $dn = Get-ADUser $Name -Server "dc.domain.com"
    Write-Host "DN= $DN"
    $ADGroup = $Name.Groups
    Write-Host "ADGROUP = $adgroup"
    Add-ADGroupMember -Identity $ADGroup -Members $dn
}

I didn't really need to modify my .csv headers but did change them to "Names" and "Groups" and then modified my code as follows and it's working well now grabbing DN names from other domain and is populating Domain Local groups in local domain where I'm running this script from...

$inputfile = "D:\scripts\file.csv"
ForEach ($Name in (import-csv $inputfile)) {
    $dn = get-aduser $Name.names -Server "dc.domain.com"
    Write-host "DN= $DN"
    $ADGroup = $Name.Groups
    write-host "ADGROUP = $adgroup"
    Add-ADGroupMember -Identity $ADGroup -Members $dn
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM