Search code examples
azure-devopsazure-pipelinespowershell-core

Azure Pipeline: Pass System.Debug to a pwsh switch parameter


How do I parse a variable which is either the string "True" or non-existent into a boolean?

I'm trying to pass the value of the predefined azure pipeline variable "system.debug" into a pwsh script as the argument to a switch parameter. I've tried both of these approaches:

-isDebug:([boolean]'$(System.Debug)')
-isDebug:$$(System.Debug)

Both of these approaches work when the "Enable system diagnostics" option is checked. It gets a value of "True" which I then must parse. However, if not checked the System.Debug variable doesn't exist at all, not false or null. This makes my parse fail below:

$isDebug = $false
if($$(System.Debug)){
  $isDebug = $$(System.Debug)
}

Error:

+   $isDebug = $$(System.Debug)
+                ~
Unexpected token '(' in expression or statement.
+ CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken

Solution

  • I was able to check if the environment variable exists with get-environmentVariable and if it does use the value, otherwise don't pass the switch parameter at all.

          if([Environment]::GetEnvironmentVariable('SYSTEM_DEBUG') -ne $null){
            write-host "SYSTEM_DEBUG: $(system.debug)"
            ./myscript.ps1 -isDebug:([boolean]$($env:SYSTEM_DEBUG))
          }
          else {
            write-host "SYSTEM_DEBUG was not set"
            ./myscript.ps1
          }