ps/Modules/Cole.PowerShell.Developer/Public/Set-LocalUserCredential.ps1

36 lines
1.3 KiB
PowerShell
Raw Permalink Normal View History

2023-05-30 22:51:22 -07:00
function Set-LocalUserCredential {
<#
.SYNOPSIS
Set the user's credentials in local environment variables
This is mostly useful as a Profile line such as: `$creds = (Get-CredentialFromEnvironmentVariables)
This way a developer can test faster with stored credentials without having to recreate them frequently
#>
param (
[Parameter(Mandatory=$false)]
[Alias('Password')]
$InputObject,
[Parameter(Mandatory=$false)]
[Alias('Username')]
$User
)
$logLead = (Get-LogLeadName)
$secureString = $null
if (![string]::IsNullOrWhiteSpace($InputObject)) {
$secureString = (ConvertTo-SecureString $InputObject -AsPlainText -Force)
} else {
$secureString = (Read-Host -Prompt "Please provide a password" -AsSecureString)
}
if ([string]::IsNullOrWhiteSpace($User)) {
$User = (whoami)
}
Write-Host "$logLead : Storing credentials for User [$User] in EnvironmentVariables in the User store"
Set-EnvironmentVariable -Name "CREDENTIAL_USERNAME" -Store User -Value $User
Set-EnvironmentVariable -Name "CREDENTIAL_LASTCHANGED" -Store User -Value ([System.DateTime]::Now.ToString())
Set-EnvironmentVariable -Name "CREDENTIAL_PASSWORD" -Store User -Value (ConvertFrom-SecureString $secureString)
}