Automated PS1 script to update O365 Contacts

Copper 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).

@Vasil Michev Hi,

 

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

 

Best Regards,

Mark

@Vasil Michev Hi,

Seems it is disabled to us.

 

Screenshot_88.png

 

Best Regards,

Mark

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.

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