Hey all, I am still pretty new at this whole powershell thing but I need to update our AD with department, title, phone numbers, etc (before me users were given a username and email and then left alone). I have a CSV file with the information in it, and I can get it to work for some users but not all. My script currently is<\/p>\n
Import-Csv C:\\StaffInfo.csv | ForEach-Object {\n$department = $_.department\nif($department.Length -lt 1)\n{$department = $null}\n$title = $_.title\nif($title.Length -lt 1)\n{$title = $null}\n$office = $_.office\nif($office.Length -lt 1)\n{$office = $null}\n$StreetAddress = $_.StreetAddress\nif($StreetAddress.Length -lt 1)\n{$StreetAddress = $null}\n$State = $_.State\nif($State.Length -lt 1)\n{$State = $null}\n$postalcode = $_.postalcode\nif($postalcode.Length -lt 1)\n{$postalcode = $null}\n$city = $_.city\nif($city.Length -lt 1)\n{$State = $null}\n$officephone = $_.telephonenumber\nif($officephone.Length -lt 1)\n{$officephone = $null}\n$MobilePhone = $_.Mobile \nif($Mobilephone.Length -lt 1)\n{$MobilePhone = $null}\n\nSet-ADUser -Identity $_.name -Department $_.department -title $_.title -Office $_.office -StreetAddress $_.streetAddress -State $_.state -PostalCode $_.postalCode -MobilePhone $_.mobile -OfficePhone $_.telephoneNumber -City $_.city\n}\n<\/code><\/pre>\n
Advertisement
And the error I am getting is:<\/p>\n
Set-ADUser : replace\nAt C:\\ADUpdate.ps1:30 char:1\n+ Set-ADUser -Identity $_.name -Department $_.department -title $_.titl ...\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n + CategoryInfo : InvalidOperation: (JohnUser:ADUser) [Set-ADUser], ADInvalidOperationException\n + FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.SetADUser\n<\/code><\/pre>\n
Advertisement
Some of the fields in the CSV are blank such as:<\/p>\n
name,First Name,Last Name,Title,Department,Office,StreetAddress,City,State,PostalCode,telephoneNumber,Mobile,Accreditations\nJohnUser,John,User,Systems Administrator,Information Technology,MainOffice,123 Main Street,Anytown,CA,12345,123.555.5555,,\nJaneUser,Jane,User,Assistant,,OtherOffice,\"555 Second Street, Suite 500\",Other Town,WA,54321,,321.888.9900,\n<\/code><\/pre>\nAny thoughts?<\/p>","upvoteCount":1,"answerCount":4,"datePublished":"2018-01-17T14:52:42.000Z","author":{"@type":"Person","name":"davidfischer","url":"https://community.spiceworks.com/u/davidfischer"},"acceptedAnswer":{"@type":"Answer","text":"
I think i figured it out, I was using old code and working with various solutions I had found. I was never referring Set-ADUser to the $null’ed empty values, insted setting everything right and then pointing it back to the imported CSV array. So instead of:<\/p>\n
Set-ADUser -Identity $_.name -Department $_.department -title $_.title -Office $_.office -StreetAddress $_.streetAddress -State $_.state -PostalCode $_.postalCode -MobilePhone $_.mobile -OfficePhone $_.telephoneNumber -City $_.city\n\n<\/code><\/pre>\nI used:<\/p>\n
Set-ADUser -Identity $_.name -Department $department -title $title -Office $office -StreetAddress $streetAddress -State $state -PostalCode $postalCode -MobilePhone $mobile -OfficePhone $telephoneNumber -City $city\n<\/code><\/pre>\nand that seems to work now, it helps when I actually fix all my code to adapt a solution.<\/p>","upvoteCount":2,"datePublished":"2018-01-17T15:26:45.000Z","url":"https://community.spiceworks.com/t/set-aduser-replace-error-when-updating-ad-from-csv-file/629186/4","author":{"@type":"Person","name":"davidfischer","url":"https://community.spiceworks.com/u/davidfischer"}},"suggestedAnswer":[{"@type":"Answer","text":"
Hey all, I am still pretty new at this whole powershell thing but I need to update our AD with department, title, phone numbers, etc (before me users were given a username and email and then left alone). I have a CSV file with the information in it, and I can get it to work for some users but not all. My script currently is<\/p>\n
Import-Csv C:\\StaffInfo.csv | ForEach-Object {\n$department = $_.department\nif($department.Length -lt 1)\n{$department = $null}\n$title = $_.title\nif($title.Length -lt 1)\n{$title = $null}\n$office = $_.office\nif($office.Length -lt 1)\n{$office = $null}\n$StreetAddress = $_.StreetAddress\nif($StreetAddress.Length -lt 1)\n{$StreetAddress = $null}\n$State = $_.State\nif($State.Length -lt 1)\n{$State = $null}\n$postalcode = $_.postalcode\nif($postalcode.Length -lt 1)\n{$postalcode = $null}\n$city = $_.city\nif($city.Length -lt 1)\n{$State = $null}\n$officephone = $_.telephonenumber\nif($officephone.Length -lt 1)\n{$officephone = $null}\n$MobilePhone = $_.Mobile \nif($Mobilephone.Length -lt 1)\n{$MobilePhone = $null}\n\nSet-ADUser -Identity $_.name -Department $_.department -title $_.title -Office $_.office -StreetAddress $_.streetAddress -State $_.state -PostalCode $_.postalCode -MobilePhone $_.mobile -OfficePhone $_.telephoneNumber -City $_.city\n}\n<\/code><\/pre>\nAnd the error I am getting is:<\/p>\n
Set-ADUser : replace\nAt C:\\ADUpdate.ps1:30 char:1\n+ Set-ADUser -Identity $_.name -Department $_.department -title $_.titl ...\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n + CategoryInfo : InvalidOperation: (JohnUser:ADUser) [Set-ADUser], ADInvalidOperationException\n + FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.SetADUser\n<\/code><\/pre>\nSome of the fields in the CSV are blank such as:<\/p>\n
name,First Name,Last Name,Title,Department,Office,StreetAddress,City,State,PostalCode,telephoneNumber,Mobile,Accreditations\nJohnUser,John,User,Systems Administrator,Information Technology,MainOffice,123 Main Street,Anytown,CA,12345,123.555.5555,,\nJaneUser,Jane,User,Assistant,,OtherOffice,\"555 Second Street, Suite 500\",Other Town,WA,54321,,321.888.9900,\n<\/code><\/pre>\nAny thoughts?<\/p>","upvoteCount":1,"datePublished":"2018-01-17T14:52:42.000Z","url":"https://community.spiceworks.com/t/set-aduser-replace-error-when-updating-ad-from-csv-file/629186/1","author":{"@type":"Person","name":"davidfischer","url":"https://community.spiceworks.com/u/davidfischer"}},{"@type":"Answer","text":"
Instead of<\/p>\n
Import-Csv C:StaffInfo.csv<\/p>\n
do this …<\/p>\n
Import-Csv C:\\StaffInfo.csv<\/p>","upvoteCount":0,"datePublished":"2018-01-17T15:04:45.000Z","url":"https://community.spiceworks.com/t/set-aduser-replace-error-when-updating-ad-from-csv-file/629186/2","author":{"@type":"Person","name":"mattbrown13","url":"https://community.spiceworks.com/u/mattbrown13"}},{"@type":"Answer","text":"
Matt,<\/p>\n
In the script it actually is correct, that was a copy/paste error, not a script issue.<\/p>","upvoteCount":0,"datePublished":"2018-01-17T15:09:22.000Z","url":"https://community.spiceworks.com/t/set-aduser-replace-error-when-updating-ad-from-csv-file/629186/3","author":{"@type":"Person","name":"davidfischer","url":"https://community.spiceworks.com/u/davidfischer"}}]}}