Kubernetes Windows Executor: Misleading `NativeCommandError` error message displayed when helper tasks output to job log

Summary

Jobs running on a Windows 2019 Kubernetes executor produce the following output in the job log when tasks are run on by the helper generate logged output:

At C:\scripts-3-150\terminate_with_json_script.ps1:7 char:1
+ powershell -File $Path
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (arti2.txt: foun...rectories  
   :String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

This is messy and suggests there has been an error whereas in fact the job is running as expected and completes normally.

Steps to reproduce

On a Kubernetes Windows 2022 node run a pipeline job which saves an artifact. The job output will be similar to the following:

Uploading artifacts...
powershell : arti2.txt: found 1 matching artifact files and directories 
At C:\scripts-3-156\terminate_with_json_script.ps1:7 char:1
+ powershell -File $Path
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (arti2.txt: foun...rectories  
   :String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
Uploading artifacts as "archive" to coordinator... 201 Created  
id=156 responseStatus=201 Created token=DegZ--71

Example pipeline configuration:

stages:
  - build

build-job: 
  stage: build
  image: mcr.microsoft.com/windows/servercore:ltsc2022
  tags: 
    - windows-2022
  script:
    - dir > arti2.txt
  artifacts:
    paths:
      - arti2.txt

Actual behavior

Job output includes misleading and unnecessary output:

At C:\scripts-3-156\terminate_with_json_script.ps1:7 char:1
+ powershell -File $Path
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (arti2.txt: foun...rectories  
   :String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Expected behavior

Output should just be:

Uploading artifacts...
powershell : arti2.txt: found 1 matching artifact files and directories 
Uploading artifacts as "archive" to coordinator... 201 Created  
id=156 responseStatus=201 Created token=DegZ--71

Relevant logs and/or screenshots

job log
Add the job log

Environment description

config.toml contents
Add your configuration here

Used GitLab Runner version

Running with gitlab-runner 15.11.0 (436955cb)
  on gitlab-runner-win-2022-54d7bf8459-t9wbz 9ggQ4861, system ID: r_Z2u8uYKGbEPf
  feature flags: FF_USE_POWERSHELL_PATH_RESOLVER:true

Possible fixes