Advertisement
jheinrichs79

KeePass - Secret Module - Initiation

Feb 5th, 2021
1,388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # ================================================================
  2. # REQUIREMENTS
  3. # ================================================================
  4. # Make sure KeePass installed.
  5. # Change the Database Path config variable in this script
  6. # Change the Key Path config variable in this script
  7. #
  8. # Written By: Jared Heinrichs
  9. # Date: Feb. 05 / 2021
  10. # ================================================================
  11.  
  12. function Start-KeePass {
  13.     [CmdletBinding()]
  14.     param (
  15.         $Path,
  16.         $UseMasterPassword,
  17.         $KeyPath
  18.     )
  19.    
  20.     begin {
  21.         #Check if needed Modules are installed on this machine
  22.         $Modules = Get-Module | Where-Object { $_.Name -eq 'SecretManagement.KeePass' -or $_.Name -eq 'Microsoft.PowerShell.SecretManagement' }
  23.         $NumMods = $Modules.count
  24.         if ($NumMods -eq 2){
  25.             write-host "All Modules are installed"
  26.         } else {
  27.             write-host "Module Requirements not met. Installing..."
  28.             Install-Module "microsoft.powershell.secretmanagement" -AllowClobber
  29.             install-module SecretManagement.KeePass -AllowClobber
  30.         }
  31.  
  32.         #Makes sure paths are correct
  33.         if (Test-Path $Path){
  34.             #Do Nothing
  35.         } else {
  36.             Write-Host "KeePass Databae doesn't exist."
  37.             $ShowFullPath = Read-Host "Please enter the correct location:"
  38.         }
  39.         if (Test-Path $KeyPath){
  40.             #Do Nothing
  41.         } else {
  42.             Write-Host "KeePass Key doesn't exist."
  43.             $KeyPath = Read-Host "Please enter the correct location:"
  44.         }
  45.  
  46.         #Check and see if this database is currently open. If open close it.
  47.         $Valts = Get-SecretVault | Where-Object { $_.Name -eq "Database"}
  48.         if ($Valts){
  49.             Write-Host "Database Running... Unregister."
  50.             Unregister-SecretVault -Name "Database"
  51.         }
  52.     }
  53.    
  54.     process {
  55.         Register-SecretVault -Name 'Database' -ModuleName 'SecretManagement.Keepass' -DefaultVault -VaultParameters @{
  56.             Path = $Path
  57.             UseMasterPassword = $UseMasterPassword
  58.             KeyPath = $KeyPath
  59.         }
  60.     }
  61.    
  62.     end {
  63.         #Display Secrets to remind user on start.
  64.         Get-SecretInfo
  65.     }
  66. }
  67. # ================================================================
  68. # Config Section
  69. # ================================================================
  70.  
  71. $Path = "\\Server\UserDrives\Administrator\KeePassDB\Database.kdbx"
  72. $UseMasterPassword = $true
  73. $KeyPath= "\\Server\UserDrives\Administrator\KeePassDB\Database.keyx"
  74.  
  75. # ================================================================
  76.  
  77. Start-KeePass -Path $Path -UseMasterPassword $UseMasterPassword -KeyPath $KeyPath
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement