Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. Function script:Invoke-Credentials {
  2. [CmdletBinding()]
  3. param(
  4. [Parameter(HelpMessage="Specify the username for the stored credential")]
  5. [ValidateNotNullOrEmpty()]
  6. [string]$Script_Username = "svc.username", #Username goes here
  7.  
  8. [Parameter(HelpMessage="Specify the path to store the credential")]
  9. [ValidateNotNullOrEmpty()]
  10. [string]$Script_CredFolder = "C:\Automate\",
  11.  
  12. [Parameter(HelpMessage="Specify the path to store the credential")]
  13. [switch]$Plaintext,
  14.  
  15. [Parameter(HelpMessage="Specify the encryption key so other users can decrypt the stored credential")]
  16. [string]$KeyPhrase = "dGhpc2lzMTkyYml0c2FuZGF3ZXNvbWUh"
  17. )
  18. #NOTE: This credential file can be read from any account as long as the $KeyPhrase parameter used to encrypt the password is also used to decrypt the password.
  19. $ErrorActionPreference = "Stop"
  20. $Key = [Text.Encoding]::ASCII.GetBytes($KeyPhrase) #Read ASCII characters from the KeyPhrase
  21. $ValidKeyLengths = 16, 24, 32
  22. Write-Verbose "Key length $($Key.Length) bytes"
  23.  
  24. If ($Key.Length -notin $ValidKeyLengths) {
  25. Write-Error "Key length must be 128, 192 or 256 bits."
  26. }
  27.  
  28. Else {
  29. $Script_CredPath = $Script_CredFolder + $Script_Username + ".pwd"
  30.  
  31. If ((Test-Path -Path $Script_CredFolder) -eq $False) { #Check if the $Script_CredFolder folder exists. If not, create it.
  32. New-Item -ItemType Directory -Path $Script_CredFolder | Out-Null
  33. }
  34.  
  35. If ((Test-Path -Path $Script_CredPath) -eq $False) { #If the password file does not exist, create it -- the first run of the script must be done manually to set this!
  36. Write-Verbose "Credential not found, prompting for new credential"
  37. (Get-Credential -Username $Script_Username -Message "Enter credentials for WebUtils DB (Check SecretServer)").Password | ConvertFrom-SecureString -Key $Key | Out-File $Script_CredPath
  38. }
  39. ElseIf ((Get-ChildItem $Script_CredPath | Select Length).Length -gt 0) {
  40. Write-Verbose "$Script_CredPath found; using stored credentials"
  41. }
  42. Else {
  43. Write-Error "0kb size file found at $Script_CredPath; please delete and retry"
  44. }
  45.  
  46. $Script_Password = Get-Content $Script_CredPath | ConvertTo-SecureString -Key $Key #Read the password using the encryption key
  47. $Script_Cred = New-Object -Typename System.Management.Automation.PSCredential -ArgumentList $Script_Username, $Script_Password #Create the PSCredential Object
  48.  
  49. If ($Plaintext) {
  50. $PlainTextPassword = $Script_Cred.GetNetworkCredential().Password #Use this if a plaintext password is needed
  51. Return $PlainTextPassword #Delete this when implementing in a script!
  52. }
  53. }
  54. } #End Invoke-Credentials
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement