Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

How to check csv for blank fields when updating AD from a CSV file

$
0
0

how to simply update my script to check if a field in the csv  file is blank or not with out having to rewrite the entire script.

The script works perfectly untill it sees a a blank field. it will error our.

 


# Import AD Module            
Import-Module ActiveDirectory           

write-Host 'Starting to update AD Attributes.......' -NoNewline -ForegroundColor Yellow           
# Import CSV into variable $users          
     
$users = Import-Csv -Delimiter "," -Path C:\temp\Users.csv           
# Loop through CSV and update users if the exist in CVS file           
           
foreach ($user in $users) {           
#Search in specified OU and Update existing attributes           
 Get-ADUser -Filter "samaccountname -eq '$($user.samaccountname)'" -Properties * -SearchBase "DC=corp,DC=fishing,DC=com"|           
  Set-ADUser -GivenName $($user.Firstname) -Surname $($user.Lastname) -DisplayName $($user.DisplayName) -Title $($user.JobTitle) -MobilePhone $($user.MobilePhone) -OfficePhone $($user.OfficePhone) -Street $($user.Street) -City $($user.City) -State $($user.State) -PostalCode $($user.PostalCode) -EmailAddress $($user.EmailAddress) -Department $($user.Department) -Office $($user.Office) -Manager $($user.Manager) -Company $($user.Company) -Country $($user.Country) -Description $($user.Description)
       
}

Write-Host 'done!' -ForegroundColor Green


Viewing all articles
Browse latest Browse all 6937

Trending Articles