1
votes

I am encountering a new issue with the PowerShell Task in an Azure DevOps pipeline. This task last ran without issues on 4/27 and the current issue occurred on the next build 5/5. During this time no changes were made to the pipeline.

The issue appears to be originating from the task itself as it is occurring in the PowerShell Task in both our Build and our Deploy Pipelines. Additional possibly relevant information, the Pipeline is run on a Self-Hosted Agent and triggered from Azure DevOps Server 2019.

Here is the script that we are running with the task:

Write-Host "##vso[build.addbuildtag]Deploy" 

Here is the YAML:

steps:

- task: PowerShell@1

  displayName: 'PowerShell Script'

  inputs:

    scriptType: inlineScript

    inlineScript: 'Write-Host "##vso[build.addbuildtag]Deploy" '

    failOnStandardError: false

  continueOnError: true

  condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))

Here is the resulting Error:

##[debug]Evaluating condition for step: 'PowerShell Script'
##[debug]Evaluating: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
##[debug]Evaluating and:
##[debug]..Evaluating succeeded:
##[debug]..=> True
##[debug]..Evaluating eq:
##[debug]....Evaluating indexer:
##[debug]......Evaluating variables:
##[debug]......=> Object
##[debug]......Evaluating String:
##[debug]......=> 'Build.SourceBranch'
##[debug]....=> 'refs/heads/master'
##[debug]....Evaluating String:
##[debug]....=> 'refs/heads/master'
##[debug]..=> True
##[debug]=> True
##[debug]Expanded: and(True, eq('refs/heads/master', 'refs/heads/master'))
##[debug]Result: True
##[section]Starting: PowerShell Script
==============================================================================
Task         : PowerShell
Description  : Run a PowerShell script
Version      : 1.2.3
Author       : Microsoft Corporation
Help         : [More Information](https://go.microsoft.com/fwlink/?LinkID=613736)
==============================================================================
##[debug]Working directory: 'D:\DevOps_LegacyAgent\_work\1\s'
##[debug]C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -Sta -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command "try { $null = [System.Security.Cryptography.ProtectedData] } catch { Write-Verbose 'Adding assemly: System.Security' ; Add-Type -AssemblyName 'System.Security' ; $null = [System.Security.Cryptography.ProtectedData] ; $Error.Clear() } ; Invoke-Expression -Command ([System.Text.Encoding]::UTF8.GetString([System.Security.Cryptography.ProtectedData]::Unprotect([System.Convert]::FromBase64String('AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAArARvThIFVke20pF03HsjGQAAAAACAAAAAAADZgAAwAAAABAAAACogvvpDqu82/uBaGAzfMLmAAAAAASAAACgAAAAEAAAAMmlYwLrG+pX6cojbe/JSEdgAAAAnSbu0YpvQYBRJpACag4t6eYLxZ38eidUUEp2oqJQg8E5SHMJTn66QrqAz0Ks0C8hGYESnkfwEIv9GdI6/stDqX0YXpR7Xvus30Hl8D3QJPfRFeugZ0es+1SdZkofp4TuFAAAANt4kYo56dv5uoAgUYZvimImJ7Td'), [System.Convert]::FromBase64String('ixGPc4PRq3A7+cJJCfL97g=='), [System.Security.Cryptography.DataProtectionScope]::CurrentUser))) ; if (!(Test-Path -LiteralPath variable:\LastExitCode)) { Write-Verbose 'Last exit code is not set.' } else { Write-Verbose ('$LastExitCode: {0}' -f $LastExitCode) ; exit $LastExitCode }"
##[command]. 'C:\Users\SAC00_DevOps_GADBld\AppData\Local\Temp\d564f4d3-aa11-40b4-a1f4-f6ef44aaa197.ps1' 
##[error]Exception calling "Unprotect" with "3" argument(s): "The operation completed successfully.
"
At line:1 char:237
+ ... Clear() } ; Invoke-Expression -Command ([System.Text.Encoding]::UTF8. ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : CryptographicException
 

##[error]Process completed with exit code 0 and had 1 error(s) written to the error stream.
##[debug]System.Exception: Process completed with exit code 0 and had 1 error(s) written to the error stream.
   at Microsoft.VisualStudio.Services.Agent.Worker.Handlers.PowerShellExeHandler.RunAsync()
   at Microsoft.VisualStudio.Services.Agent.Worker.TaskRunner.RunAsync()
   at Microsoft.VisualStudio.Services.Agent.Worker.StepsRunner.RunStepAsync(IStep step, CancellationToken jobCancellationToken)
##[section]Finishing: PowerShell Script
2

2 Answers

0
votes

Try the following:

Additionally, add Add-Type -AssemblyName System.Security

steps:

- task: PowerShell@1

  displayName: 'PowerShell Script'

  inputs:

    scriptType: inlineScript

    inlineScript: |

     Add-Type -AssemblyName System.Security
     
     Write-Host "##vso[build.addbuildtag]Deploy" 

    failOnStandardError: false

  continueOnError: true

  condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
0
votes

I was able to resolve this by switching to v2 of the Powershell task (PowerShell@2)