PowerShell job fails: __uc_return : The term '__uc_return' is not recognized.
search cancel

PowerShell job fails: __uc_return : The term '__uc_return' is not recognized.

book

Article ID: 215690

calendar_today

Updated On:

Products

CA Continuous Delivery Automation - Automation Engine CA Automic One Automation

Issue/Introduction

PowerShell job fails with the following error message:

Exception found: System.Management.Automation.RuntimeException
Exception text: You cannot call a method on a null-valued expression.

__uc_return : The term '__uc_return' is not recognized as the name of a cmdlet, function, script 
file, or operable program. Check the spelling of the name, or if a path was included, verify that 
the path is correct and try again.
At C:\Automic\Automation.Platform\Agents\windows\temp\JAADYVCM.TXT.ps1:74 char:3
+   __uc_return( 9999 )
+   ~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (__uc_return:String) [], ParentContainsErrorRecordE 
   xception
    + FullyQualifiedErrorId : CommandNotFoundException

Environment

Release : 12.3

Component : AUTOMATION ENGINE

OS: Windows Server 2012, 2016, 2019

PowerShell version: 5.1.14393.4350

Cause

Configuration.

Resolution

The HEADER.WINDOWS in client 0 was utilizing the include HEADER.WINDOWS.USER.HEAD which was not configured for PowerShell 5.

Non working HEADER.WINDOWS.USER.HEAD:

:if &uc_win_typ = "EXTCOMPROC"
:set &UC_JOBMD   = get_var(UC_EX_JOB_MD)
:set &UC_IP_PORT = get_var(UC_EX_IP_PORT)
:set &UC_IP_ADR  = get_var(UC_EX_IP_ADDR)
:set &UC_REALNR  = SYS_ACT_RESTART_ME_NR
:set &UC_MANDANT = SYS_ACT_CLIENT

$bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField"
$objectRef = $host.GetType().GetField("externalHostRef", $bindingFlags).GetValue($host)

$bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetProperty"
$consoleHost = $objectRef.GetType().GetProperty("Value", $bindingFlags).GetValue($objectRef, @())

[void] $consoleHost.GetType().GetProperty("IsStandardOutputRedirected", $bindingFlags).GetValue($consoleHost, @())
$bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField"
$field = $consoleHost.GetType().GetField("standardOutputWriter", $bindingFlags)
$field.SetValue($consoleHost, [Console]::Out)
$field2 = $consoleHost.GetType().GetField("standardErrorWriter", $bindingFlags)
$field2.SetValue($consoleHost, [Console]::Out)

!Common function for returning from UC4 script
!Function calls the Messanger and generate the UC4 return code
function __uc_return( $retcode )
{
  __call_messanger "E" $retcode
  if( $LastExitCode -ne 0 )
  {
    Write "ATTENTION - UC4:JOBMESSENGER failed"
    Write "JOB ended with RETCODE $retcode"
    Write "JOBMSG ended with RETCODE $retcode"
    Write "JOB will abort with RETCODE from JOBMSG step"

  }
  exit $retcode
}

!Function calls the messanger executable and generate the output
function __call_messanger( $typ, $retcode )
{
:SET &UC_PS_JOBMD_QUOTE_CHK = STR_FIND(&UC_JOBMD,'"')
:IF &UC_PS_JOBMD_QUOTE_CHK <> 0
  $exe    = &UC_JOBMD
:ELSE
  $exe    = "&UC_JOBMD"
:ENDIF
  $jobid  = "JNR=&UC_REALNR"
  $client = "MNR=&UC_MANDANT"
  $port   = "PNR=&UC_IP_PORT"
  $ip     = "IPA=&UC_IP_ADR"
  $type   = "TYP=$typ"
  $rc     = "RET=$retcode"

  if( $typ -eq "E" )
  {
    $txt = "TXT=        Job ended with retcode"
    & $exe $jobid $client $port $ip $type $txt $rc
  }
  else
  {
    $txt = "TXT=        Job started"
    & $exe $jobid $client $port $ip $type $rc $txt
  }
  if( $LastExitCode -ne 0 )
  {
    __uc_return( $LastExitCode )
  }
}

__call_messanger "S" "0"

!Generic PowerShell exception handling,
!catch all unhandle exceptions and ends UC4 script with return code 9999
trap [Exception]
{
  Write ""
  Write $("Exception found: " + $_.Exception.GetType().FullName);
  Write $("Exception text: " + $_.Exception.Message);
  Write ""
  __uc_return( 9999 )
}

:endif

Correct HEADER.WINDOWS.USER.HEAD:

:if &uc_win_typ = "EXTCOMPROC"
:set &UC_JOBMD   = get_var(UC_EX_JOB_MD)
:set &UC_IP_PORT = get_var(UC_EX_IP_PORT)
:set &UC_IP_ADR  = get_var(UC_EX_IP_ADDR)

if ($PSVersionTable.PSVersion.Major -le 2)
{
  $bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField"
  $objectRef = $host.GetType().GetField("externalHostRef", $bindingFlags).GetValue($host)

  $bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetProperty"
  $consoleHost = $objectRef.GetType().GetProperty("Value", $bindingFlags).GetValue($objectRef, @())

  [void] $consoleHost.GetType().GetProperty("IsStandardOutputRedirected", $bindingFlags).GetValue($consoleHost, @())
  $bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField"
  $field = $consoleHost.GetType().GetField("standardOutputWriter", $bindingFlags)
  $field.SetValue($consoleHost, [Console]::Out)
  $field2 = $consoleHost.GetType().GetField("standardErrorWriter", $bindingFlags)
  $field2.SetValue($consoleHost, [Console]::Out)
}

!Function calls the messanger executable and generate the output
function __call_messanger( $typ, $retcode )
{
:SET &UC_PS_JOBMD_QUOTE_CHK = STR_FIND(&UC_JOBMD,'"')
:IF &UC_PS_JOBMD_QUOTE_CHK <> 0
  $exe    = &UC_JOBMD
:ELSE
  $exe    = "&UC_JOBMD"
:ENDIF
  $jobid  = "JNR=&UC_REALNR"
  $client = "MNR=&UC_MANDANT"
  $port   = "PNR=&UC_IP_PORT"
  $ip     = "IPA=&UC_IP_ADR"
  $type   = "TYP=$typ"
  $rc     = "RET=$retcode"

  if( $typ -eq "E" )
  {
    $txt = "TXT=        Job ended with retcode"
    & $exe $jobid $client $port $ip $type $txt $rc
  }
  else
  {
    $txt = "TXT=        Job started"
    & $exe $jobid $client $port $ip $type $rc $txt
  }
  if( $LastExitCode -ne 0 )
  {
    __uc_return( $LastExitCode )
  }
}

!Common function for returning from UC4 script
!Function calls the Messanger and generate the UC4 return code
function __uc_return( $retcode )
{
  __call_messanger "E" $retcode
  if( $LastExitCode -ne 0 )
  {
    Write "ATTENTION - UC4:JOBMESSENGER failed"
    Write "JOB ended with RETCODE $retcode"
    Write "JOBMSG ended with RETCODE $retcode"
    Write "JOB will abort with RETCODE from JOBMSG step"

  }
  exit $retcode
}



__call_messanger "S" "0"

!Generic PowerShell exception handling,
!catch all unhandle exceptions and ends UC4 script with return code 9999
trap [Exception]
{
  Write ""
  Write $("Exception found: " + $_.Exception.GetType().FullName);
  Write $("Exception text: " + $_.Exception.Message);
  Write ""
  __uc_return( 9999 )
}

:endif