Automated PS1 script to update O365 Contacts

%3CLINGO-SUB%20id%3D%22lingo-sub-826542%22%20slang%3D%22en-US%22%3EAutomated%20PS1%20script%20to%20update%20O365%20Contacts%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-826542%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Tech%20Community%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20was%20tasked%20to%20automate%20the%20update%20of%20O365%20Contacts%20from%20the%20automated%20export%20of%20details%20from%20one%20of%20our%20systems%20which%20holds%20the%20mobile%2Ctelephone%20%23.%20The%20script%20below%20used%20is%20working%20but%20I%20don't%20know%20how%20to%20automate%20it%20as%20my%20GA%20account%20has%20MFA%20activate%20hence%20cannot%20use%20app%20password%20on%20the%20PS1.%3C%2FP%3E%3CP%3EJust%20wanted%20to%20know%20as%20well%20which%20account%20has%20been%20updated%20or%20not%20as%20the%20info%20from%20the%20csv%20file%20contains%20user%20that%20is%20not%20on%20O365%20as%20well.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EError%20Encountered%3A%3C%2FP%3E%3CP%3ESet-MsolUser%20%3A%20User%20Not%20Found.%20User%3A%20.%3CBR%20%2F%3EAt%20line%3A1%20char%3A21%3CBR%20%2F%3E%2B%20...%20%7C%20foreach%20%7BSet-MsolUser%20-UserPrincipalName%20%24_.'Email%20Address'%20-Mobil%20...%3CBR%20%2F%3E%2B%20~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%3CBR%20%2F%3E%2B%20CategoryInfo%20%3A%20OperationStopped%3A%20(%3A)%20%5BSet-MsolUser%5D%2C%20MicrosoftOnlineException%3CBR%20%2F%3E%2B%20FullyQualifiedErrorId%20%3A%20Microsoft.Online.Administration.Automation.UserNotFoundException%2CMicrosoft.Online.Administration.Automation.SetUser%3C%2FP%3E%3CP%3ESet-MsolUser%20%3A%20You%20must%20provide%20a%20required%20property%3A%20Parameter%20name%3A%20ObjectId%3CBR%20%2F%3EAt%20line%3A1%20char%3A21%3CBR%20%2F%3E%2B%20...%20%7C%20foreach%20%7BSet-MsolUser%20-UserPrincipalName%20%24_.'Email%20Address'%20-Mobil%20...%3CBR%20%2F%3E%2B%20~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%3CBR%20%2F%3E%2B%20CategoryInfo%20%3A%20OperationStopped%3A%20(%3A)%20%5BSet-MsolUser%5D%2C%20MicrosoftOnlineException%3CBR%20%2F%3E%2B%20FullyQualifiedErrorId%20%3A%20Microsoft.Online.Administration.Automation.RequiredPropertyNotSetException%2CMicrosoft.Online.Administration.Automation.SetUser%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EScript%20Used%3A%3C%2FP%3E%3CP%3EConnect-MsolService%20-Credential%3CBR%20%2F%3E%24csvPath%20%3D%24(Get-ChildItem%20-path%20'C%3A%5CExport'%20%7C%20%3F%20%7B%20(%24_.Extension%20-eq%20'.csv')%20-and%20(%24_.Name%20-match%20'Export_Contact_Info')%7D%20%7C%20Sort-Object%20CreationTime%20-Descending)%5B0%5D%3CBR%20%2F%3E%24csvData%20%3D%20Get-Content%20-Path%20%24csvPath.fullname%20%7C%20Select-Object%20-Skip%202%20%7C%20Out-String%20%7C%20ConvertFrom-Csv%3CBR%20%2F%3E%24csvData%20%7C%20foreach%20%7BSet-MsolUser%20-UserPrincipalName%20%24_.'Email%20Address'%20-MobilePhone%20%24_.'Mobile%20Formated'%20-PhoneNumber%20%24_.'DID%20formated'%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBest%20Regards%2C%3C%2FP%3E%3CP%3EMark%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-826542%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EIdentity%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-827143%22%20slang%3D%22en-US%22%3ERe%3A%20Automated%20PS1%20script%20to%20update%20O365%20Contacts%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-827143%22%20slang%3D%22en-US%22%3E%3CP%3EThe%20error%20message%20has%20nothing%20to%20do%20with%20authentication%2C%20most%20likely%20the%20identifier%20that%20you%20are%20using%20(%22Email%20address%22)%20doesn't%20return%20a%20match.%20Anyway%2C%20if%20you%20want%20to%20automate%20the%20process%2C%20either%20use%20an%20account%20that%20has%20MFA%20disabled%2C%20or%20configure%20bypass%20(known%20location%2Ftrusted%20IPs%20is%20my%20preferred%20solution%20for%20bypassing%20MFA).%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-828386%22%20slang%3D%22en-US%22%3ERe%3A%20Automated%20PS1%20script%20to%20update%20O365%20Contacts%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-828386%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F58%22%20target%3D%22_blank%22%3E%40Vasil%20Michev%3C%2FA%3E%26nbsp%3BHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWould%20you%20share%20me%20more%20about%20the%20trusted%2Fknown%20IP%20location%20to%20bypass%20MFA%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBest%20Regards%2C%3C%2FP%3E%3CP%3EMark%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-828579%22%20slang%3D%22en-US%22%3ERe%3A%20Automated%20PS1%20script%20to%20update%20O365%20Contacts%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-828579%22%20slang%3D%22en-US%22%3E%3CP%3ERead%20here%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Freports-monitoring%2Fquickstart-configure-named-locations%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Freports-monitoring%2Fquickstart-configure-named-locations%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-838164%22%20slang%3D%22en-US%22%3ERe%3A%20Automated%20PS1%20script%20to%20update%20O365%20Contacts%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-838164%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F58%22%20target%3D%22_blank%22%3E%40Vasil%20Michev%3C%2FA%3E%26nbsp%3BHi%2C%3C%2FP%3E%3CP%3ESeems%20it%20is%20disabled%20to%20us.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F130193i3E37B8084F49CF83%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22Screenshot_88.png%22%20title%3D%22Screenshot_88.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBest%20Regards%2C%3C%2FP%3E%3CP%3EMark%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-839193%22%20slang%3D%22en-US%22%3ERe%3A%20Automated%20PS1%20script%20to%20update%20O365%20Contacts%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-839193%22%20slang%3D%22en-US%22%3E%3CP%3EI%20guess%20you%20don't%20have%20a%20qualifying%20SKU.%20I'm%20drawing%20a%20blank%20on%20this%2C%20and%20the%20documentation%20doesn't%20specify%20which%20licenses%20you%20need.%20But%20as%20its%20unavailable%20for%20you%20I'd%20wager%20it's%20at%20least%20Azure%20AD%20P1.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-854214%22%20slang%3D%22en-US%22%3ERe%3A%20Automated%20PS1%20script%20to%20update%20O365%20Contacts%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-854214%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F58%22%20target%3D%22_blank%22%3E%40Vasil%20Michev%3C%2FA%3E%26nbsp%3BHi%2C%20Do%20you%20have%20other%20in%20mind%20aside%20from%20acquiring%20Azure%20AD%20P1%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Contributor

Hi Tech Community,

 

I was tasked to automate the update of O365 Contacts from the automated export of details from one of our systems which holds the mobile,telephone #. The script below used is working but I don't know how to automate it as my GA account has MFA activate hence cannot use app password on the PS1.

Just wanted to know as well which account has been updated or not as the info from the csv file contains user that is not on O365 as well.

 

Error Encountered:

Set-MsolUser : User Not Found. User: .
At line:1 char:21
+ ... | foreach {Set-MsolUser -UserPrincipalName $_.'Email Address' -Mobil ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [Set-MsolUser], MicrosoftOnlineException
+ FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.UserNotFoundException,Microsoft.Online.Administration.Automation.SetUser

Set-MsolUser : You must provide a required property: Parameter name: ObjectId
At line:1 char:21
+ ... | foreach {Set-MsolUser -UserPrincipalName $_.'Email Address' -Mobil ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [Set-MsolUser], MicrosoftOnlineException
+ FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.RequiredPropertyNotSetException,Microsoft.Online.Administration.Automation.SetUser

 

Script Used:

Connect-MsolService -Credential
$csvPath =$(Get-ChildItem -path 'C:\Export' | ? { ($_.Extension -eq '.csv') -and ($_.Name -match 'Export_Contact_Info')} | Sort-Object CreationTime -Descending)[0]
$csvData = Get-Content -Path $csvPath.fullname | Select-Object -Skip 2 | Out-String | ConvertFrom-Csv
$csvData | foreach {Set-MsolUser -UserPrincipalName $_.'Email Address' -MobilePhone $_.'Mobile Formated' -PhoneNumber $_.'DID formated'}

 

Best Regards,

Mark

6 Replies

The error message has nothing to do with authentication, most likely the identifier that you are using ("Email address") doesn't return a match. Anyway, if you want to automate the process, either use an account that has MFA disabled, or configure bypass (known location/trusted IPs is my preferred solution for bypassing MFA).

Highlighted

@Vasil Michev Hi,

 

Would you share me more about the trusted/known IP location to bypass MFA?

 

Best Regards,

Mark

Highlighted

@Vasil Michev Hi,

Seems it is disabled to us.

 

Screenshot_88.png

 

Best Regards,

Mark

Highlighted

I guess you don't have a qualifying SKU. I'm drawing a blank on this, and the documentation doesn't specify which licenses you need. But as its unavailable for you I'd wager it's at least Azure AD P1.

Highlighted

@Vasil Michev Hi, Do you have other in mind aside from acquiring Azure AD P1?