none
Set-ADUser cmdlet failing RRS feed

  • Question

  • I have some code and a csv file where I'm trying to bulk update users in AD.

    $cred = Get-Credential
    $csv = '.\UpdateUsers - Copy.csv'
    
    $userlist = Import-Csv $csv
    $userlist | foreach {
        Set-ADUser -Identity $_.SamAccountName -EA stop -City $_.City -Company $_.Company -Country $_.Country -Department $_.Department -Description $_.Description -DisplayName $_.DisplayName -EmailAddress $_.EmailAddress -EmployeeID $_.EmployeeID -EmployeeNumber $_.EmployeeNumber -Fax $_.Fax -GivenName $_.GivenName -MobilePhone $_.MobilePhone -Office $_.Office -OfficePhone $_.OfficePhone -POBox $_.POBox -PostalCode $_.PostalCode -SamAccountName $_.SamAccountName -State $_.State -StreetAddress $_.StreetAddress -Surname $_.Surname -Title $_.Title -UserPrincipalName $_.UserPrincipalName -Manager $_.manager -Credential $cred
    }
    The command succeeds on most of the users in the csv file.  It seems to fail, however, when it comes across an attribute that is null. For example, there are users in the csv file that have no value for the OfficePhone attribute.  How would I handle the script allowing null values for any of these properties?

    Monday, September 14, 2015 11:01 PM

Answers

  • Load like this and splat the result:

    $p=@{
    Identity=$_.SamAccountName
    ErrorAction='Stop'

    } if($_.City){$p.City=$_.City} if($_.Company){$p.Company=$_.Company} if($_.Country){$p.Country=$_.Country} if($_.Country){$p.Department=$_.Department} #... etc Set-AdUser @p


    \_(ツ)_/

    • Proposed as answer by Kriss Milne Tuesday, September 15, 2015 12:53 AM
    • Marked as answer by Thomas Garrity Tuesday, September 15, 2015 2:03 AM
    Monday, September 14, 2015 11:21 PM

All replies

  • Yup. You have to filter out null attributes


    \_(ツ)_/

    Monday, September 14, 2015 11:15 PM
  • Load like this and splat the result:

    $p=@{
    Identity=$_.SamAccountName
    ErrorAction='Stop'

    } if($_.City){$p.City=$_.City} if($_.Company){$p.Company=$_.Company} if($_.Country){$p.Country=$_.Country} if($_.Country){$p.Department=$_.Department} #... etc Set-AdUser @p


    \_(ツ)_/

    • Proposed as answer by Kriss Milne Tuesday, September 15, 2015 12:53 AM
    • Marked as answer by Thomas Garrity Tuesday, September 15, 2015 2:03 AM
    Monday, September 14, 2015 11:21 PM
  • That was the ticket. Thanks! Whatever spatting is, haha. Wish I could follow that logic.
    Tuesday, September 15, 2015 2:02 AM
  • Hi Thomas,

    First of all, it's splatting.

    Basically, you create a hashtable and store it in a variable ($P in jrv's example). A hashtable is an object where you can store any kinds of values by assigning them a name. Then you add all property-values that are not empty to that hashtable, assigning the name of the parameter, as Set-ADUser uses them.

    Finally, by splatting (Using the variable name with a '@' rather than a '$') the cmdlet uses those entries in the hashtable as if you had specified the parameters of the identical name.

    Confused? Let's add an example:

    # Traditional way to run a cmdlet:
    Get-ChildItem -Path "C:\temp" -Filter "*.txt" -Recurse
    
    # Running the cmdlet with splatting:
    $hash = @{
    Path = "C:\temp"
    Filter = "*.txt"
    }
    $hash.Recurse = $true
    Get-ChildItem @hash

    Both ways of running Get-Childitem do exactly the same, as both specify the exact same parameters, only in a different way.

    The traditional way is simpler to use while splatting is more flexible (as it allows you to easily decide dynamically which parameters to specify based on input).

    Hope this allowed you to understand just what jrv tried to show you.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Tuesday, September 15, 2015 12:03 PM
  • Nice job!
    Tuesday, September 15, 2015 12:26 PM
  • That was the ticket. Thanks! Whatever spatting is, haha. Wish I could follow that logic.

    PS C:\> help about_Splatting
    


    -- Bill Stewart [Bill_Stewart]

    Tuesday, September 15, 2015 2:12 PM
    Moderator