set mailNickName from csv

Welcome Forums General PowerShell Q&A set mailNickName from csv

This topic contains 12 replies, has 3 voices, and was last updated by

 
Participant
1 year, 1 month ago.

  • Author
    Posts
  • #82340

    Participant
    Points: 58
    Rank: Member

    I'm trying to set a 'not set' value of the mailNickName attribute with this code:

    $users = Import-Csv -Path .\ConfUsers.csv 
    #has two columns samAccountName,mailNickNameValue
                          
    foreach ($user in $users) {                        
     Get-ADUser -Identity $($user.samAccountName) -Properties mailnickname -Server domain.com |
        Set-ADUser -Identity $_ -Replace @{mailNickname=$_.mailNickNameValue}       
    }

    Error is on the Identity parameter.

  • #82345

    Participant
    Points: 1
    Rank: Member

    For some reasons, dotted notation fails to work with AD cmdlet. replace this line.

    $sam = $user.samAccountName; Get-ADUser -Identity $sam -Properties mailnickname -Server domain.com |
    
    • #82346

      Participant
      Points: 58
      Rank: Member
      Set-ADUser : Cannot validate argument on parameter 'Replace'. The argument is null or an element of the argument collection contains a null value.
    • #82352

      Participant
      Points: 0
      Rank: Member

      This should work:

      Foreach ($user in $users)
          { 
            Get-ADUser -Identity $user.SamAccountName -Properties mailnickname `|
            % {Set-ADUser -Identity $PSItem -Replace @{mailnickname=$user.mailnicknamevalue}}
          }
      
    • #82354

      Participant
      Points: 58
      Rank: Member
      Set-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument,
  • #82349

    Participant
    Points: 58
    Rank: Member

    Also tried this:

    $users = Import-Csv -Path .\ConfUsers.csv
    foreach ($user in $users) {
        $sam = $user.samAccountName; Get-ADUser -Identity $sam -Properties * -Server domain.com | 
            Set-ADUser $user -Server domain.com -Replace @{ mailNickName = "$($user.mailNicknameValue)" }
    }

    ...but get different error:

    et-ADUser : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline 
    input.
    • #82357

      Participant
      Points: 0
      Rank: Member

      Set-aduser accepts pipline input by value. Is there a specific reason for the initial "get-aduser"? You already have the samaccount and mailnickname values. Seems like you can accomplish the same by a simple:

      $users = Import-Csv -Path .\ConfUsers.csv
      foreach ($user in $users) {
          Set-aduser -Identity $user.samaccountname -Replace @{mailNickName = $user.mailNicknameValue}
      }
      
    • #82360

      Participant
      Points: 58
      Rank: Member

      ah ok, thank you I see now, that did work. So I really didn't need PSItem per your earlier script?

    • #82361

      Participant
      Points: 0
      Rank: Member

      Nope – i noticed that after the fact.

    • #82363

      Participant
      Points: 58
      Rank: Member

      Juan, where do I see that Set-ADUser accepts pipeline by value? I only see "false" for all parameters (there's alot)

      get-help Set-ADUser -Full

    • #82364

      Participant
      Points: 0
      Rank: Member

      Its under the identity parameter:

       -Identity 
           Specifies an Active Directory user object by providing one of the following property values. The identifier in parentheses is the LDAP display name for the attribute.
      
           Distinguished Name
      
           Example:  CN=SaraDavis,CN=Europe,CN=Users,DC=corp,DC=contoso,DC=com
      
           GUID (objectGUID)
      
           Example: 599c3d2e-f72d-4d20-8a88-030d99495f20
      
           Security Identifier (objectSid)
      
           Example: S-1-5-21-3165297888-301567370-576410423-1103
      
           SAM account name  (sAMAccountName)
      
           Example: saradavis
      
           The cmdlet searches the default naming context or partition to find the object. If two or more objects are found, the cmdlet returns a non-terminating error.
      
           This parameter can also get this object through the pipeline or you can set this parameter to an object instance.
      
           This example shows how to set the parameter to a distinguished name.
      
           -Identity  "CN=SaraDavis,CN=Europe,CN=Users,DC=corp,DC=contoso,DC=com"
      
           This example shows how to set this parameter to a user object instance named "userInstance".
      
           -Identity   $userInstance
      
           Required?                    true
           Position?                    1
           Default value
           Accept pipeline input?       True (ByValue)
           Accept wildcard characters?  false
      
    • #82366

      Participant
      Points: 58
      Rank: Member

      thanks again.

    • #82358

      Participant
      Points: 1
      Rank: Member

      Try this. This should resolve.

      $users = Import-Csv -Path .\ConfUsers.csv
      foreach ($user in $users) {
          $sam = $user.samAccountName
          $nickname = $user.mailNicknameValue 
          Get-ADUser -Identity $sam -Properties * -Server domain.com | Set-ADUser -Replace @{mailNickName = $nickname}
      }
      

The topic ‘set mailNickName from csv’ is closed to new replies.