Public/Save-LPData.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
<#
.Synopsis
   Saves the encrypted LastPass data to the user's APPDATA
.DESCRIPTION
   Runs Export-CliXml to a file in the user's APPDATA directory so that future
   module loads can pull the cached data instead of contacting LastPass.
.EXAMPLE
   Save-LPData
#>

function Save-LPData
{
    Param(
        # Optionally save the vaulted passwords offline
        [Parameter()]
        [Switch]
        $SaveVault
    )

    Begin
    {
        if (!$LPVault)
        {
            $LPVault = Get-LPVault
        }
        if (!$LPKeys)
        {
            $LPKeys = Get-LPKeys
        }
        if (!$LPAccounts)
        {
            $LPAccounts = Get-LPAccounts
        }
    }
    Process
    {
        try {
            $SavedData = @{
                'Login' = $LPLogin
                'LPKeys' = $LPKeys
                'Iterations' = $LPIterations
                'Cookies' = $LPSession.Cookies.GetCookies($LPURL)
            }
            if ($SaveVault) {
                $SavedData.Vault = $LPVault
            }
            $SavedData | Export-CliXml $env:APPDATA\PSLastPass.xml
        }
        catch {
            throw "Failed to export LastPass data: $_"
        }
    }
}