Private/Import-PluginArgs.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
function Import-PluginArgs {
    [CmdletBinding()]
    param(
        [Parameter(Position=0)]
        [string[]]$DnsPlugin,
        [PSTypeName('PoshACME.PAAccount')]$Account
    )

    # make sure any account passed in is actually associated with the current server
    # or if no account was specified, that there's a current account.
    if (-not $Account) {
        if (-not ($Account = Get-PAAccount)) {
            throw "No Account parameter specified and no current account selected. Try running Set-PAAccount first."
        }
    } else {
        if ($Account.id -notin (Get-PAAccount -List).id) {
            throw "Specified account id $($Account.id) was not found in the current server's account list."
        }
    }

    # build the path to the plugin data file and import it
    $pDataFile = Join-Path (Join-Path (Get-DirFolder) $Account.id) 'plugindata.xml'
    if (Test-Path -Path $pDataFile -PathType Leaf) {
        # import the existing file
        Write-Debug "Loading saved plugin data"
        $pData = Import-CliXml $pDataFile
    } else {
        # return early with an empty hashtable because nothing was saved
        return @{}
    }

    # If one or more plugins are specified, we want to filter out args from
    # the saved set that are unrelated to thespecified ones.
    # NOTE: There is no checking for ambiguous parameter sets here.
    # We're going to assume Export-PluginArgs has already made sure there are none saved.
    if ($DnsPlugin) {
        $uniquePlugins = @($DnsPlugin) | Sort-Object -Unique
        if ($uniquePlugins.Count -gt 0) {
            # Get the list of non-common param names from the specified plugins
            $paramNames = @()
            $ignoreParams = @('RecordName','TxtValue') + [Management.Automation.PSCmdlet]::CommonParameters +
                [Management.Automation.PSCmdlet]::OptionalCommonParameters

            foreach ($plugin in $uniquePlugins) {
                # dot source the plugin file
                try {
                    . (Join-Path $PSScriptRoot "..\DnsPlugins\$plugin.ps1")
                } catch { throw }

                # check for the add command that should exist now
                $addCmdName = "Add-DnsTxt$Plugin"
                if (-not ($cmd = Get-Command $addCmdName -ErrorAction Ignore)) {
                    throw "Expected plugin command $addCmdName not found."
                }

                # add the non-common names to the list
                $paramNames += $cmd.Parameters.Keys | Where-Object {
                    ($_ -notin $ignoreParams) -and
                    ($true -notin $cmd.Parameters[$_].Attributes.ValueFromRemainingArguments)
                }
            }

            # now remove any saved params that don't match our list
            foreach ($paramName in @($pData.Keys)) {
                if ($paramName -notin $paramNames) {
                    $pData.Remove($paramName)
                }
            }
        }
    }

    return $pData
}