Remove-KPEntry.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#.ExternalHelp psKeePass.Help.xml
function Remove-KPEntry
{
    # http://technet.microsoft.com/en-us/library/hh847872.aspx
     [CmdletBinding(DefaultParameterSetName='Parameter Set 1', 
                  SupportsShouldProcess=$true, 
                  PositionalBinding=$false
                  #HelpUri = 'http://www.microsoft.com/',
                  #ConfirmImpact='Medium'
                  )]
     #[OutputType([String])]

    param(
            [Parameter(Mandatory=$false,ValueFromPipeline=$True,DontShow)]
                [KeePassLib.PwEntry]$InputObject,
            [Parameter(Mandatory=$False)]
                [Switch]$Permanent,
            [Parameter(Mandatory=$False)]
                [Switch]$PassThru
    )

    BEGIN
    {
        # http://stackoverflow.com/questions/20690347/c-sharp-delete-group-from-keepass-database
        $kpDatabase = new-object KeePassLib.PwDatabase
        $statusLogger = New-Object KeePassLib.Interfaces.NullStatusLogger
    }
    PROCESS
    {
        #Write-Output $InputObject
        if ($InputObject)
        {
            $InputObject | % {
                try
                {
                    if ($_ -is [KeePassLib.PwEntry] )
                    {
                        try
                        {
                            $PwEntry = $_
                            $connectionInfo = $PwEntry.connectionInfo
                            $compositeKey = $PwEntry.compositeKey
                            $kpDatabase.open($connectionInfo,$compositeKey,$statusLogger)

                            $pwDeletedObject = New-Object KeePassLib.PwDeletedObject
                            $pwDeletedObject.Uuid = $PwEntry.Uuid
                            $pwDeletedObject.DeletionTime = Get-Date
                            $kpDatabase.DeletedObjects.Add($pwDeletedObject)
                            $kpDatabase.MergeIn($kpDatabase,[KeePassLib.PwMergeMethod]::Synchronize,$statusLogger)

                            if ($kpDatabase.RecycleBinEnabled -and (-not $Permanent.IsPresent))
                            {
                                $pwGroupRecycleBin = $kpDatabase.RootGroup.FindGroup($kpDatabase.RecycleBinUuid,$true)
                                $pwGroupRecycleBin.AddEntry($PwEntry, $true, $true)
                            }

                            $PwEntry.Touch($true,$true)
                            $kpDatabase.Save($statusLogger)
                            $kpDatabase.Close()
                        }
                        catch [Exception]
                        {
                            Write-KPLog -message $_ -Level EXCEPTION
                            Write-Host $($_.Exception.Message) -ForegroundColor Red
                        }

                    }
                    else
                    {
                        Write-Host InputObjec is not KeePassLib.PwEntry. -ForegroundColor Red
                        break
                    }

                    if ($PassThru.IsPresent)
                    {
                        Get-KPEntry -ConnectionInfo $connectionInfo -CompositeKey $compositeKey -Uuid $PwEntry.Uuid
                    }

                }
                catch [Exception]
                {
                    Write-KPLog -message $_ -Level EXCEPTION
                    Write-Host $($_.Exception.Message) -ForegroundColor Red
                    continue
                }

            }#InputObject
        }
    }
    END
    {
        
    }
}