Public/Set-StoredCredential.ps1

function Set-StoredCredential {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$UserName,
        
        [Parameter(Mandatory = $true)]
        [string]$PasswordEnvVar
    )

    # Retrieve assword from environment variables
    $passwordPlainText = $env:$PasswordEnvVar

    if (-not $passwordPlainText) {
        throw "Environment variable $PasswordEnvVar not found."
    }

    # Convert password to secure string
    $password = ConvertTo-SecureString $passwordPlainText -AsPlainText -Force

    # Create PSCredential object
    $cred = New-Object System.Management.Automation.PSCredential ($UserName, $password)

    return $cred
}