Last active
May 17, 2018 00:11
-
-
Save quonic/c40da5d6047b1df7deeb9f5efe39b54c to your computer and use it in GitHub Desktop.
Store creds in a secure manner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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