Public/Connection/Test-GraphConnection.ps1

# Module: Orbit
# Function: Testing
# Author: David Eberhardt
# Updated: 27-MAY 2023
# Status: Beta




function Test-GraphConnection {
  <#
  .SYNOPSIS
    Tests whether a valid PS Session exists for Graph
  .DESCRIPTION
    A connection established via Connect-MgGraph is parsed.
  .EXAMPLE
    Test-GraphConnection
 
    Will Return $TRUE only if a session is found.
  .INPUTS
    None
  .OUTPUTS
    Boolean
  .NOTES
    Calls Get-MgContext to determine whether a Connection exists
  .COMPONENT
    TeamsSession
  .FUNCTIONALITY
    Tests the connection to Graph
  .LINK
    https://github.com/DEberhardt/Orbit/tree/main/docs/Orbit.Authentication/Test-GraphConnection.md
  .LINK
    https://github.com/DEberhardt/Orbit/tree/main/docs/about/about_TeamsSession.md
  .LINK
    https://github.com/DEberhardt/Orbit/tree/main/docs/
  #>


  [CmdletBinding()]
  [OutputType([Boolean])]
  param() #param

  begin {
    Show-OrbitFunctionStatus -Level Live
    #Write-Verbose -Message "[BEGIN ] $($MyInvocation.MyCommand)"

  } #begin

  process {
    #Write-Verbose -Message "[PROCESS] $($MyInvocation.MyCommand)"

    try {
      $Context = (Get-MgContext -WarningAction SilentlyContinue -ErrorAction STOP)
      return $( if ( $null -eq $Context ) { $false } else { $true } )
    }
    catch {
      return $false
    }

  } #process

  end {
    #Write-Verbose -Message "[END ] $($MyInvocation.MyCommand)"
  } #end

} #Test-GraphConnection