Public/New-Config.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<#
    .SYNOPSIS
    Initialize a new org configuration

    .DESCRIPTION
    Creates a new org configuration and writes it to a file.

    .INPUTS
    None. You cannot pipe objects to New-Config.

    .OUTPUTS
    The new configuration

    .PARAMETER Prompt
    If you should be prompted for the values. Default is $false

    .PARAMETER SfUserName
    The Salesforce username to connect to the connected App

    .PARAMETER SfPassword
    The Salesforce password to connect to the connected App

    .PARAMETER SfSecurityToken
    The Salesforce security token to connect to the connected App

    .PARAMETER SfOauthConsumerKey
    The Salesforce connected app consumer key

    .PARAMETER SfOauthConsumerSecret
    The Salesforce connected app consumer secret

    .PARAMETER IamHsdpUserName
    The HSDP IAM username that has access to the CDR/PDS instance

    .PARAMETER IamHsdpUserPassword
    The HSDP IAM password for he username that has access to the CDR/PDS instance

    .PARAMETER SandBox
    Indicates if this is a sandbox instance. Defaults to $false (Note: $true has not been tested )

    .PARAMETER Path
    The path to write the persistant configuration file. Defaults to "config.xml"

    .PARAMETER Scopes
    An array of scope values for HSDP IAM. defaults to @("profile","email","read_write")

    .EXAMPLE
    PS> New-Config -SfUserName "philipsupport@ecc.onboard" -SfPassword "23457293457829345" -SfSecurityToken "lkjLkdfjLKFjdLKJflDKJFF" `
        -SfOauthConsumerKey "3MVG9JZ_r.QzrS7izXVWrETc3v0NfBxwFgCay87uv43B4MWDgRSVaTnGG.zn2dddYk8tRk2ea5memOKg4hd22" -SfOauthConsumerSecret "3487139487139487134" `
        -IamHsdpUserName "testqlikpdsupgrade@mailinator.com" -IamHsdpUserPassword "LDSKJHF*#RSKLKFJD" -Scopes @("profile","email","READ_WRITE")
#>

function New-Config {
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingPlainTextForPassword', '', Justification='needed to collect')]
    param(
        [Bool]
        $Prompt = $false,

        [ValidateNotNullOrEmpty()]
        [String]
        $SfUserName,

        [ValidateNotNullOrEmpty()]
        [String]
        $SfPassword,

        [ValidateNotNullOrEmpty()]
        [String]
        $SfSecurityToken,

        [ValidateNotNullOrEmpty()]
        [String]
        $SfOauthConsumerKey,

        [ValidateNotNullOrEmpty()]
        [String]
        $SfOauthConsumerSecret,

        [ValidateNotNullOrEmpty()]
        [String]
        $IamHsdpUserName,

        [ValidateNotNullOrEmpty()]
        [String]
        $IamHsdpUserPassword,

        [Switch]
        $SandBox = $false,

        [String]
        $Path = "./config.xml",

        [String[]]
        $Scopes = @("profile","email","read_write")
    )
    $config = $null
    if ($Prompt) {
        $config = Read-Config
    } else {
        $config = (New-Object PSObject -Property @{
            SfCredentials = New-Object System.Management.Automation.PSCredential ($SfUserName, (ConvertTo-SecureString -String $SfPassword -AsPlainText -Force))
            SfSecurityToken = $SfSecurityToken
            SfOauth = New-Object System.Management.Automation.PSCredential ($SfOauthConsumerKey, (ConvertTo-SecureString -String $SfOauthConsumerSecret -AsPlainText -Force))
            Sandbox = $Sandbox
            Scopes = $Scopes
        })
        if ($IamHsdpUserName -and $IamHsdpUserPassword) {
            $config | Add-Member -MemberType NoteProperty -Name "IamCredentials" -Value (New-Object System.Management.Automation.PSCredential ($IamHsdpUserName, (ConvertTo-SecureString -String $IamHsdpUserPassword -AsPlainText -Force)))
        }
    }
    if ($Path) {
        $config | Export-Clixml -Path $Path
    }
    Write-Output $config
}