Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ================================================================
- # REQUIREMENTS
- # ================================================================
- # Make sure KeePass installed.
- # Change the Database Path config variable in this script
- # Change the Key Path config variable in this script
- #
- # Written By: Jared Heinrichs
- # Date: Feb. 05 / 2021
- # ================================================================
- function Start-KeePass {
- [CmdletBinding()]
- param (
- $Path,
- $UseMasterPassword,
- $KeyPath
- )
- begin {
- #Check if needed Modules are installed on this machine
- $Modules = Get-Module | Where-Object { $_.Name -eq 'SecretManagement.KeePass' -or $_.Name -eq 'Microsoft.PowerShell.SecretManagement' }
- $NumMods = $Modules.count
- if ($NumMods -eq 2){
- write-host "All Modules are installed"
- } else {
- write-host "Module Requirements not met. Installing..."
- Install-Module "microsoft.powershell.secretmanagement" -AllowClobber
- install-module SecretManagement.KeePass -AllowClobber
- }
- #Makes sure paths are correct
- if (Test-Path $Path){
- #Do Nothing
- } else {
- Write-Host "KeePass Databae doesn't exist."
- $ShowFullPath = Read-Host "Please enter the correct location:"
- }
- if (Test-Path $KeyPath){
- #Do Nothing
- } else {
- Write-Host "KeePass Key doesn't exist."
- $KeyPath = Read-Host "Please enter the correct location:"
- }
- #Check and see if this database is currently open. If open close it.
- $Valts = Get-SecretVault | Where-Object { $_.Name -eq "Database"}
- if ($Valts){
- Write-Host "Database Running... Unregister."
- Unregister-SecretVault -Name "Database"
- }
- }
- process {
- Register-SecretVault -Name 'Database' -ModuleName 'SecretManagement.Keepass' -DefaultVault -VaultParameters @{
- Path = $Path
- UseMasterPassword = $UseMasterPassword
- KeyPath = $KeyPath
- }
- }
- end {
- #Display Secrets to remind user on start.
- Get-SecretInfo
- }
- }
- # ================================================================
- # Config Section
- # ================================================================
- $Path = "\\Server\UserDrives\Administrator\KeePassDB\Database.kdbx"
- $UseMasterPassword = $true
- $KeyPath= "\\Server\UserDrives\Administrator\KeePassDB\Database.keyx"
- # ================================================================
- Start-KeePass -Path $Path -UseMasterPassword $UseMasterPassword -KeyPath $KeyPath
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement