Author Posts

October 17, 2017 at 6:53 pm

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.

October 17, 2017 at 7:04 pm

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 |

October 17, 2017 at 7:20 pm

Set-ADUser : Cannot validate argument on parameter 'Replace'. The argument is null or an element of the argument collection contains a null value.

October 17, 2017 at 7:39 pm

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.

October 17, 2017 at 7:40 pm

This should work:

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

October 17, 2017 at 7:46 pm

Set-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument,

October 17, 2017 at 7:48 pm

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}
}

October 17, 2017 at 7:49 pm

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}
}

October 17, 2017 at 7:52 pm

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

October 17, 2017 at 7:54 pm

Nope – i noticed that after the fact.

October 17, 2017 at 8:06 pm

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

October 17, 2017 at 8:10 pm

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