Powershell help please

Sawyer, Ian 1 Reputation point
2022-04-05T12:26:26.063+00:00

I have created a custom attribute and want to populate it from a csv i have that has SAM account names in column 1 and custom attribute in column 2
I run this script and get this error
Import-Module ActiveDirectory
$Attribcsv=Import-csv “c:\scripts\sf\beauty.csv”
ForEach ($User in $Attribcsv)
{
Get-ADUser -Identity $User.samAccountName | set-ADUser -Add @{SalesforceRole="Beauty"}}

So i'd hope to populate the custom attribute of the users in the csv with the word Beauty. Instead I get this error, can anyone please point me in the right direction? Thanks

Get-ADUser : Cannot validate argument on parameter 'Identity'. The Identity property on the argument is null or empty.
At line:5 char:22

  • Get-ADUser -Identity $User.samAccountName | set-ADUser -Add @{Salesfo ...
  • ~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
  • FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,431 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 45,416 Reputation points
    2022-04-05T14:44:35.387+00:00

    Are you sure that there's a column named "samAccountName" in the CSV file's header record?

    Run this to verify the column is named the way you say it is:

    [array]$x = Import-csv "c:\scripts\sf\beauty.csv"
    if ($x[0].psobject.properties.name -notcontains "samAccountName"){
        write-host "samAccountName isn't a column in the CSV!" -ForegroundColor Red
        return
    }
    

    Assuming that it doesn't say the column name is missing, perhaps there are empty lines in the CSV? Try adding some simple data verification to the script:

    Import-csv "c:\scripts\sf\beauty.csv" |
        ForEach-Object{
            if ($_.samAccountName.Trim().Length -gt 1){
                Get-ADUser -Identity ($_.samAccountName.Trim()) | 
                    Set-ADUser -Add @{SalesforceRole="Beauty"}
            }
        }
    

    Note that you're trying to add a property name "SalesforceRole", not an extension attribute., to the user object. Is that what you want? Have you extended the AD schema to include that attribute?

    0 comments No comments