Public/Register-ChromiumSecretVault.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
function Register-ChromiumSecretVault {
    <#
    .SYNOPSIS
    A convenience Cmdlet to automatically register typical installed VaultParameters
    .EXAMPLE
    Register-ChromiumSecretVault
    Detects all Chromium-based secret vaults in the preset list and registers them
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param (
        #Select one of the available preset vaults to register
        [ValidateSet(
            #TODO: Enum for this if I can figure out the powershell module nesting trickiness
            'Edge','EdgeBeta','EdgeCanary','Chrome','ChromeBeta','ChromeCanary'
        )][String]$Preset,
        #Specify to overwrite vaults already present
        [Switch]$AllowClobber,
        #Specify the path to SecretManagement.Chromium if it is not in your default module path. This is typically only used for debugging.
        [String]$ModuleName = 'SecretManagement.Chromium'
    )

    $findChromiumParams = @{}
    if ($Preset) {$findChromiumParams.Preset = $Preset}
    foreach ($profileItem in Find-Chromium @findChromiumParams) {
        $VaultName = $ProfileItem.Name
        if ($ProfileItem.Profile -ne 'Default') {
            $VaultName += '-' + $ProfileItem.Profile
        }
        
        if ($PSCmdlet.ShouldProcess($VaultName, 'Register Chromium Secret Vault')) {
            $registerVaultParams = @{
                #BUG: https://github.com/PowerShell/SecretManagement/issues/96
                #TODO: Fetch the actual profile name, not just profile folder name
                Name = $($VaultName -replace ' ','')
                ModuleName = $moduleName
                AllowClobber = $AllowClobber
                VaultParameters = @{
                    DataPath = [String]$ProfileItem.LoginDataPath
                    StatePath = [String]$ProfileItem.LocalStatePath
                }
                Description = $ProfileItem.Name,
                    'Profile',
                    $ProfileItem.Profile,
                    'at',
                    (Split-Path $ProfileItem.LoginDataPath) -join ' '
            }

            try {
                Register-SecretVault @registerVaultParams
            } catch {
                if ($PSItem.FullyQualifiedErrorId -eq 'RegisterSecretVaultInvalidVaultName,Microsoft.PowerShell.SecretManagement.RegisterSecretVaultCommand') {
                    write-warning "$VaultName is already registered. Skipping..."
                } else {throw}
            }
        }
    }

}