Can someone help fix the issue I’m having with the script below? The problem I’m having is some rows have empty cells. For example, not everyone has a cell phone so the cell for that individual is blank. When the script hits a blank cell, it errors out. Also open to any recommended changes overall <\/p>\n
Import-Csv C:\\Data.csv | ForEach {\n $manager = (Get-ADUser -Filter \"emailAddress -eq '$($_.SUPERVISOR_EMAIL)'\").distinguishedName\n Get-ADUser -Filter \"emailAddress -eq '$($_.EMAIL)'\" |\n set-aduser -employeeID $_.EMPLID -Manager $manager -Title $_.TITLE -Department $_.DEPARTMENT -Replace @{\n ExtensionAttribute1=$_.STATUS\n ExtensionAttribute2=$_.SUPERVISOR_NAME\n ExtensionAttribute3=$_.DEPARTMENT\n telephoneNumber=$_.DESK_PHONE\n homePhone=$_.MAIN_PHONE\n mobile=$_.CELL_PHONE\n }\n}\n<\/code><\/pre>","upvoteCount":3,"answerCount":6,"datePublished":"2021-09-24T21:09:17.000Z","author":{"@type":"Person","name":"ronnie8775","url":"https://community.spiceworks.com/u/ronnie8775"},"acceptedAnswer":{"@type":"Answer","text":"
Advertisement
try like so<\/p>\n
$csv = Import-Csv \"Data.csv\"\n\nforeach($row in $csv){\n # get aduser \n $User = Get-ADUser -Filter \"emailAddress -eq '$($row.email)'\"\n # get manager's distinguishedName\n $manager = (Get-ADUser -Filter \"emailAddress -eq '$($row.supervisor_email)'\").distinguishedName\n\n # set splat\n $setpr = @{\n identity = $User.SamAccountName\n replace = @{\n employeeID = $row.EMPLID\n Manager = $manager\n Title = $row.TITLE\n Department = $row.DEPARTMENT\n ExtensionAttribute1 = $row.STATUS\n ExtensionAttribute2 = $row.SUPERVISOR_NAME\n ExtensionAttribute3 = $row.DEPARTMENT\n telephoneNumber = $row.DESK_PHONE\n homePhone = $row.MAIN_PHONE\n mobile = $row.CELL_PHONE\n } \n }\n\n # remove blank attributes\n ($setpr.replace.GetEnumerator() | \n where-object { -not $_.Value }) |\n foreach-object { $setpr.replace.Remove($_.Name) }\n\n # set values\n try{\n set-aduser @setpr -ErrorAction Stop -Verbose\n }\n catch{\n $error[0]\n }\n\n}\n<\/code><\/pre>","upvoteCount":2,"datePublished":"2021-09-25T01:23:50.000Z","url":"https://community.spiceworks.com/t/powershell-ad-attribute-csv-import/812195/5","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},"suggestedAnswer":[{"@type":"Answer","text":"
Advertisement
Can someone help fix the issue I’m having with the script below? The problem I’m having is some rows have empty cells. For example, not everyone has a cell phone so the cell for that individual is blank. When the script hits a blank cell, it errors out. Also open to any recommended changes overall
<\/p>\n
Import-Csv C:\\Data.csv | ForEach {\n $manager = (Get-ADUser -Filter \"emailAddress -eq '$($_.SUPERVISOR_EMAIL)'\").distinguishedName\n Get-ADUser -Filter \"emailAddress -eq '$($_.EMAIL)'\" |\n set-aduser -employeeID $_.EMPLID -Manager $manager -Title $_.TITLE -Department $_.DEPARTMENT -Replace @{\n ExtensionAttribute1=$_.STATUS\n ExtensionAttribute2=$_.SUPERVISOR_NAME\n ExtensionAttribute3=$_.DEPARTMENT\n telephoneNumber=$_.DESK_PHONE\n homePhone=$_.MAIN_PHONE\n mobile=$_.CELL_PHONE\n }\n}\n<\/code><\/pre>","upvoteCount":3,"datePublished":"2021-09-24T21:09:17.000Z","url":"https://community.spiceworks.com/t/powershell-ad-attribute-csv-import/812195/1","author":{"@type":"Person","name":"ronnie8775","url":"https://community.spiceworks.com/u/ronnie8775"}},{"@type":"Answer","text":"change the foreach-object to a proper foreach and then you have to eval each property you want to set if there is a value for it in the CSV object.<\/p>\n
Also look into splatting that might help.<\/p>\n
Give it a try.<\/p>","upvoteCount":1,"datePublished":"2021-09-24T21:36:45.000Z","url":"https://community.spiceworks.com/t/powershell-ad-attribute-csv-import/812195/2","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
I think you could also use a try catch on each field. This way if there is no value, it will simply skip that line and continue. Neally will correct me if I’m wrong
<\/p>\n
This is untested code that uses the proper foreach Neally mentioned.<\/p>\n
$data = Import-Csv C:\\Data.csv \nForEach($d in $data){\n $manager = (Get-ADUser -Filter \"emailAddress -eq $d.SUPERVISOR_EMAIL\").distinguishedName\n $user = Get-ADUser -Filter \"emailAddress -eq $d.EMAIL\"\n try{set-aduser $user -employeeID $d.EMPLID}\n catch{write-host \"No employeeID found\"}\n try{set-aduser $user -Replace @{ExtensionAttribute1=$d.STATUS}}\n catch{write-host \"No ExtensionAttribute1 found\"}\n AND SO ON...\n }\n\n<\/code><\/pre>","upvoteCount":2,"datePublished":"2021-09-24T22:48:58.000Z","url":"https://community.spiceworks.com/t/powershell-ad-attribute-csv-import/812195/3","author":{"@type":"Person","name":"jrp78","url":"https://community.spiceworks.com/u/jrp78"}},{"@type":"Answer","text":"either try<\/p>\n
$users=Import-Csv C:\\Data.csv\nForeach($User in $Users){\n $manager = (Get-ADUser -Filter \"emailAddress -eq '$($user.SUPERVISOR_EMAIL)'\").distinguishedName\n\t$Parameters = @{\n\t\tmanager = $manager\n Title = $user.TITLE\n Department = $user.DEPARTMENT\n Replace = @{\n ExtensionAttribute1=$user.STATUS\n ExtensionAttribute2=$user.SUPERVISOR_NAME\n ExtensionAttribute3=$user.DEPARTMENT\n telephoneNumber=$user.DESK_PHONE}\n\t}\n\tIF($User.CELL_PHONE -ne $null){$Parameters.add(\"mobile\", $($User.CELL_PHONE))}\n\tIF($User.MAIN_PHONE -ne $null){$Parameters.add(\"homePhone\", $($User.MAIN_PHONE))}\n\n\t Get-ADUser -Filter \"emailAddress -eq '$($user.EMAIL)'\" |\n Set-ADUser @Parameters\n}\n<\/code><\/pre>\nI am not pretty sure about your CSV but above should work, also try<\/p>\n
IF($User.CELL_PHONE -like \"*\"){$Parameters.add(\"mobile\", $($User.CELL_PHONE))}\nIF($User.MAIN_PHONE -like \"*\"){$Parameters.add(\"homePhone\", $($User.MAIN_PHONE))}\n<\/code><\/pre>\nor try catch<\/p>\n
$users=Import-Csv C:\\Data.csv\n$ErrorActionPreference='stop'\nforeach ($user in $users){\n $manager = (Get-ADUser -Filter \"emailAddress -eq '$($user.SUPERVISOR_EMAIL)'\").distinguishedName\n $Params = @{\n manager = $manager\n Title = $user.TITLE\n Department = $user.DEPARTMENT\n Replace = @{\n ExtensionAttribute1=$user.STATUS\n ExtensionAttribute2=$user.SUPERVISOR_NAME\n ExtensionAttribute3=$user.DEPARTMENT\n telephoneNumber=$user.DESK_PHONE\n homePhone =$user.MAIN_PHONE\n mobile =$user.CELL_PHONE\n }\n }\n Try{\n Get-ADUser -Filter \"emailAddress -eq '$($user.EMAIL)'\" |\n Set-ADUser @Params\n }\n Catch{\n Write-Warning $_\n }\n }\n\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2021-09-25T01:09:30.000Z","url":"https://community.spiceworks.com/t/powershell-ad-attribute-csv-import/812195/4","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}},{"@type":"Answer","text":"\nIF($User.CELL_PHONE -ne $null){$Parameters.add(“mobile”, $($User.CELL_PHONE))}
\nIF($User.MAIN_PHONE -ne $null){$Parameters.add(“homePhone”, $($User.MAIN_PHONE))}<\/p>\n<\/blockquote>\n
comparing to $Null, $null should be on the left<\/p>\n