Skip to content

Instantly share code, notes, and snippets.

@quonic
Last active May 17, 2018 00:11
Show Gist options
  • Select an option

  • Save quonic/c40da5d6047b1df7deeb9f5efe39b54c to your computer and use it in GitHub Desktop.

Select an option

Save quonic/c40da5d6047b1df7deeb9f5efe39b54c to your computer and use it in GitHub Desktop.
Store creds in a secure manner
function Get-ConfigData {
Param(
$Path = "$env:TEMP\MyScriptDefaultCredStore.config.clixml"
)
if (Test-Path -Path $Path) {
# Open Config file if exists
$ConfigData = Import-Clixml $Path
}
else {
# Create Config file if does not exists
$ConfigData = @{
LastUpdateDate = (Get-Date).ToUniversalTime().AddHours(-5) # -5 for CST
Credential = (Get-Credential -Message "Username and password")
}
Export-Clixml -Path $Path -InputObject $ConfigData
}
return $ConfigData
}
function Get-MyCredentials {
Param(
[object]
$ConfigData
)
if ($ConfigData.Credential) {
# Check if creds are in config file
return $ConfigData.Credential
}
else {
# Go recreate the config file if creds aren't there.
return (Get-ConfigData).Credential
}
}
$Global:ConfigData = Get-ConfigData
$Global:Credentials = Get-MyCredentials -ConfigData $Global:ConfigData
Invoke-MyFunction -Credential $Credentials
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment