powershellconsoleazure-pipelinespowershell-ise

Exception setting "TreatControlCAsInput": "The handle is invalid. in Windows PowerShell ISE


We have a PowerShell script that contains "[Console]::TreatControlCAsInput = $true". The script is executed in a Azure DevOps Pipeline and fails with

Exception setting "TreatControlCAsInput": "The handle is invalid.
"
At line:1 char:3
+   [Console]::TreatControlCAsInput = $true
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
    + FullyQualifiedErrorId : ExceptionWhenSetting

We've narrowed down the issue by reproducing this with a single line of code outside of an Azure Pipeline. Simply open Windows PowerShell ISE and run this:

[Console]::TreatControlCAsInput = $true

If we do this in a Windows PowerShell command window, the line of code succeeds.

Does anyone know what is different about the two environments that causes this difference in behavior? We are hoping that the answer would help us understand and resolve the larger issue with the script that runs in an Azure Pipeline.

The script runs fine if executed:

  1. In Cmd.exe window using Powershell.exe -File ....
  2. In Windows PowerShell window

The script fails if executed in WIndows PowerShell ISE


Solution

  • Note:


    The Windows PowerShell ISE does not allocate a console by default, which causes attempts to call the members of the static .NET [Console] class to throw the exception you saw.

    While it allocates a (hidden) console (used behind the scenes) on demand - namely the first time you run an external console application (e.g. cmd /c ver) - after which [Console] members then can be accessed, any interactive console functionality is fundamentally unsupported in the ISE.

    Since [Console]::TreatControlCAsInput = $true is only relevant to interactive functionality, notably [Console]::ReadKey():


    Note: