Automate connect to exchange online (with a scheduled task) to run set-mailbox command

PhiL 1 Reputation point
2021-04-23T14:33:34.2+00:00

Hello guys,
please help me automate the connection via PS-Session to exchange online.
If you have a better solution, please share it with me.

Set-ExecutionPolicy Unrestricted

Read-Host "Enter Password" -AsSecureString | ConvertFrom-SecureString | Out-File "C:\Temp\Password.txt"

$User = "serviceuser@xxxxxxxxxxxxx "

$File = "C:\Temp\Password.txt"

$MyCredential=New-Object -TypeName System.Management.Automation.PSCredential ` -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $MyCredential -Authentication Basic -AllowRedirection

Import-PSSession $Session -DisableNameChecking -AllowClobber -WarningAction SilentlyContinue

------------
The Error:---------------
Access Denied

Exchange | Exchange Server | Management
{count} votes

1 answer

Sort by: Most helpful
  1. Andy David - MVP 157.8K Reputation points MVP Volunteer Moderator
    2021-04-23T14:45:36.597+00:00

    Yea, you dont want to use that :)

    Use the certificate based EXo Module and add it the Exchange Admin Role Group
    https://learn.microsoft.com/en-us/powershell/exchange/app-only-auth-powershell-v2?view=exchange-ps

    https://www.youtube.com/watch?v=wIxOW6nZ5OU


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.