Advertisement
nilrem2

PowerShell Module for Securing Credentials

Apr 8th, 2020
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .Synopsis
  3.     Encrypts a password to a txt file
  4.  
  5. .Description
  6.     Encrypts a password to a txt file.
  7.     If no filePath is specified it will output to '<your default folder>\$env:COMPUTERNAME\$env:USERNAME\$description.txt'
  8.     If a filePath is specified the output will be '$filePath\$env:COMPUTERNAME\$env:USERNAME\$description.txt'
  9.  
  10. .Parameter filePath
  11.     Optional - String - filePath to store the encrypted key
  12.  
  13. .Example
  14.     Set-SecureCredentials
  15.  
  16. .Example
  17.     Set-SecureCredentials '\\Server1\lockedDownShare\'
  18.  
  19. .Link
  20.     https://mcpmag.com/articles/2017/07/20/save-and-read-sensitive-data-with-powershell.aspx
  21. #>
  22. Function Set-SecureCredentials
  23. {
  24.     Param(
  25.         [Parameter(Mandatory=$false)][string] $filePath = '\\server1\<your default folder>'
  26.     )
  27.  
  28.     #$password = Read-Host "Password`t" -AsSecureString
  29.     $description = Read-Host "Description`t"
  30.  
  31.     if ($filePath -eq '\\server1\<your default folder>')
  32.     {
  33.         $filePath += "\$env:COMPUTERNAME\$env:USERNAME\$description.xml"
  34.     }
  35.     else
  36.     {
  37.         $filePath += "\$description.xml"
  38.     }
  39.    
  40.     New-Item -Path $filePath -ItemType "file" -Force
  41.     Get-Credential | Export-Clixml -Path $filePath
  42.  
  43. }
  44.  
  45. Function Get-SecureCredentials
  46. {
  47.     Param(
  48.         [Parameter(Mandatory=$true)][string] $file
  49.     )
  50.  
  51.     $credentials = Import-Clixml -Path $file
  52.     Write-Output $credentials
  53.  
  54. }
  55.  
  56. #Declare which functions to export/make available
  57. Export-ModuleMember -Function 'Set-SecureCredentials'
  58. Export-ModuleMember -Function 'Get-SecureCredentials'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement