Code: Select all
If (-not (Get-Module MSOnline) ) {
Write-Host "No Microsoft Online PowerShell module installed" -WarningAction
Try { Install-Module MSOnline -ErrorAction Stop; Import-Module MSOnline -ErrorAction Stop }
Catch { Write-Warning "Unable to load Microsoft Office 365 module because $($Error[0])"; Exit }
}
Try {
$UserCredential = Get-Credential
Connect-MsolService -Credential $UserCredential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session -AllowClobber -DisableNameChecking
#Start Script...
Get-Mailbox Enterprise.Administrator
Get-MSOLUser Enterprise.Administrator
#End Script...
}
Catch { Write-Warning "Unable to execute PowerShell remoting session because $($Error[0])"; Exit }
Finally {If ($Session) {Remove-PSSession $Session}}
Some information I've been looking into but still confused integrating to the script above.
App Password: this is insecure since I must save it as plain text
https://support.office.com/en-us/articl ... 3953ee1183
Global Reader Role, this sounds good, but I cannot change any settings or delete some values:
https://docs.microsoft.com/en-us/azure/ ... bal-reader
So far I can only execute manual line by line of the powershell script using the steps defined in: https://docs.microsoft.com/en-us/powers ... xchange-ps
But it is impractical to use it for Scheduled script and bigger script.
Thank you.