Larry David
Larry David

Reputation: 55

Update Multiple User ExtensionAttributes by UPN

Looking to import UserPrincipalName and updates ExtensionAttributes 1-15

I currently have a script that works to update an individual UserPrincipalName ExtensionAttributes and works great.

 Set-AzureADUserExtension -ObjectId [email protected] -ExtensionName extensionattribute1 -ExtensionValue "stuff"
 Set-AzureADUserExtension -ObjectId [email protected] -ExtensionName extensionattribute2 -ExtensionValue "things"
 Set-AzureADUserExtension -ObjectId [email protected] -ExtensionName extensionattribute3 -ExtensionValue "stuff"
 Set-AzureADUserExtension -ObjectId [email protected] -ExtensionName extensionattribute4 -ExtensionValue "things"
 Set-AzureADUserExtension -ObjectId [email protected] -ExtensionName extensionattribute5 -ExtensionValue "test"
 Set-AzureADUserExtension -ObjectId [email protected] -ExtensionName extensionattribute6 -ExtensionValue "test"
 Set-AzureADUserExtension -ObjectId [email protected] -ExtensionName extensionattribute7 -ExtensionValue "test"
 Set-AzureADUserExtension -ObjectId [email protected] -ExtensionName extensionattribute8 -ExtensionValue "florida"
 Set-AzureADUserExtension -ObjectId [email protected] -ExtensionName extensionattribute9 -ExtensionValue "12444"
 Set-AzureADUserExtension -ObjectId [email protected] -ExtensionName extensionattribute10 -ExtensionValue "stuff"
 Set-AzureADUserExtension -ObjectId [email protected] -ExtensionName extensionattribute11 -ExtensionValue "things"
 Set-AzureADUserExtension -ObjectId [email protected] -ExtensionName extensionattribute12 -ExtensionValue "test"
 Set-AzureADUserExtension -ObjectId [email protected] -ExtensionName extensionattribute13 -ExtensionValue "test"
 Set-AzureADUserExtension -ObjectId [email protected] -ExtensionName extensionattribute14 -ExtensionValue "test"
 Set-AzureADUserExtension -ObjectId [email protected] -ExtensionName extensionattribute15 -ExtensionValue "test"

But as you can see can be time consuming if I have several users to update.

So I tried to create the following

$users = import-csv "C:\temp\womp.csv"

$users | ForEach-Object{

Set-AzureADUserExtension -ObjectId $_.extensionattribute1 (Get-AzureADUser -ObjectID $_.UserPrincipalName).objectID
Set-AzureADUserExtension -ObjectId $_.extensionattribute2 (Get-AzureADUser -ObjectID $_.UserPrincipalName).objectID
Set-AzureADUserExtension -ObjectId $_.extensionattribute3 (Get-AzureADUser -ObjectID $_.UserPrincipalName).objectID
Set-AzureADUserExtension -ObjectId $_.extensionattribute4 (Get-AzureADUser -ObjectID $_.UserPrincipalName).objectID
Set-AzureADUserExtension -ObjectId $_.extensionattribute5 (Get-AzureADUser -ObjectID $_.UserPrincipalName).objectID

}

But have gotten numerous of errors.

Set-AzureADUserExtension : A positional parameter cannot be found that accepts argument

and

Get-AzureADUser : Error occurred while executing GetUser Code: Request_ResourceNotFound

Attached is a screenshot is my CSV setup: CSV Setup

Thank you,

Upvotes: 1

Views: 1963

Answers (1)

Joy Wang
Joy Wang

Reputation: 42123

I could not understand what the Set-AzureADUserExtension -ObjectId $_.extensionattribute1 (Get-AzureADUser -ObjectID $_.UserPrincipalName).objectID mean in your script, I suppose you want to set the several extensions for all the users in your .csv file.

Besides, if we create a new extension property, the name should be like extension_0380f0f700c040b5aa577c9268940b53_MyNewProperty, why yours are extensionattribute1, extensionattribute2.

Try the script works for me, my sample just has two users and two extensions, the logic is the same, you could try it.

$users = import-csv "C:\Users\joyw\Desktop\testfile.csv"

$users | ForEach-Object{

Set-AzureADUserExtension -ObjectId $_.UserPrinciaplName -ExtensionName extension_242365dc795xxxxxfb73236a3_testex1 -ExtensionValue $_.extension_242365dc795xxxxxfb73236a3_testex1
Set-AzureADUserExtension -ObjectId $_.UserPrinciaplName -ExtensionName extension_242365dc795xxxxxfb73236a3_MyProp -ExtensionValue $_.extension_242365dc795xxxxxfb73236a3_MyProp


}

My testfile.csv file:

UserPrinciaplName,extension_242365dc795xxxxxfb73236a3_testex1,extension_242365dc795xxxxxfb73236a3_MyProp
[email protected],testvalue1,testvaule2
[email protected],testv1,testv2

enter image description here

Check the result for the two users:

enter image description here

Upvotes: 1

Related Questions