public/New-VPASDPASetupScript.ps1

<#
.Synopsis
   GENERATE DPA INSTALLATION SCRIPT
   CREATED BY: Vadim Melamed, EMAIL: vmelamed5@gmail.com
.DESCRIPTION
   USE THIS FUNCTION GENERATE AN INSTALLATION SCRIPT THAT WILL DEPLOY A DPA CONNECTOR
.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
.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
.OUTPUTS
   If successful:
   {
        "script_url": "***",
        "bash_cmd": "***"
   }
   ---
   $false if failed
#>

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

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

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

        [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,Position=2)]
        [ValidateRange(15,240)]
        [Int]$ExpirationPeriod,

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

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

    Begin{
        $tokenval,$sessionval,$PVWA,$Header,$ISPSS,$IdentityURL,$EnableTextRecorder,$AuditTimeStamp,$NoSSL,$VaultVersion,$HideWarnings,$AuthenticatedAs,$SubDomain = Get-VPASSession -token $token
        $CommandName = $MyInvocation.MyCommand.Name
        $log = Write-VPASTextRecorder -inputval $CommandName -token $token -LogType COMMAND
    }
    Process{
        Write-Verbose "SUCCESSFULLY PARSED SUBDOMAIN VALUE"
        Write-Verbose "SUCCESSFULLY PARSED TOKEN VALUE"

        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
    }
}