Private/GetAnyBoxPSCreds.ps1

function GetAnyBoxPSCreds {
    [CmdletBinding()]
    param(
        [parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet("AmazonMusic","Audible","GooglePlay","InternetArchive","NPR","Pandora","ReelGood","Spotify","Tidal","TuneIn","YouTube","YouTubeMusic")]
        [string]$ServiceName
    )

    # See if we can get the user's $ServiceName Credentials from the Windows Credential Manager
    try {
        $StoredCreds = Get-StoredCredential -Target $ServiceName -ErrorAction Stop
        if (!$StoredCreds) {throw "Unable to find Windows Credential Manager Target called '$ServiceName'"}
    } catch {
        $CmdString = @"
if (!`$(Get-Module -ListAvailable AnyBox -ErrorAction SilentlyContinue)) {Install-Module AnyBox}
if (!`$(Get-Module AnyBox -ErrorAction SilentlyContinue)) {Import-Module AnyBox}
 
`$InputResult = Show-AnyBox -Title '$ServiceName Credentials' -Message 'Enter Your $ServiceName Credentials' -ContentAlignment 'Center' -Buttons 'Cancel','Submit' -MinWidth 400 -FontSize 20 -Prompts @(
New-AnyBoxPrompt -Message 'UserName:'
New-AnyBoxPrompt -Message 'Password:' -InputType Password
)
 
'[PSCustomObject]@{UserName = ' + "'" + `$InputResult.Input_0 + "'" + '; ' + 'Password = ' + "'" + [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR(`$InputResult.Input_1)) + "'" + '}'
"@

        try {
            $EncodedCmd = [System.Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($CmdString))
            $InvokeCmdString = powershell.exe -NoProfile -NoLogo -ExecutionPolicy Bypass -EncodedCommand $EncodedCmd
            $CredentialsPSObj = Invoke-Expression $InvokeCmdString
        } catch {
            Write-Error $_
            return
        }

        #$CredManObj = New-StoredCredential -Target $ServiceName -Username $CredentialsPSObj.UserName -Password $CredentialsPSObj.Password
        $ArgList = "-NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -Command `"New-StoredCredential -Target $ServiceName -UserName $($CredentialsPSObj.UserName) -Password $($CredentialsPSObj.Password)`""
        $null = Start-Process -FilePath powershell.exe -NoNewWindow -Wait -ArgumentList $ArgList
        $StoredCreds = Get-StoredCredential -Target $ServiceName -ErrorAction Stop
    }

    $StoredCreds

}