public/Get-PSCredVault.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
function Get-PSCredVault {
    [CmdletBinding()]
    param(
        [string[]]
        $Name,
        [switch]
        $RevealPasswords,
        $VaultPath = (Join-Path $env:UserProfile ".Vault")
     )

     ## Test Vault Folder
    if (!(Test-Path $VaultPath)){
        Write-Error "Path: $VaultPath Doesn't exist. Exiting."
        return
    }

    ## Gather Creds based on filters
    $keys = Get-ChildItem $VaultPath
    if ($Name){
        $keys = $keys | Where-Object {$Name -contains $_.BaseName}
    }

    if ($keys){
        #$keys | % {}
        foreach ($key in $keys){
            $cred = Import-Clixml $key.FullName
            $props = @{
                Name = $key.BaseName
                UserName = $cred.UserName
                WhenCreated = $key.CreationTime
                WhenModified = $key.LastWriteTime
                FullPath = $key.FullName
            }
            if ($RevealPasswords){
                $props['Password'] = $cred.GetNetworkCredential().password
            }
            else {
                $props['Password'] = "********"
            }
            $obj = New-Object -TypeName psobject -Property $props
            $obj.psobject.typenames.insert(0,'PSCredvault.Cred')
            Write-Output $obj

        }
    }
    else {
        Write-Warning "No Creds found in $VaultPath"
    }

}