Public/Connect-SpecAzAccountUsingClientIDAndSecret.ps1

function Connect-SpecAzAccountUsingClientIDAndSecret {
    <#
    .SYNOPSIS
    Connects to Azure Account using the provided Client ID and Client Secret.
 
    .DESCRIPTION
    The Connect-SpecAzAccountUsingClientIDAndSecret function establishes a connection to Azure Account 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-SpecAzAccountUsingClientIDAndSecret -TenantID "yourtenantid" -AppID "yourappid" -ClientSecret "yourclientsecret"
 
    This example connects to Azure Account with the specified Tenant ID, Client ID, and Client Secret.
 
    .NOTES
    Return Values:
      0: Successfully connected to Azure Account.
    101: Unable to connect to Azure Account.
    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
    )

    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-SpecAzAccount]"
    $result = Connect-SpecAzAccount -tenantid $tenantID -PSCredential $psCredential -ea Stop -ev x

    switch ($result) {
        101 { throw "Connect-SpecAzAccount: error 101 An error occurred trying to connect to AzAccount" }
        default { return 0 }
    }
}