Public/New-Config.ps1

<#
    .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
    A new PSCustomObject with 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 {

    [CmdletBinding()]
    [OutputType([PSCustomObject])]

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingPlainTextForPassword', '', Justification = 'needed to collect')]
    param(
        [Parameter(Mandatory = $false, Position = 0)]
        [Bool]
        $Prompt = $false,

        [Parameter(Mandatory = $false, Position = 1)]
        [ValidateNotNullOrEmpty()]
        [String]
        $SfUserName,

        [Parameter(Mandatory = $false, Position = 2)]
        [ValidateNotNullOrEmpty()]
        [String]
        $SfPassword,

        [Parameter(Mandatory = $false, Position = 3)]
        [ValidateNotNullOrEmpty()]
        [String]
        $SfSecurityToken,

        [Parameter(Mandatory = $false, Position = 4)]
        [ValidateNotNullOrEmpty()]
        [String]
        $SfOauthConsumerKey,

        [Parameter(Mandatory = $false, Position = 5)]
        [ValidateNotNullOrEmpty()]
        [String]
        $SfOauthConsumerSecret,

        [Parameter(Mandatory = $false, Position = 6)]
        [ValidateNotNullOrEmpty()]
        [String]
        $IamHsdpUserName,

        [Parameter(Mandatory = $false, Position = 7)]
        [ValidateNotNullOrEmpty()]
        [String]
        $IamHsdpUserPassword,

        [Parameter(Mandatory = $false, Position = 8)]
        [Switch]
        $SandBox = $false,

        [Parameter(Mandatory = $false, Position = 9)]
        [String]
        $Path = "./config.xml",

        [Parameter(Mandatory = $false, Position = 10)]
        [String[]]
        $Scopes = @("profile", "email", "read_write"),

        [Parameter(Mandatory = $false, Position = 11)]
        [ValidateNotNullOrEmpty()]
        [String]
        $ServiceKeyFile,

        [Parameter(Mandatory = $false, Position = 12)]
        [ValidateNotNullOrEmpty()]
        [String]
        $ServiceAppId

    )
    begin {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Function started"
    }

    end {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Complete"
    }

    process {
        Write-Debug "[$($MyInvocation.MyCommand.Name)] PSBoundParameters: $($PSBoundParameters | Out-String)"
        $config = $null
        if ($Prompt) {
            $config = Read-Config
        }
        else {
            if ($PSBoundParameters.ContainsKey('ServiceKeyFile')) {
                if (-not (Test-Path $ServiceKeyFile -PathType Leaf)) {
                    throw "Service Keyfile '$($ServiceKeyFile)' does not exist."
                }
            }
            $config = (New-Object PSCustomObject -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 ($ServiceKeyFile -and $ServiceAppId) {
                $config | Add-Member -MemberType NoteProperty -Name "ServiceKeyFile" -Value $ServiceKeyFile
                $config | Add-Member -MemberType NoteProperty -Name "ServiceAppId" -Value $ServiceAppId
            }
            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
    }
}