Connect-SnSOffice365.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
function Connect-SnSOffice365 { <# .SYNOPSIS Connects to Office 365 using provided parameters. .DESCRIPTION Connects to Office 365. .INPUTS None. You cannot pipe objects to Connect-SnSOffice365. .OUTPUTS None. You cannot pipe objects from Connect-SnSOffice365. .LINK https://github.com/scouternasetjanster/Office365-Scoutnet-synk .PARAMETER Configuration Configuration to use. If not specified the cached configuration will be used. #> [CmdletBinding(HelpURI = 'https://github.com/scouternasetjanster/Office365-Scoutnet-synk', PositionalBinding = $False)] param ( [Parameter(Mandatory=$False, HelpMessage="Configuration to use. If not specified the cached configuration will be used.")] $Configuration, [Parameter(HelpMessage="Enable use of managed identity when running in Azure runnbooks.")] [switch]$ManagedIdentity = $false ) if ($Configuration) { $Script:SNSConf = $Configuration } if (!$Script:SNSConf) { throw "No configuration specified. Please provide a configuration!" } if ($ManagedIdentity) { Write-SNSLog "Using managed identity for domain $($Script:SNSConf.DomainName)" } if ($ManagedIdentity) { Connect-AzAccount -Identity -ErrorAction "Stop" |out-null $token = (Get-AzAccessToken -ResourceTypeName MSGraph -ErrorAction "Stop").token Connect-MgGraph -AccessToken $token -ErrorAction "Stop" |out-null } else { Connect-MgGraph -ContextScope Process -Scopes $Script:SNSConf.RequiredScopes -ErrorAction "Stop" } try { if ($ManagedIdentity) { Connect-ExchangeOnline -ManagedIdentity -Organization $Script:SNSConf.DomainName -ShowBanner:$false -CommandName $Script:SNSConf.commandNames -Verbose:$false -ErrorAction "Stop" } else { Connect-ExchangeOnline -ShowBanner:$false -CommandName $Script:SNSConf.commandNames -Verbose:$false -ErrorAction "Stop" } } Catch { Write-SNSLog -Level "Error" "Kunde logga in på ExchangeOnline Error $_" throw } } |