3

My goal is to extract the recent messages from the message center and push it to another list that is accessible to non-admins too. I found this script in a forum, exports a csv, that is already a progress. It works in my test tenant, but not in production, because there we use multi-factor authentication.

$target = "C:\temp\messages.csv"
#Connect-MsolService 
$cred = Get-Credential
$jsonPayload = (@{userName=$cred.username;password=$cred.GetNetworkCredential().password;} | convertto-json).tostring()
$cookie = (invoke-restmethod -contenttype "application/json" -method Post -uri "https://api.admin.microsoftonline.com/shdtenantcommunications.svc/Register" -body $jsonPayload).RegistrationCookie
$jsonPayload = (@{lastCookie=$cookie;locale="en-US";preferredEventTypes=@(2)} | convertto-json).tostring()
$events = (invoke-restmethod -contenttype "application/json" -method Post -uri "https://api.admin.microsoftonline.com/shdtenantcommunications.svc/GetEvents" -body $jsonPayload)
$events.Events | export-csv -LiteralPath $target -NoTypeInformation

As far as I understood, I need to use the Connect-MsolService command to connect with MFA, but I didn't find any reference for getting the credentials out from this session to be used in the code later. Can somebody help me with this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.