public/Import-VPASPlatform.ps1

<#
.Synopsis
   IMPORT PLATFORM FROM CYBERARK
   CREATED BY: Vadim Melamed, EMAIL: vmelamed5@gmail.com
.DESCRIPTION
   USE THIS FUNCTION TO IMPORT A PLATFORM FROM CYBERARK
.PARAMETER NoSSL
   If the environment is not set up for SSL, API calls will be made via HTTP not HTTPS (Not Recommended!)
.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 ZipPath
   The location of the zip file containing platform details files
.EXAMPLE
   $ImportPlatformJSON = Import-VPASPlatform -ZipPath {C:\ExampleDir\ExamplePlatform.zip}
.OUTPUTS
   JSON Object (ImportPlatform) if successful
   $false if failed
#>

function Import-VPASPlatform{
    [OutputType('System.Object',[bool])]
    [CmdletBinding()]
    Param(

        [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,HelpMessage="Enter zip file containing platform details files (for example: C:\Temp\ImportPlatform.zip)",Position=0)]
        [String]$ZipPath,

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

        [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,Position=2)]
        [Switch]$NoSSL

    )

    Begin{
        $tokenval,$sessionval,$PVWA,$Header,$ISPSS,$IdentityURL = Get-VPASSession -token $token
    }
    Process{

        Write-Verbose "SUCCESSFULLY PARSED PVWA VALUE"
        Write-Verbose "SUCCESSFULLY PARSED TOKEN VALUE"
        Write-Verbose "SUCCESSFULLY PARSED ZIP PATH VALUE: $ZipPath"

        try{

            $ZipPathArray = [System.IO.File]::ReadAllBytes($ZipPath)
            Write-Verbose "CONVERTED ZIP FILE TO BYTE ARRAY"

            Write-Verbose "INITIALIZING BODY PARAMETERS"
            $params = @{
                ImportFile = $ZipPathArray
            } | ConvertTo-Json

            if($NoSSL){
                Write-Verbose "NO SSL ENABLED, USING HTTP INSTEAD OF HTTPS"
                $uri = "http://$PVWA/PasswordVault/API/Platforms/Import/"
            }
            else{
                Write-Verbose "SSL ENABLED BY DEFAULT, USING HTTPS"
                $uri = "https://$PVWA/PasswordVault/API/Platforms/Import/"
            }

            Write-Verbose "MAKING API CALL TO CYBERARK"

            if($sessionval){
                $response = Invoke-RestMethod -Headers @{"Authorization"=$Header} -Uri $uri -Body $params -Method POST -ContentType "application/json" -WebSession $sessionval
            }
            else{
                $response = Invoke-RestMethod -Headers @{"Authorization"=$Header} -Uri $uri -Body $params -Method POST -ContentType "application/json"
            }
            Write-Verbose "SUCCESSFULLY IMPORTED $ZipPath"
            Write-Verbose "RETURNING NEW PLATFORMID"
            return $response

        }catch{
            Write-Verbose "UNABLE TO IMPORT $ZipPath"
            Write-VPASOutput -str $_ -type E
            return $false
        }
    }
    End{

    }
}