public/New-VPASDPASetupScript.ps1

<#
.Synopsis
   GENERATE DPA INSTALLATION SCRIPT
   CREATED BY: Vadim Melamed, EMAIL: vpasmodule@gmail.com
.DESCRIPTION
   USE THIS FUNCTION GENERATE AN INSTALLATION SCRIPT THAT WILL DEPLOY A DPA CONNECTOR
.LINK
   https://vpasmodule.com/commands/New-VPASDPASetupScript
.PARAMETER token
   HashTable of data containing various pieces of login information (PVWA, LoginToken, HeaderType, etc).
   If -token is not passed, function will use last known hashtable generated by New-VPASToken
.PARAMETER ConnectorType
   Platform type the connector will be deployed to
   Possible values: AWS, AZURE, ON-PREMISE, GCP
.PARAMETER ConnectorOS
   Operating system type the connector will be deployed to
   Possible values: windows, darwin, linux
.PARAMETER ExpirationPeriod
   Duration the installation script will be valid for in minutes (expiration value must be between 15 and 240)
   Default value: 15 minutes
.PARAMETER ConnectorPoolID
   UniqueID of the pool the connector will be deployed to
   ID is typically a long string value like so: a1bcd234-efg5-67h8-90ij-9876k54lm321
.PARAMETER InputParameters
   HashTable of values containing the parameters required to make the API call
.EXAMPLE
   $InstallationScript = New-VPASDPASetupScript -ConnectorType ON-PREMISE -ConnectorOS windows -ConnectorPoolID "a1bcd234-efg5-67h8-90ij-9876k54lm321"
.EXAMPLE
   $InstallationScript = New-VPASDPASetupScript -ConnectorType ON-PREMISE -ConnectorOS windows -ConnectorPoolID "a1bcd234-efg5-67h8-90ij-9876k54lm321" -ExpirationPeriod 60
.EXAMPLE
   $InputParameters = @{
        ConnectorType = "AWS"|"AZURE"|"ON-PREMISE"|"GCP"
        ConnectorOS = "windows"|"darwin"|"linux"
        ExpirationPeriod = 60
        ConnectorPoolID = "1234-abcde-45678-lmnop-09876poiuy"
   }
   $InstallationScript = New-VPASDPASetupScript -InputParameters $InputParameters
.OUTPUTS
   If successful:
   {
        "script_url": "***",
        "bash_cmd": "***"
   }
   ---
   $false if failed
#>

function New-VPASDPASetupScript{
    [OutputType('System.Collections.Hashtable',[bool])]
    [CmdletBinding(DefaultParameterSetName='Set1')]
    Param(

        [Parameter(Mandatory=$true,ParameterSetName='Set1',ValueFromPipelineByPropertyName=$true,HelpMessage="Enter ConnectorType (AWS, AZURE, ON-PREMISE, GCP)")]
        [ValidateSet('AWS','AZURE','ON-PREMISE','GCP')]
        [String]$ConnectorType,

        [Parameter(Mandatory=$true,ParameterSetName='Set1',ValueFromPipelineByPropertyName=$true,HelpMessage="Enter ConnectorOS (windows, darwin, linux)")]
        [ValidateSet('windows','darwin','linux')]
        [String]$ConnectorOS,

        [Parameter(Mandatory=$false,ParameterSetName='Set1',ValueFromPipelineByPropertyName=$true)]
        [ValidateRange(15,240)]
        [Int]$ExpirationPeriod,

        [Parameter(Mandatory=$true,ParameterSetName='Set1',ValueFromPipelineByPropertyName=$true,HelpMessage="Enter ConnectorPoolID (typically a long string ie: a1bcd234-efg5-67h8-90ij-9876k54lm321)")]
        [String]$ConnectorPoolID,

        [Parameter(Mandatory=$true,ParameterSetName='InputParameters',ValueFromPipelineByPropertyName=$true,HelpMessage="Hashtable of parameters required to make API call, refer to get-help -examples for valid inputs")]
        [hashtable]$InputParameters,

        [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true)]
        [hashtable]$token
    )

    Begin{
        $tokenval,$sessionval,$PVWA,$Header,$ISPSS,$IdentityURL,$EnableTextRecorder,$AuditTimeStamp,$NoSSL,$VaultVersion,$HideWarnings,$AuthenticatedAs,$SubDomain,$EnableTroubleshooting = Get-VPASSession -token $token
        $CommandName = $MyInvocation.MyCommand.Name
        $log = Write-VPASTextRecorder -inputval $CommandName -token $token -LogType COMMAND
    }
    Process{
        try{
            if($PSCmdlet.ParameterSetName -eq "InputParameters"){
                $KeyHash = @{
                    set1 = @{
                        AcceptableKeys = @("ConnectorType","ConnectorOS","ExpirationPeriod","ConnectorPoolID")
                        MandatoryKeys = @("ConnectorType","ConnectorOS","ConnectorPoolID")
                    }
                }
                $CheckSet = Test-VPASHashtableKeysHelper -InputHash $InputParameters -KeyHash $KeyHash

                if(!$CheckSet){
                    $log = Write-VPASTextRecorder -inputval "FAILED TO FIND TARGET PARAMETER SET" -token $token -LogType MISC
                    Write-Verbose "FAILED TO FIND TARGET PARAMETER SET"
                    Write-VPASOutput -str "FAILED TO FIND TARGET PARAMETER SET...VIEW EXAMPLES BELOW:" -type E
                    $examples = Write-VPASExampleHelper -CommandName $CommandName
                    return $false
                }
                else{
                    foreach($key in $InputParameters.Keys){
                        Set-Variable -Name $key -Value $InputParameters.$key
                    }
                }
            }
        }catch{
            $log = Write-VPASTextRecorder -inputval $_ -token $token -LogType ERROR
            $log = Write-VPASTextRecorder -inputval "REST API COMMAND RETURNED: FALSE" -token $token -LogType MISC
            Write-Verbose "FAILED TO GENERATE SCRIPT"
            Write-VPASOutput -str $_ -type E
            return $false
        }

        try{
            if($SubDomain -eq "N/A"){
                Write-VPASOutput -str "SelfHosted + PriviledgeCloud Standard solutions do not support this API Call, returning false" -type E
                $log = Write-VPASTextRecorder -inputval "SelfHosted + PrivilegeCloud Standard solutions do not support this API Call, returning false" -token $token -LogType MISC
                $log = Write-VPASTextRecorder -inputval $false -token $token -LogType RETURN
                return $false
            }

            if(!$ExpirationPeriod){
                $ExpirationPeriod = 15
            }

            Write-Verbose "BUILDING PARAMETERS"
            $params = @{
                connector_type = $ConnectorType
                connector_os = $ConnectorOS
                expiration_minutes = $ExpirationPeriod
                connector_pool_id = $ConnectorPoolID
            }
            $log = Write-VPASTextRecorder -inputval $params -token $token -LogType PARAMS
            $params = $params | ConvertTo-Json

            write-verbose "MAKING API CALL TO CYBERARK"
            $uri = "https://$SubDomain.dpa.cyberark.cloud/api/connectors/setup-script"
            Write-Verbose "CONSTRUCTING URI: $uri"

            $log = Write-VPASTextRecorder -inputval $uri -token $token -LogType URI
            $log = Write-VPASTextRecorder -inputval "POST" -token $token -LogType METHOD



            if($sessionval){
                $response = Invoke-RestMethod -Headers @{"Authorization"=$Header} -Uri $uri -Method POST -Body $params -ContentType "application/json" -WebSession $sessionval
            }
            else{
                $response = Invoke-RestMethod -Headers @{"Authorization"=$Header} -Uri $uri -Method POST -Body $params -ContentType "application/json"
            }

            $log = Write-VPASTextRecorder -inputval $response -token $token -LogType RETURN
            Write-Verbose "RETURNING JSON OBJECT"
            return $response
        }catch{
            $log = Write-VPASTextRecorder -inputval $_ -token $token -LogType ERROR
            $log = Write-VPASTextRecorder -inputval "REST API COMMAND RETURNED: FALSE" -token $token -LogType MISC
            Write-Verbose "FAILED TO RETRIEVE SETUP SCRIPT"
            Write-VPASOutput -str $_ -type E
            return $false
        }
    }
    End{
        $log = Write-VPASTextRecorder -inputval $CommandName -token $token -LogType DIVIDER
    }
}