Read-Credential.ps1


<#PSScriptInfo
 
.VERSION 1.0
 
.GUID 1ebaf5ea-d613-4e2e-94a5-ad462b016d5b
 
.AUTHOR saw-friendship
 
.COMPANYNAME
 
.COPYRIGHT saw-friendship
 
.TAGS saw-friendship Credential ReadHost SecureString
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
 
.DESCRIPTION
 Read Credential in PowerShell Window
 
.EXAMPLE
 $Cred = Read-Credential
 
.EXAMPLE
 Read-Credential -OutVariable Cred
 
.EXAMPLE
 Read-Credential -Login Login -Password Password
 
  
#>
 

param(
    $Login = (Read-Host -Prompt 'Login'),
    $Password = (Read-Host -Prompt 'Password' -AsSecureString),
    [string]$OutVariable
)

if($Password -is [SecureString]){
        $PSCredential = New-Object system.Management.Automation.PSCredential($Login,$Password)
    } else {
        $Password = ConvertTo-SecureString -String $Password -AsPlainText -Force
        $PSCredential = New-Object system.Management.Automation.PSCredential($Login,$Password)
}

if(!$PSCredential){break}

if(!$OutVariable){
        $PSCredential
    } else {
        Set-Variable -Name $OutVariable -Value $PSCredential -Option AllScope -Visibility Public -Scope Global
}