Private/Connect-SpecMGGraph.ps1

function Connect-SpecMGGraph {
    <#
    .SYNOPSIS
    Connects to MGGraph using the provided credentials.
 
    .DESCRIPTION
    The Connect-SpecMGGraph function establishes a connection to MGGraph using the specified Tenant ID and PSCredential.
 
    .PARAMETER TenantID
    The ID of the Azure tenant to connect to.
 
    .PARAMETER PSCredential
    A PSCredential object containing the client secret used for authentication.
 
    .EXAMPLE
    Connect-SpecMGGraph -TenantID "yourtenantid" -PSCredential $credential
 
    This example connects to MGGraph with the specified Tenant ID and PSCredential.
 
    .NOTES
    Return Values:
      0: Successfully connected to MGGraph.
    101: Unable to connect to MGGraph.
 
    Author: owen.heaume
    Version: 1.0
#>


    [cmdletbinding()]

    param (
        [parameter (mandatory = $true)]
        [string]$TenantID,

        [parameter (mandatory = $true)]
        [PSCredential]$PSCredential
    )

    try {
        Write-Verbose "Attempting to connect to MGGraph"
        Connect-MgGraph -TenantId $TenantID -ClientSecretCredential $psCredential -NoWelcome -ea Stop -ev x
        Write-verbose "Connected!"
        return 0
    } catch {
        Write-Error "Unable to connect to MGGraph. The error was:$x"
        return 101
    }
}