Author Posts

January 13, 2015 at 3:39 am

Just trying to simplify an admin task. I need to take the user account logon name firstnamespacesurname and then create an alias firstname.surname.

If I process on a specific OU its fine

Add-PSSnapin Quest.ActiveRoles.ADManagement
Get-QADUser -enabled -sl 0 -LdapFilter '(mail=*)' -searchroot "OU=etc,OU=etc,dc=etc,dc=etc" |`
    foreach-object{ ( $alias = $_.sAMAccountName -replace " ","." )
            Set-QADUser -Identity $_ -ObjectAttributes @{mailNickname = $alias}


However, if I try and process from a list of users from csv or txt file it fails.

Add-PSSnapin Quest.ActiveRoles.ADManagement
Import-Csv -path "$Env:userprofile\Desktop\alias.csv" |`
    foreach-object { ( $alias = $_.sAMAccountName -replace " ",".") |`
        Set-QADUser -Identity $_ -ObjectAttributes @{mailNickname = $alias}}

January 13, 2015 at 5:41 am

There is a pipe char at the end of the third row in your second example.

This:

Add-PSSnapin Quest.ActiveRoles.ADManagement
Import-Csv -path "$Env:userprofile\Desktop\alias.csv" |`
    foreach-object { ( $alias = $_.sAMAccountName -replace " ",".") |`
        Set-QADUser -Identity $_ -ObjectAttributes @{mailNickname = $alias}}

should be:

Add-PSSnapin Quest.ActiveRoles.ADManagement
Import-Csv -path "$Env:userprofile\Desktop\alias.csv" |`
    foreach-object { ( $alias = $_.sAMAccountName -replace " ",".") 
        Set-QADUser -Identity $_ -ObjectAttributes @{mailNickname = $alias}}

Also, depending on how your csv is designed you might want to use -Identity $_.sAMAccountName for the Set-QADUser command.

January 13, 2015 at 5:55 am

Tried all kinds of ways, if i leave as $_ and remove the second pipe as suggested i get this error

Set-QADUser : Cannot bind parameter 'Identity'. Cannot convert the "@{user 1=user 2}" value of type "System.Management.Automation.PSCustomObject" to type 
"Quest.ActiveRoles.ArsPowerShellSnapIn.Data.IdentityParameter".
At C:\setalias.ps1:4 char:31
+         Set-QADUser -Identity $_ -ObjectAttributes @{mailNickname = $alias}}
+                               ~~
    + CategoryInfo          : InvalidArgument: (:) [Set-QADUser], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Quest.ActiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets.SetUserCmdlet

If i set to $_.sAMAccountname i get this

Set-QADUser : Cannot validate argument on parameter 'Identity'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At C:\setalias.ps1:4 char:31
+         Set-QADUser -Identity $_.sAMAccountName -ObjectAttributes @{mailNickname ...
+                               ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-QADUser], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Quest.ActiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets.SetUserCmdlet

January 13, 2015 at 6:39 am

I read up on the Set-QADUser, it only accepts DN, SID, GUID, UPN or Domain\UserName for the parameter -Identity
You need to have one of those in your CSV, if you don't you could append the domain name like this:

$DomainName = 'mydomain'
Add-PSSnapin Quest.ActiveRoles.ADManagement
Import-Csv -path "$Env:userprofile\Desktop\alias.csv" |`
    foreach-object { ( $alias = $_.sAMAccountName -replace " ",".") 
        Set-QADUser -Identity ('{0}\{1}' -f $DomainName, $_.sAMAccountName) -ObjectAttributes @{mailNickname = $alias}}