Skipping white space

Welcome Forums General PowerShell Q&A Skipping white space

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
10 months, 2 weeks ago.

  • Author
    Posts
  • #103975

    Participant
    Topics: 3
    Replies: 7
    Points: 2
    Rank: Member

    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

    Participant
    Topics: 1
    Replies: 1323
    Points: 1,641
    Helping Hand
    Rank: Community Hero

    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

The topic ‘Skipping white space’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort