Skipping white space

This topic contains 1 reply, has 2 voices, and was last updated by  Olaf Soyk 2 weeks ago.

  • Author
    Posts
  • #103975

    Aricky
    Participant

    Good evening,
    I am using csv file to feed into the script below that updates user attributes but there are some properties which doesn't have values so it is erroring out.
    I have used -erroraction silientlyContinue to get it working but what would be the best practice in this scenario and how can I add it to script below.

    $data = Import-csv -path C:\temp\Userdata.csv
    Foreach ($user in $data)
    {
    Get-ADUser -Filter "samaccountname -eq '$($user.sAMAccountName)'" | Set-ADUser -Replace @{revision = "$($user.revision)"} -ErrorAction SilentlyContinue
    Get-ADUser -Filter "samaccountname -eq '$($user.sAMAccountName)'" | Set-ADUser -Replace @{title = "$($user.title)"} -ErrorAction SilentlyContinue
    Get-ADUser -Filter "samaccountname -eq '$($user.sAMAccountName)'" | Set-ADUser -Replace @{street = "$($user.street)"} -ErrorAction SilentlyContinue
    Get-ADUser -Filter "samaccountname -eq '$($user.sAMAccountName)'" | Set-ADUser -Replace @{streetAddress = "$($user.streetAddress)"} -ErrorAction SilentlyContinue
    }
    
  • #103976

    Olaf Soyk
    Participant

    If I've got this right you can use the instance method of the Set-ADUser cmdlet like this:

    $props = 'Revision','Title','Street','StreetAddress'
    Import-CSV C:\temp\Userdata.csv |
        ForEach-Object{
            if($u = Get-AdUser $_.sAMAccountName -Properties $props){
                $u.Title = $_.Title 
                $u.Revision = $_.Revision 
                $u.Street = $_.Street 
                $u.StreetAddress = $_.StreetAddress
                Set-AdUser -Instance $u
            }else{
                Write-Host 'User not found'
            }
        }

    ... untested!!
    The credit for this goes to jrv and Richard Mueller ... found in the Microsoft Technet Forum for Powershell

You must be logged in to reply to this topic.