Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Anchor
Review Action Script Templates
Review Action Script Templates
Review Action Script Templates

If the only RED 10 WhereScape Target Enablement Pack you have available was released prior to this version of the migration tooling then it will be missing code to deal with the RED8/9 legacy script output protocol. If this is the case then you can update you Action Processing Script Template execute script function directly. This should be performed prior to the action script generation tasks in Jobs 5 and 6 or the scripts beginning with 'c<n>_' if running the tasks manually. 


You can replace the functions in each of your PowerShell and Python templates depending on what your enablement provides. The updated functions are as follows:

Code Block
languagepowershell
titleExecuteScript
collapsetrue
function ExecuteScript($name){
    $prefix = [string]::Format("WSL_SCRIPT_{0}_",$name)
    $command = [Environment]::GetEnvironmentVariable("${prefix}COMMAND")
    if ([string]::IsNullOrEmpty($command) -or "$command" -like "*${PSCommandPath}*") {
      # Abort if there is no command or the command conatins this action script
      throw [string]::Format("No Script or SQL Block found for routine {0} of $OBJECT$",$name)
    }
    else {
      # Copy accross any routine specific env vars
      Get-ChildItem Env:${prefix}* | ForEach-Object {
        $unprefixedvar = $_.Name -replace $prefix, 'WSL_'
        [Environment]::SetEnvironmentVariable($unprefixedvar,$_.Value)
      }
      # Ensure the environment var WSL_WORKDIR is set and accessible, defaults to current run directory when not
      if ( -not ( (Test-Path env:WSL_WORKDIR) -and (Test-Path "${env:WSL_WORKDIR}") ) ) {
        [Environment]::SetEnvironmentVariable('WSL_WORKDIR',$PSScriptRoot)
      }
    }
    if ( Test-Path "$command" ) {
      # We have an SQL Block file
      $sqlBlockFile = Get-Item "$command"
      if ($sqlBlockFile.Extension -eq '.sql') {
        $block = Get-Content $sqlBlockFile -Raw
        $result = ExecuteSQLBlock $block
        if ( ($result -ne $null) -and ($result[1] -ne $null) -and ($result[1] -ne -1) ) {
          WriteAudit "Rows affected:$($result[1])"
        }
      }
      else {
        throw [string]::Format("SQL Block file had unexpected file extension: {0}",$command)
      }
    }
    else {
        $legacyOutputProtocol = $false
        if ( ('$WSL_EXP_LEGACY_SCRIPT_SUPPORT$' -eq 'TRUE') -or ('$PLEGACY_SCRIPT_SUPPORT$' -eq 'TRUE') ) {
          $scriptStdOut = & cmd.exe /c ${env:WSL_COMMAND}
          if ( $scriptStdOut -ne $null ) {
              $stdOutLines = $scriptStdOut -split '\r\n|\n'
              if ( $stdOutLines[0] -in ('1','-1','-2','-3') ) {
                  WriteAudit -message 'Parsing legacy script output protocol' -type "detail"
                  $legacyOutputProtocol = $true
                  if ($stdOutLines[0] -in ('-2','-3')) {
                      WriteAudit -message $stdOutLines[1] -statusCode "E"
                  }
                  elseif ($stdOutLines[0] -in ('-1')) {
                      WriteAudit -message $stdOutLines[1] -statusCode "W"
                  }
                  else {
                      WriteAudit -message $stdOutLines[1]
                  }
                  for ($i = 2; $i -lt $stdOutLines.Length; $i++){
                      WriteAudit -message $stdOutLines[$i]
                  }
              } 
              else {
                # We couldn't detect legacy output protocol so assume new protocol and pass stdout through
                WriteAudit 'Using new script output protocol' -type "detail"
                $stdOutLines | Write-Host
              }
          }
      }
      else {
        & cmd.exe /c ${env:WSL_COMMAND}
      }
      if ( $LASTEXITCODE -ne 0 -or ( $legacyOutputProtocol -and $stdOutLines[0] -in ('-2','-3') ) ) {
        if ( $LASTEXITCODE -ne 0 ) {
          $exitCode = $LASTEXITCODE
        }
        else {
          $exitCode = $scriptStdOut[0]
        }
        throw [string]::Format("Script execution failed with exit code: {0}. Check both audit and detail logs.",$exitCode)
      }
    }
}