common/specific.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 Format-PwEntry
{
    Param
    (
        [Parameter(Mandatory=$false,ValueFromPipeline=$True)]
            $PwEntry,
            [Switch]$ForcePlainText,
            $CompositeKey,
            $ConnectionInfo
    )
            $PwEntry.Strings | % {
                                    try
                                    {
                                        $val = $null
                                        $val = $PwEntry.Strings.ReadSafe($_.Key)
                                        if (-not [String]::IsNullOrEmpty($val))
                                        {
                                            if ( ($_.Key -eq "Password") )
                                            {
                                                try
                                                {
                                                    $secPassword = $val | ConvertTo-SecureString -AsPlainText -Force -ErrorAction stop
                                                    if (-not $ForcePlainText.IsPresent)
                                                    {
                                                        $val = $secPassword
                                                    }
                                                }
                                                catch [Exception]
                                                {
                                                    #$val = $_.Exception.Message
                                                    $val = $null
                                                }
                                            }
                                        }
                                        else
                                        {
                                            $val = $null
                                        }
                                    }
                                    catch [Exception]
                                    {
                                        $val = $null
                                    }


                                    try
                                    {
                                        Add-Member -InputObject $PwEntry -MemberType NoteProperty -Name $_.Key -Value $val
                                    }
                                    catch [exception]
                                    {
                                        Write-Log -message $_ -Level EXCEPTION
                                    }
                                            
                                }# $kpItem.Strings

            try
            {
                if ($PwEntry.UserName -and $secPassword)
                {
                    $psCred = New-Object System.Management.Automation.PSCredential ($PwEntry.UserName, $secPassword)
                    Add-Member -InputObject $PwEntry -MemberType NoteProperty -Name PsCredential -Value $psCred
                    Add-Member -InputObject $PwEntry -MemberType NoteProperty -Name User -Value $PwEntry.UserName
                }
                #Add-Member -InputObject $kpItem -MemberType NoteProperty -Name GroupPath -Value (Get-ParentGroup $kpItem)
                Add-Member -InputObject $PwEntry -MemberType NoteProperty -Name compositeKey -Value $compositeKey
                Add-Member -InputObject $PwEntry -MemberType NoteProperty -Name connectionInfo -Value $connectionInfo

                $getHistory = {
                    Param([bool]$ForcePlainText=$false)
                    if ($this.History)
                    {
                        if ($ForcePlainText)
                        {
                            $item = $this.History  | % {Format-PwEntry -PwEntry $_ -CompositeKey $this.CompositeKey -ConnectionInfo $this.ConnectionInfo -ForcePlainText:$true}
                        }
                        else
                        {
                            $item = $this.History  | % {Format-PwEntry -PwEntry $_ -CompositeKey $this.CompositeKey -ConnectionInfo $this.ConnectionInfo}
                        }
                    }
                    Set-StandardMembers -MyObject $item -DefaultProperties LastAccessTime,UserName,Password,Title
                    return $item
                }
                Add-Member -InputObject $PwEntry -MemberType ScriptMethod -Name getHistory -Value $getHistory

                        
            }
            catch [Exception]
            {
                Write-Log -message $_ -Level EXCEPTION
            }
            
    return $PwEntry
}