I have been unable to figure out how to get my script to work on updating the title, department, and manager fields through Powershell.<\/p>\n
My CSV file has a header of:<\/p>\n
Here is my script:<\/p>\n
Connect-AzureAD<\/p>\n
$CSVrecords = Import-Csv C:\\temp\\test.csv -Delimiter “;”<\/p>\n
$SkippedUsers = @()\n$FailedUsers = @()<\/p>\n
foreach ($CSVrecord in $CSVrecords) {\n$upn = $CSVrecord.UserPrincipalName\n$manager = $CSVrecord.Manager\n$user = Get-AzureADUser -Filter “userPrincipalName eq ‘$upn’”\n$Mangerobj = Get-AzureADUser -Filter “userprincipalname eq ‘$manager’”\nif ($user) {\ntry{\n$user | Set-AzureADUser -Department $CSVrecord.Department -JobTitle $CSVrecord.jobTitle | Set-AzureADUserManager -RefObjectid $Managerobj.objectid\n} catch {\n$FailedUsers += $upn\nWrite-Warning “$upn user found, but FAILED to update.”\n}\n}\nelse {\nWrite-Warning “$upn not found, skipped”\n$SkippedUsers += $upn\n}\n}<\/p>\n
My output lists:<\/p>\n
WARNING: [email protected]<\/a> user found, but FAILED to update.<\/p>\nNow it worked the first time through by updating the Job Title and Department, but it is not finding the Manager. I just cannot get it to work to update the manager field.<\/p>\nAny advice welcome!<\/p>","upvoteCount":5,"answerCount":9,"datePublished":"2021-10-05T15:32:47.000Z","author":{"@type":"Person","name":"dannig178","url":"https://community.spiceworks.com/u/dannig178"},"suggestedAnswer":[{"@type":"Answer","text":"I have been unable to figure out how to get my script to work on updating the title, department, and manager fields through Powershell.<\/p>\nMy CSV file has a header of:<\/p>\n\n\n\n\nuserPrincipalName;department;jobTitle;manager<\/th>\n<\/tr>\n<\/thead>\n<\/table>\n<\/div>Here is my script:<\/p>\n<\/a>Connect to AzureAD<\/h1>\nConnect-AzureAD<\/p>\n<\/a>Get CSV content<\/h1>\n$CSVrecords = Import-Csv C:\\temp\\test.csv -Delimiter “;”<\/p>\n<\/a>Create arrays for skipped and failed users<\/h1>\n$SkippedUsers = @()\n$FailedUsers = @()<\/p>\n<\/a>Loop trough CSV records<\/h1>\nforeach ($CSVrecord in $CSVrecords) {\n$upn = $CSVrecord.UserPrincipalName\n$manager = $CSVrecord.Manager\n$user = Get-AzureADUser -Filter “userPrincipalName eq ‘$upn’”\n$Mangerobj = Get-AzureADUser -Filter “userprincipalname eq ‘$manager’”\nif ($user) {\ntry{\n$user | Set-AzureADUser -Department $CSVrecord.Department -JobTitle $CSVrecord.jobTitle | Set-AzureADUserManager -RefObjectid $Managerobj.objectid\n} catch {\n$FailedUsers += $upn\nWrite-Warning “$upn user found, but FAILED to update.”\n}\n}\nelse {\nWrite-Warning “$upn not found, skipped”\n$SkippedUsers += $upn\n}\n}<\/p>\n<\/a>Array skipped users<\/h1>\n<\/a>$SkippedUsers<\/h1>\n<\/a>Array failed users<\/h1>\n<\/a>$FailedUsers<\/h1>\nMy output lists:<\/p>\nWARNING: [email protected]<\/a> user found, but FAILED to update.<\/p>\nNow it worked the first time through by updating the Job Title and Department, but it is not finding the Manager. I just cannot get it to work to update the manager field.<\/p>\nAny advice welcome!<\/p>","upvoteCount":5,"datePublished":"2021-10-05T15:32:47.000Z","url":"https://community.spiceworks.com/t/updating-manager-field-with-powershell/813126/1","author":{"@type":"Person","name":"dannig178","url":"https://community.spiceworks.com/u/dannig178"}},{"@type":"Answer","text":"Welcome.<\/p>\nIf you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>\n
Now it worked the first time through by updating the Job Title and Department, but it is not finding the Manager. I just cannot get it to work to update the manager field.<\/p>\n
Any advice welcome!<\/p>","upvoteCount":5,"answerCount":9,"datePublished":"2021-10-05T15:32:47.000Z","author":{"@type":"Person","name":"dannig178","url":"https://community.spiceworks.com/u/dannig178"},"suggestedAnswer":[{"@type":"Answer","text":"
WARNING: [email protected]<\/a> user found, but FAILED to update.<\/p>\nNow it worked the first time through by updating the Job Title and Department, but it is not finding the Manager. I just cannot get it to work to update the manager field.<\/p>\nAny advice welcome!<\/p>","upvoteCount":5,"datePublished":"2021-10-05T15:32:47.000Z","url":"https://community.spiceworks.com/t/updating-manager-field-with-powershell/813126/1","author":{"@type":"Person","name":"dannig178","url":"https://community.spiceworks.com/u/dannig178"}},{"@type":"Answer","text":"Welcome.<\/p>\nIf you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>\n
Any advice welcome!<\/p>","upvoteCount":5,"datePublished":"2021-10-05T15:32:47.000Z","url":"https://community.spiceworks.com/t/updating-manager-field-with-powershell/813126/1","author":{"@type":"Person","name":"dannig178","url":"https://community.spiceworks.com/u/dannig178"}},{"@type":"Answer","text":"
Welcome.<\/p>\n
If you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>\n