functions/New-ShadowCopy.ps1

<#
   .SYNOPSIS
      Creates a Shadow Copy Application.
   .DESCRIPTION
      Creates a Shadow Copy of an existing Application. Essbase 21c or greater is required to utilize Shadow Copies.
   .PARAMETER RestURL <string>
      The base URL for the REST API interface. Example: 'https://your.domain.com/essbase/rest/v1'
   .PARAMETER PrimaryApplication <string>
      The name of the Application to be promoted to.
   .PARAMETER ShadowApplication <string>
      The name of the Shadow Application to be promoted.
   .PARAMETER WebSession <WebRequestSession>
      A Web Request Session that contains authentication and header information for the connection.
   .PARAMETER Credential <pscredential>
      PowerShell credentials that contain authentication information for the connection.
   .PARAMETER RunInBackground <switch>
      Run as a job.
   .PARAMETER HideShadow <switch>
      Hiding a Shadow Copy prevents anyone from seeing the application.
   .PARAMETER DeleteExisting <switch>
      If used, the existing Shadow Application will be forcefully deleted before being recreated/copied from the Primary Application.
   .INPUTS
      None
   .OUTPUTS
      None
   .EXAMPLE
      New-ShadowCopy -RestURL 'https://your.domain.com/essbase/rest/v1' -PrimaryApplication 'MyApp' -ShadowApplication 'MyShadowApp' -WebSession $MyWebSession
   .EXAMPLE
      New-ShadowCopy -RestURL 'https://your.domain.com/essbase/rest/v1' -PrimaryApplication 'MyApp' -ShadowApplication 'MyShadowApp' -Credential $MyCredentials -HideShadow -DeleteExisting -RunInBackground
   .NOTES
      Created by : Shayne Scovill
   .LINK
      https://github.com/Shayne55434/RESTBase
#>

function New-ShadowCopy {
   [CmdletBinding()]
   param(
      [Parameter(Mandatory)]
      [ValidateNotNullOrEmpty()]
      [string]$RestURL,
      
      [Parameter(Mandatory)]
      [ValidateNotNullOrEmpty()]
      [string]$PrimaryApplication,
      
      [Parameter(Mandatory)]
      [ValidateNotNullOrEmpty()]
      [string]$ShadowApplication,
      
      [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='Run as a job.')]
      [switch]$RunInBackground,
      
      [Parameter(HelpMessage='Hiding a Shadow Copy prevents anyone from seeing the application, but it also prevents running the compare against it.')]
      [switch]$HideShadow,
      
      [Parameter(HelpMessage='If used, the existing Shadow Application will be forcefully deleted before being recreated/copied from the Primary Application.')]
      [switch]$DeleteExisting
   )
   
   # 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.'
   }
   
   if ($DeleteExisting.IsPresent) {
      try {
         Write-Verbose "Deleting '$ShadowApplication'."
         $null = Remove-EssbaseApplication -RestURL $RestURL @htbAuthentication -Application $ShadowApplication -Force -Confirm
      }
      catch {
         Write-Error "Could not delete '$ShadowApplication'. $($_)"
      }
   }
   
   [hashtable]$htbInvokeParameters = @{
      Method = 'Post'
      Uri = "$RestURL/applications/actions/shadowCopy"
      ContentType = 'Application/JSON'
      Body = @{
         primaryAppName= $PrimaryApplication
         shadowAppName = $ShadowApplication
         hideShadow = $HideShadow.IsPresent
         waitForOngoingUpdatesInSecs = 0
         runInBackground = $RunInBackground.IsPresent
      } | ConvertTo-Json
      Headers = @{
         accept = 'Application/JSON'
      }
   }
   $htbInvokeParameters += $htbAuthentication
   
   try {
      [object]$objJobResults = Invoke-RestMethod @htbInvokeParameters
      
      # If RunInBackground is selected, wait for the job to complete and report the final Status
      if ($RunInBackground.IsPresent) {
         Write-Debug "Job_ID: $($objJobResults.job_ID); appName: $($objJobResults.appName); dbName: $($objJobResults.dbName); jobType: $($objJobResults.jobType); statusMessage: $($objJobResults.statusMessage);"
         [string]$strProgressCharacter = '.'
         do {
            Write-Progress -CurrentOperation ("Executing job '$($objJobResults.job_ID) - $($objJobResults.jobType)'." ) ("Waiting for the job to complete$strProgressCharacter")
            [object]$objJobDetails = Get-EssbaseJob -RestURL $RestURL -JobID $objJobResults.job_ID @htbAuthentication
            Start-Sleep -Seconds 2
            $strProgressCharacter += '.'
         } while ($objJobDetails.statusMessage -eq 'In Progress')
         Write-Progress -CurrentOperation ("Executing job '$($objJobResults.job_ID) - $($objJobResults.jobType)'.") -Completed "Done waiting for the job to complete."
         
         if($objJobDetails.statusMessage -match 'Failed') {
            Write-Error "The job has failed. $($objJobDetails.jobOutputInfo.errorMessage)."
         }
         else {
            Write-Verbose "Job has completed. Status: $($objJobDetails.statusMessage)."
         }
      }
   }
   catch {
      Write-Error "Unable to create shadow copy of '$PrimaryApplication'. $($_)"
   }
}