powershellazure-devopstfsazure-pipelinespowershell-core

Exception setting "CursorPosition": "The handle is invalid." when running pipeline task using PowerShell Core


I'm trying to migrate CI process from TFS to Azure Devops.
I set up a pipeline with several PowerShell tasks, and turned on option "Use PowerShell Core" for them:

Task options

Then I ran my pipeline, and the very first task was failed with error in heading:

Exception setting "CursorPosition": "The handle is invalid." 

After that I turned option off and re-ran pipeline. All tasks were ran without errors.

I know, that this option chooses between "regular" PowerShell and PowerShell Core (pwsh). PowerShell Core is installed on build server, and is accessible from PATH. Build server is the same Windows Server 2016 machine, which I used for TFS agents.

What does this error mean? Which handle is mentioned? What is the difference in behavior between PowerShell and PowerShell Core in this case?


Solution

  • What does this error mean?

    The error Exception setting "CursorPosition": "The handle is invalid." occurs when a script tries to set the cursor position in a console window that isn't available or supported in the current environment. This typically happens in non-interactive environments, such as the pipeline in your build server, where there is no actual console window to manipulate.

    Which handle is mentioned?

    The "handle" in this context refers to a reference to a system resource, specifically the console window. In non-interactive environments, the console window handle might be invalid or unavailable, leading to this error when attempting to set properties like CursorPosition.

    What is the difference in behavior between PowerShell and PowerShell Core in this case?

    Here's a table summarizing the main differences between PowerShell and PowerShell Core:

    Feature PowerShell ( Windows PowerShell 5.1) PowerShell Core (PowerShell 7.x )
    Platform Support Windows only Cross-platform (Windows, Linux, macOS)
    Dependencies .NET Framework .NET Core
    Launched as powershell.exe pwsh.exe on Windows and pwsh on MacOS and Linux

    You can refer to this official document Differences between Windows PowerShell 5.1 and PowerShell 7.x for more details.

    The key difference in this case may be that PowerShell Core is stricter about console manipulations in non-interactive environments, leading to errors when trying to set properties like CursorPosition. After all, it has to handle console-related commands in a way that is compatible across different operating systems.

    Windows PowerShell might handle these commands more leniently due to its tighter integration with the Windows console subsystem. It may not throw errors for certain console manipulations even if the console window is not available, or it might handle them differently.