Skip to content

[BUG]: Powershell@2 task ShowWarnings and non-empty VerbosePreference results in $VerbosePreference being a string in Inline Scripts #20804

Open
@Pxtl

Description

New issue checklist

Task name

Powershell@2

Task version

2.230.0

Issue Description

When both ShowWarnings is enabled, the value of $VerbosePreference is a string and not a proper enum value.

A common powershell pattern is to call a function with -Verbose:$VerbosePreference to prevent verbosity from disappearing as we cross a barrier into another script or function.

Because of this combination, the following error is produced:

System.Management.Automation.ParameterBindingException: Cannot convert 'System.String' to the 
type 'System.Management.Automation.SwitchParameter' required by parameter 'Verbose'. 

This normally works because Powershell silently converts the [Management.Automation.ActionPreference] into a [SwitchParameter] or something like it.

So, this works:

$VerbosePreference = 'continue'
"hello, world" | Set-Content c:\temp\example.txt -Verbose:$VerbosePreference

yielding the verbose message

VERBOSE: Performing the operation "Set Content" on target "Path: C:\temp\example.txt".

Unfortunately, when ShowWarnings is enabled, the inline script is run inside of an invoke-command.

And in this case the auto-conversion fails.

So,

Invoke-Command {
  $VerbosePreference = 'continue'
  "hello, world" | Set-Content c:\temp\example.txt -Verbose:$VerbosePreference
}

yields

Invoke-Command : Cannot convert 'System.String' to the type 'System.Management.Automation.SwitchParameter' required by parameter 'Verbose'.
At line:1 char:1
+ Invoke-Command {
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.InvokeCommandCommand

Environment type (Please select at least one enviroment where you face this issue)

  • Self-Hosted
  • Microsoft Hosted
  • VMSS Pool
  • Container

Azure DevOps Server type

Azure DevOps Server (Please specify exact version in the textbox below)

Azure DevOps Server Version (if applicable)

Version Azure DevOps Server 2022.0.1 (AzureDevOpsServer_20231109.3)

Operation system

Windows 10

Relevant log output

========================== Starting Command Output ===========================
##[debug]Entering Invoke-VstsTool.
##[debug] Arguments: '-NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'C:\AgentPools\Default\Agent1\_work\_temp\05ee7bb2-71dd-4c03-84a9-87147d3667b5.ps1'"'
##[debug] FileName: 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'
##[debug] WorkingDirectory: 'C:\AgentPools\Default\Agent1\_work\515\s'
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'C:\AgentPools\Default\Agent1\_work\_temp\05ee7bb2-71dd-4c03-84a9-87147d3667b5.ps1'"

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
1      8ad7873a-1cd...                 NotStarted    False                                ...                      
##[debug]STDERR: Invoke-Command : Cannot convert 'System.String' to the type 'System.Management.Automation.SwitchParameter' required by 
##[debug]STDERR: parameter 'Verbose'. 
##[debug]STDERR: At C:\AgentPools\Default\Agent1\_work\_temp\05ee7bb2-71dd-4c03-84a9-87147d3667b5.ps1:10 char:13
##[debug]STDERR: +             Invoke-Command {$ErrorActionPreference = 'stop'
##[debug]STDERR: +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
##[debug]STDERR:     + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
##[debug]STDERR:     + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.InvokeCommandCommand
##[debug]STDERR:  
##[error]Invoke-Command : Cannot convert 'System.String' to the type 'System.Management.Automation.SwitchParameter' required by 
parameter 'Verbose'. 
At C:\AgentPools\Default\Agent1\_work\_temp\05ee7bb2-71dd-4c03-84a9-87147d3667b5.ps1:10 char:13
+             Invoke-Command {$ErrorActionPreference = 'stop'
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.InvokeCommandCommand
##[debug]Processed: ##vso[task.logissue type=error]Invoke-Command : Cannot convert 'System.String' to the type 'System.Management.Automation.SwitchParameter' required by 
parameter 'Verbose'. 
At C:\AgentPools\Default\Agent1\_work\_temp\05ee7bb2-71dd-4c03-84a9-87147d3667b5.ps1:10 char:13
+             Invoke-Command {$ErrorActionPreference = 'stop'
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.InvokeCommandCommand


##[debug]Exit code: 0
##[debug]Leaving Invoke-VstsTool.

Full task logs with system.debug enabled

2025-01-23T00:02:52.3983081Z ##[debug]Evaluating condition for step: 'Proof of failure'
2025-01-23T00:02:52.3984551Z ##[debug]Evaluating: SucceededNode()
2025-01-23T00:02:52.3984745Z ##[debug]Evaluating SucceededNode:
2025-01-23T00:02:52.3985179Z ##[debug]=> True
2025-01-23T00:02:52.3985403Z ##[debug]Result: True
2025-01-23T00:02:52.3985627Z ##[section]Starting: Proof of failure
2025-01-23T00:02:52.4164315Z ==============================================================================
2025-01-23T00:02:52.4164666Z Task         : PowerShell
2025-01-23T00:02:52.4164708Z Description  : Run a PowerShell script on Linux, macOS, or Windows
2025-01-23T00:02:52.4164781Z Version      : 2.230.0
2025-01-23T00:02:52.4164824Z Author       : Microsoft Corporation
2025-01-23T00:02:52.4164873Z Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/powershell
2025-01-23T00:02:52.4164967Z ==============================================================================
2025-01-23T00:02:53.4773282Z ##[debug]VstsTaskSdk 0.11.0 commit 7ff27a3e0bdd6f7b06690ae5f5b63cb84d0f23f4
2025-01-23T00:02:53.8478595Z ##[debug]Entering C:\AgentPools\Default\Agent1\_work\_tasks\PowerShell_e213ff0f-5d5c-4791-802d-52ea3e7be1f1\2.230.0\powershell.ps1.
2025-01-23T00:02:53.9247970Z ##[debug]Loading resource strings from: C:\AgentPools\Default\Agent1\_work\_tasks\PowerShell_e213ff0f-5d5c-4791-802d-52ea3e7be1f1\2.230.0\task.json
2025-01-23T00:02:53.9549304Z ##[debug]Loaded 14 strings.
2025-01-23T00:02:53.9632605Z ##[debug]SYSTEM_CULTURE: 'en-US'
2025-01-23T00:02:53.9712583Z ##[debug]Loading resource strings from: C:\AgentPools\Default\Agent1\_work\_tasks\PowerShell_e213ff0f-5d5c-4791-802d-52ea3e7be1f1\2.230.0\Strings\resources.resjson\en-US\resources.resjson
2025-01-23T00:02:54.0329910Z ##[debug]Loaded 14 strings.
2025-01-23T00:02:54.0578816Z ##[debug]INPUT_ERRORACTIONPREFERENCE: 'stop'
2025-01-23T00:02:54.0685071Z ##[debug]INPUT_WARNINGPREFERENCE: 'default'
2025-01-23T00:02:54.0750762Z ##[debug]INPUT_INFORMATIONPREFERENCE: 'default'
2025-01-23T00:02:54.0818027Z ##[debug]INPUT_VERBOSEPREFERENCE: 'continue'
2025-01-23T00:02:54.0890507Z ##[debug]INPUT_DEBUGPREFERENCE: 'default'
2025-01-23T00:02:54.0996638Z ##[debug]INPUT_PROGRESSPREFERENCE: 'silentlyContinue'
2025-01-23T00:02:54.1048791Z ##[debug]INPUT_SHOWWARNINGS: 'true'
2025-01-23T00:02:54.1113813Z ##[debug] Converted to bool: True
2025-01-23T00:02:54.1181129Z ##[debug]INPUT_FAILONSTDERR: 'true'
2025-01-23T00:02:54.1246018Z ##[debug] Converted to bool: True
2025-01-23T00:02:54.1318003Z ##[debug]INPUT_IGNORELASTEXITCODE: 'false'
2025-01-23T00:02:54.1372051Z ##[debug] Converted to bool: False
2025-01-23T00:02:54.1449183Z ##[debug]INPUT_PWSH: 'false'
2025-01-23T00:02:54.1491195Z ##[debug] Converted to bool: False
2025-01-23T00:02:54.1555927Z ##[debug]INPUT_WORKINGDIRECTORY: 'C:\AgentPools\Default\Agent1\_work\515\s'
2025-01-23T00:02:54.1719572Z ##[debug]Asserting container path exists: 'C:\AgentPools\Default\Agent1\_work\515\s'
2025-01-23T00:02:54.1788597Z ##[debug]INPUT_TARGETTYPE: 'inline'
2025-01-23T00:02:54.1864981Z ##[debug]INPUT_SCRIPT: '"hello, world" | Set-Content .\example.txt -Verbose:$VerbosePreference
2025-01-23T00:02:54.1895603Z ##[debug]Get-Content .\example.txt'
2025-01-23T00:02:54.1960022Z ##[debug]INPUT_RUNSCRIPTINSEPARATESCOPE: 'false'
2025-01-23T00:02:54.2005556Z ##[debug] Converted to bool: False
2025-01-23T00:02:54.2061325Z Generating script.
2025-01-23T00:02:54.2245554Z ##[debug]AGENT_VERSION: '3.225.0'
2025-01-23T00:02:54.2359462Z ##[debug]AGENT_TEMPDIRECTORY: 'C:\AgentPools\Default\Agent1\_work\_temp'
2025-01-23T00:02:54.2413451Z ##[debug]Asserting container path exists: 'C:\AgentPools\Default\Agent1\_work\_temp'
2025-01-23T00:02:54.2633156Z ##[debug]Asserting leaf path exists: 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'
2025-01-23T00:02:54.2752109Z ========================== Starting Command Output ===========================
2025-01-23T00:02:54.3411794Z ##[debug]Entering Invoke-VstsTool.
2025-01-23T00:02:54.3594267Z ##[debug] Arguments: '-NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'C:\AgentPools\Default\Agent1\_work\_temp\05ee7bb2-71dd-4c03-84a9-87147d3667b5.ps1'"'
2025-01-23T00:02:54.3663262Z ##[debug] FileName: 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'
2025-01-23T00:02:54.3720363Z ##[debug] WorkingDirectory: 'C:\AgentPools\Default\Agent1\_work\515\s'
2025-01-23T00:02:54.3782065Z ##[command]"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'C:\AgentPools\Default\Agent1\_work\_temp\05ee7bb2-71dd-4c03-84a9-87147d3667b5.ps1'"
2025-01-23T00:02:54.7656128Z 
2025-01-23T00:02:54.7664717Z Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
2025-01-23T00:02:54.7676896Z --     ----            -------------   -----         -----------     --------             -------                  
2025-01-23T00:02:54.7686306Z 1      8ad7873a-1cd...                 NotStarted    False                                ...                      
2025-01-23T00:02:55.0819653Z ##[debug]STDERR: Invoke-Command : Cannot convert 'System.String' to the type 'System.Management.Automation.SwitchParameter' required by 
2025-01-23T00:02:55.0867685Z ##[debug]STDERR: parameter 'Verbose'. 
2025-01-23T00:02:55.0915586Z ##[debug]STDERR: At C:\AgentPools\Default\Agent1\_work\_temp\05ee7bb2-71dd-4c03-84a9-87147d3667b5.ps1:10 char:13
2025-01-23T00:02:55.0962155Z ##[debug]STDERR: +             Invoke-Command {$ErrorActionPreference = 'stop'
2025-01-23T00:02:55.1005616Z ##[debug]STDERR: +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2025-01-23T00:02:55.1049795Z ##[debug]STDERR:     + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
2025-01-23T00:02:55.1095565Z ##[debug]STDERR:     + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.InvokeCommandCommand
2025-01-23T00:02:55.1137484Z ##[debug]STDERR:  
2025-01-23T00:02:55.1401802Z ##[error]Invoke-Command : Cannot convert 'System.String' to the type 'System.Management.Automation.SwitchParameter' required by 
parameter 'Verbose'. 
At C:\AgentPools\Default\Agent1\_work\_temp\05ee7bb2-71dd-4c03-84a9-87147d3667b5.ps1:10 char:13
+             Invoke-Command {$ErrorActionPreference = 'stop'
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.InvokeCommandCommand
2025-01-23T00:02:55.1410530Z ##[debug]Processed: ##vso[task.logissue type=error]Invoke-Command : Cannot convert 'System.String' to the type 'System.Management.Automation.SwitchParameter' required by 
parameter 'Verbose'. 
At C:\AgentPools\Default\Agent1\_work\_temp\05ee7bb2-71dd-4c03-84a9-87147d3667b5.ps1:10 char:13
+             Invoke-Command {$ErrorActionPreference = 'stop'
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.InvokeCommandCommand
2025-01-23T00:02:55.1411121Z 
2025-01-23T00:02:55.1415356Z 
2025-01-23T00:02:55.1430803Z ##[debug]Exit code: 0
2025-01-23T00:02:55.1497220Z ##[debug]Leaving Invoke-VstsTool.
2025-01-23T00:02:55.1614470Z ##[debug]Processed: ##vso[task.complete result=Failed]Error detected
2025-01-23T00:02:55.1651779Z ##[debug]Leaving C:\AgentPools\Default\Agent1\_work\_tasks\PowerShell_e213ff0f-5d5c-4791-802d-52ea3e7be1f1\2.230.0\powershell.ps1.
2025-01-23T00:02:55.1935690Z ##[section]Finishing: Proof of failure

Repro steps

aside: since the log does not include it, our build server has PS version 5.1.17763.6640

Task config in pipeline:

          - task: Powershell@2
            displayName: Proof of failure
            inputs:
              targetType: inline
              showWarnings: true
              failOnStderr: true
              VerbosePreference: continue
              script: |
                "hello, world" | Set-Content .\example.txt -Verbose:$VerbosePreference
                Get-Content .\example.txt

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions