functions/Copy-EssbaseApplication.ps1

<#
   .SYNOPSIS
      Creates a copy of an existing application.
   .DESCRIPTION
      Creates a copy of an existing application. If the application already exists, 'DeleteExisting' must be used or the copy will fail.
   .PARAMETER RestURL <string>
      The base URL for the REST API interface. Example: 'https://your.domain.com/essbase/rest/v1'
   .PARAMETER SourceApplication <string>
      String value of the Application name to be copied.
   .PARAMETER DestinationApplication <string>
      Array String value of the Application name to be (re)created. Accepts value(s) from Pipeline.
   .PARAMETER WebSession <WebRequestSession>
      A Web Request Session that contains authentication and header information for the connection.
   .PARAMETER Credential <pscredential>
      PowerShell credential that contain authentication information for the connection.
   .PARAMETER DeleteExisting <switch>
      If used, the Destination Application will be forcefully deleted before being copied from the Source Application.
   .INPUTS
      System.String[]
   .OUTPUTS
      None
   .EXAMPLE
      Copy-EssbaseApplication -RestURL 'https://your.domain.com/essbase/rest/v1' -SourceApplication 'MyApplication' -DestinationApplication 'CopyOfMyApplication' -WebSession $MyWebSession [-DeleteExisting]
   .EXAMPLE
      'CopyOfMyApplication', 'AnotherCopyOfMyApplication' | Copy-EssbaseApplication -RestURL 'https://your.domain.com/essbase/rest/v1' -SourceApplication 'MyApplication' -Credential $MyCredentials [-DeleteExisting]
   .NOTES
      Created by : Shayne Scovill
   .LINK
      https://github.com/Shayne55434/RESTBase
#>

function Copy-EssbaseApplication {
   [CmdletBinding()]
   param(
      [Parameter(Mandatory)]
      [ValidateNotNullOrEmpty()]
      [string]$RestURL,
      
      [Parameter(Mandatory)]
      [ValidateNotNullOrEmpty()]
      [string]$SourceApplication,
      
      [Parameter(Mandatory, ValueFromPipeline)]
      [ValidateNotNullOrEmpty()]
      [string[]]$DestinationApplication,
      
      [Parameter(Mandatory, ParameterSetName='WebSession')]
      [ValidateNotNullOrEmpty()]
      [Microsoft.PowerShell.Commands.WebRequestSession]$WebSession,
      
      [Parameter(Mandatory, ParameterSetName='Credential')]
      [ValidateNotNullOrEmpty()]
      [pscredential]$Credential,
      
      [Parameter(Mandatory, ParameterSetName='Username')]
      [ValidateNotNullOrEmpty()]
      [string]$Username,
      
      [Parameter(HelpMessage='If used, the Destination Application will be forcefully deleted before being copied from the Source Application.')]
      [switch]$DeleteExisting
   )
   
   begin {
      # Decipher which authentication type is being used
      [hashtable]$htbAuthentication = @{}
      if ($null -ne $Credential) {
         $htbAuthentication.Add('Credential', $Credential)
         Write-Verbose 'Using provided credentials.'
      }
      elseif ($null -ne $WebSession)  {
         $htbAuthentication.Add('WebSession', $WebSession)
         Write-Verbose 'Using provided Web Session variable.'
      }
      else {
         [pscredential]$Credential = Get-Credential -Message 'Please enter your Essbase password' -UserName $Username
         $htbAuthentication.Add('Credential', $Credential)
         Write-Verbose 'Using provided username and password.'
      }
   }
   process {
      foreach ($destination in $DestinationApplication){
         if ($DeleteExisting.IsPresent) {
            try {
               Write-Verbose "Deleting application '$destination'."
               $null = Remove-EssbaseApplication -RestURL $RestURL @htbAuthentication -Application $destination -Force -Confirm
            }
            catch {
               Write-Error "Unable to delete $destination. $($_)"
            }
         }
         
         [hashtable]$htbInvokeParameters = @{
            Method = 'Post'
            Uri = "$RestURL/applications/actions/copy"
            Body = @{
               from = $SourceApplication
               to = $destination
            } | ConvertTo-Json
            Headers = @{
               accept = 'Application/JSON'
            }
            ContentType = 'Application/JSON'
         } + $htbAuthentication
         
         Write-Verbose "Copying '$SourceApplication' to '$destination'."
         try {
            $null = Invoke-RestMethod @htbInvokeParameters
         }
         catch {
            if ($_ -match '504 Gateway Time-out') {
               Write-Verbose "'504 Gateway Time-out'. The copy should still complete. There is just no way to monitor the progress."
            }
            else {
               Write-Error "Failed to copy '$($SourceApplication)'. $($_)"
            }
         }
      }
   }
}