Public/Connect-SpecMgGraphUsingClientIDAndSecret.ps1
function Connect-SpecMgGraphUsingClientIDAndSecret { <# .SYNOPSIS Connects to MGGraph using the provided Client ID and Client Secret. .DESCRIPTION The Connect-SpecMgGraphUsingClientIDAndSecret function establishes a connection to MGGraph using the specified Tenant ID, Client ID, and Client Secret. .PARAMETER TenantID The ID of the Azure tenant to connect to. .PARAMETER AppID The Client ID (Application ID) for authentication. .PARAMETER ClientSecret The Client Secret for authentication. .EXAMPLE Connect-SpecMgGraphUsingClientIDAndSecret -TenantID "yourtenantid" -AppID "yourappid" -ClientSecret "yourclientsecret" This example connects to MGGraph with the specified Tenant ID, Client ID, and Client Secret. .NOTES Return Values: 0: Successfully connected to MGGraph. 101: Unable to connect to MGGraph. 201: Critical error - Unable to convert to a secure string. 202: Critical error - Unable to create a PSCredential object. Author: owen.heaume Version: 1.0 #> [cmdletbinding()] param ( [parameter (mandatory = $true)] [string]$TenantID, [parameter (mandatory = $true)] [string]$AppID, [parameter (mandatory = $true)] [string]$ClientSecret ) if ($NULL -eq (Get-Module microsoft.graph.authentication -ListAvailable) ) { throw "Please install Microsoft.Graph.Authentication to use this function." } else { Write-Verbose "[Microsoft.Graph.Authentication] is available on this device" } write-verbose "Calling function [Get-SpecPsCredential]" $psCredential = Get-SpecPSCredential -AppID $AppID -ClientSecret $ClientSecret switch ($psCredential) { 201 { throw "Critical error '201': Unable to convert to secure string" } 202 { throw "Critical error '202': Unable to create PSCredential object" } default { Write-Verbose "Successfully obtained PSCredential" } } Write-Verbose "Calling function [Connect-SpecMgGraph]" $result = Connect-SpecMGGraph -TenantID $TenantID -PSCredential $psCredential switch ($result) { 101 { throw "An error occurred connecting to MgGraph." } default { return 0 } } } |