Gitlab and powershell 7 pwsh

:hugs: Please help fill in this template with all the details to help others help you more efficiently. Use formatting blocks for code, config, logs and ensure to remove sensitive data.

Problem to solve

I am trying to run a bunch of powershell scripts in my gitlab-ci.yml. We recently updated to use PWSH Powershell 7. My powershell script works fine when I run on the pwsh command line but complains when run through gitlab. Here is the code:

- Write-Output "-------------------------------"
    - |
      [xml]$prodVars = Get-Content ${PRODVARSFILE}
      $ele=$prodVars.CatapultVariableSet.Variable | Where-Object {$_.name -eq "TitleVersion" }
      $ele.EvaluatedDefinition = "${EXTERNAL_VER}"
      $ele.'#text' = "${EXTERNAL_VER}"
      $ele=$prodVars.CatapultVariableSet.Variable | Where-Object {$_.name -eq "Product Version" }
      $ele.EvaluatedDefinition = "${PRODVER}"
      $ele.'#text' = "${PRODVER}"
      $prodVars.Save(${PRODVARSFILE})

This is the error:

InvalidOperation: 
Line |
 368 |  $ele.'#text' = "${EXTERNAL_VER}"
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | The property '#text' cannot be found on this object. Verify that the property exists and can be set.

This works totally fine when run on a pwsh shell. I have checked the config.toml. We have set

executor = "shell"
shell = "pwsh"

Versions

Gitlab Version : 16.9(self hosted)
Runner version: 16.1.0

Is that code running in a Powershell script file, or embedded into the script section of a CI/CD job?

Hi,
It is code embedded into the script , basically those are lines of code in gitlab-ci.yml.
Here is the xml I am trying to update if it helps. This worked totally fine on Powershell 5.0 and also works fine when I run it in the PS 7.0 shell, just doesn’t work when run through gitlab-ci.yml:

<?xml version="1.0" ?><CatapultVariableSet>
  <Variable Comment="Manually update for each project" Definition="Some text" EvaluatedDefinition="R2022x" Name="Product Version">R2024x</Variable>
  <Variable Comment="Manually update for each project" Definition="Some text" EvaluatedDefinition="ProductName" Name="Product Name (short)">ProductName</Variable>
  <Variable Comment="Manually update for each project" Definition="Some text" EvaluatedDefinition="CompanyName ProductName" Name="Product Name (full)">CompanyName ProductName</Variable>
  <Variable Comment="Cover page item. Manually update for each project." Definition="Some text" EvaluatedDefinition="R2022x Refresh 02" Name="TitleVersion">R2024x Refresh 01</Variable>
  <Variable Comment="Cover page item. Manually update for each project." Definition="Some text" EvaluatedDefinition="ProductName" Name="TitlePackageName">ProductName</Variable>
  <Variable Comment="Cover page item. Manually update for each project." Definition="Some text" Name="TitleDocType">User Guide</Variable>
  <Variable Comment="Collection Name, manually update for each project" EvaluatedDefinition="" Name="Collection"/>
  <Variable Comment="The installed product name to show on the copyright page." EvaluatedDefinition="ProductName" Name="Copyright Product Name">ProductName</Variable>
</CatapultVariableSet>

Thanks. I have not written Powershell in a long time, I don’t know about the specific version differences. :slight_smile:

For context on my question, I was curious if the variable resolution, and code interpretation would behave differently when inside GitLab runner shell, and script, or within a .ps1 script file.

If I understand the code correctly, the $ele object would be initialized from parsing the prodVars result set parsed from the XML file. Maybe it did not get initialized properly, and as such, .#text is missing as attribute?

Maybe add an echo/print statement after the first line, and inspect the object attributes.

$_ could be the problem, but not sure.

As a workaround, you could add the code into a .ps1 script file, and call that from the CI/CD job.