Calling Console.WindowWidth during a Calamari script throws an exception

As one of my deployment steps, I package up a SimpleMigrations project to migrate the database. It runs fine locally and when executed from Powershell on the Tentacle (running Windows Server 2016, dotnetcore 1.1), but when included as a deployment step, it fails.

If I run the script directly from Calamari with calamari --run-script --script deploy.ps1 I get the same exception.

The deploy.ps1 is:

dotnet <redacted>.dll up

The exception is:

System.IO.IOException: The handle is invalid
   at System.ConsolePal.GetBufferInfo(Boolean throwOnNoConsole, Boolean& succeeded)
   at System.ConsolePal.get_WindowWidth()
   at SimpleMigrations.Console.ConsoleLogger.WriteHeader(String message)
   at SimpleMigrations.SimpleMigrator`2.MigrateTo(Int64 newVersion)
   at <redacted>(List`1 args)
   at <redacted>(String[] args)

I’ve isolated it down to the call to Console.WindowWidth.

I’m not sure if this is a problem with dotnet CLI, Powershell, Calamari or something else.

Hi Matt,

Thanks for getting in touch! I suspect you are running in to the problem described here: http://stackoverflow.com/questions/20492535/system-io-ioexception-when-calling-system-console-windowwidth
When we execute a script in Octopus we use PsExec and we redirect output (so that we can capture it to return the logs), and that means you don’t have a console window for things like Console.WindowWidth.

There’s a suggestion in the last answer in there to try launching your command via cmd. That is, your deploy.ps1 script would contain something like:

cmd /c dotnet redacted.dll up

Certainly worth a try.

Regards,
Mark