Private/Get-SpecPSCredential.ps1

function Get-SpecPSCredential {
    <#
    .SYNOPSIS
    Creates a PSCredential object using the provided Application ID (AppID) and Client Secret.
 
    .DESCRIPTION
    The Get-SpecPSCredential function generates a PSCredential object using the specified Application ID (AppID) and Client Secret.
 
    .PARAMETER AppID
    The Application ID (Client ID) used for authentication.
 
    .PARAMETER ClientSecret
    The Client Secret used for authentication.
 
    .EXAMPLE
    $credential = Get-SpecPSCredential -AppID "yourappid" -ClientSecret "yourclientsecret"
 
    This example creates a PSCredential object with the specified AppID and Client Secret.
 
    .NOTES
    Return Values:
    PSCredential object: A PSCredential object containing the provided AppID and Client Secret.
    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]$AppID,

        [parameter (mandatory = $true)]
        [string]$ClientSecret
    )

    try {
        $password = ConvertTo-SecureString $ClientSecret -AsPlainText -Force -ea stop -ev x
    } catch {
        Write-Error "[Get-SpecPSCredential] Unable to convert to secure string. The error was $x"
        return 201
    }

    try {
        $psCredential = New-Object System.Management.Automation.PSCredential($AppID, $password)
    } catch {
        Write-Error "[Get-SpecPSCredential] Unable to create the PSCredential object. The error was $x"
        return 202
    }

    return $pscredential
}