Functions/Connect-MSOnlineAdminAccount.ps1
<#
.SYNOPSIS This function connects to MSOnline using admin account credentials or a MSPComplete Endpoint. .DESCRIPTION This function connects to MSOnline using admin account credentials or a MSPComplete Endpoint. It returns whether the connection and logon was successful. .EXAMPLE Connect-MSOnlineAdminAccount -Endpoint $Endpoint .EXAMPLE $Endpoint | Connect-MSOnlineAdminAccount .EXAMPLE Connect-MSOnlineAdminAccount -Username $username -Password $password #> function Connect-MSOnlineAdminAccount { [CmdletBinding(PositionalBinding=$false)] [OutputType([Bool])] param ( # The username of the MSOnline account. [Parameter(Mandatory=$true, ParameterSetName="credential")] [String]$username, # The password of the MSOnline account. [Parameter(Mandatory=$true, ParameterSetName="credential")] [SecureString]$password, # The MSPComplete Endpoint. [Parameter(Mandatory=$true, ParameterSetName="endpoint", ValueFromPipeline=$true)] $endpoint ) # If given endpoint, retrieve credential directly if ($PSCmdlet.ParameterSetName -eq "endpoint") { $msolCredential = $endpoint.Credential $username = $msolCredential.Username } # Create the MSOnline credential from the given username and password else { $msolCredential = [PSCredential]::new($username, $password) } # Logon to MSOnline try { Connect-MsolService -Credential $msolCredential -ErrorAction Stop # Logon was successful Write-Information "Connection and logon to MSOnline successful with username '$($username)'." return $true } # Logon was unsuccessful catch { Write-Error "Failed MSOnline account login with username '$($username)'.`r`n$($_.Exception.Message)" return $false } } |