certbot / certbot

Certbot is EFF's tool to obtain certs from Let's Encrypt and (optionally) auto-enable HTTPS on your server. It can also act as a client for any other CA that uses the ACME protocol.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Powershell calling certbot in console or ISE script execution causes remoteException seemingly related to virtual terminal handling

segmentlime opened this issue · comments

I posted this first at Let's Encrypt but they suggested maybe better here:

if i type:

certbot renew

at the PS console prompt (in PS ISE), then the following is written to the console:

 certbot : Saving debug log to C:\Certbot\log\letsencrypt.log
 At line:1 char:1certbot : Saving debug log to C:\Certbot\log\letsencr
 + certbot renew
 + ~~~~~~~~~~~~~
     + CategoryInfo          : NotSpecified: (Saving debug lo...letsencrypt.log:String) [], RemoteException
     + FullyQualifiedErrorId : NativeCommandError
  
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
 Processing C:\Certbot\renewal\*****.conf
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
 Certificate not yet due for renewal
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
 The following certificates are not due for renewal yet:
 
   C:\Certbot\live\*******\fullchain.pem expires on 2022-**-** (skipped)
 
 No renewals were attempted.
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

(Note: I used * character to obfuscate information related to actual domain.)

It seems like certbot ran the subcommand ok, but there was some issue and a Powershell exception that seems perhaps related to the output of the first line output:

certbot : Saving debug log to C:\Certbot\log\letsencrypt.log

From the debug log:

 2022-06-05 15:14:37,649:DEBUG:certbot._internal.main:certbot version: 1.24.0
 2022-06-05 15:14:37,649:DEBUG:certbot._internal.main:Location of certbot entry point: C:\Program Files (x86)\Certbot\bin\certbot.exe
 2022-06-05 15:14:37,649:DEBUG:certbot._internal.main:Arguments: ['--preconfigured-renewal']
 2022-06-05 15:14:37,649:DEBUG:certbot._internal.main:Discovered plugins: PluginsRegistry(PluginEntryPoint#manual,PluginEntryPoint#null,PluginEntryPoint#standalone,PluginEntryPoint#webroot)
 2022-06-05 15:14:37,649:DEBUG:certbot.compat.misc:Failed to set console mode
 Traceback (most recent call last):
   File "C:\Program Files (x86)\Certbot\pkgs\certbot\compat\misc.py", line 60, in prepare_virtual_console
     h.SetConsoleMode(h.GetConsoleMode() | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
 pywintypes.error: (6, 'GetConsoleMode', 'The handle is invalid.')
 2022-06-05 15:14:37,759:DEBUG:certbot._internal.log:Root logging level set at 30

And comparing this to debug log contents for the execution of the same command except from the Windows CMD console (wherein the output is exactly the same except that there is no PS remote exception):

2022-06-05 15:13:26,430:DEBUG:certbot._internal.main:certbot version: 1.24.0
2022-06-05 15:13:26,430:DEBUG:certbot._internal.main:Location of certbot entry point: C:\Program Files (x86)\Certbot\bin\certbot.exe
2022-06-05 15:13:26,430:DEBUG:certbot._internal.main:Arguments: ['--preconfigured-renewal']
2022-06-05 15:13:26,430:DEBUG:certbot._internal.main:Discovered plugins: PluginsRegistry(PluginEntryPoint#manual,PluginEntryPoint#null,PluginEntryPoint#standalone,PluginEntryPoint#webroot)
2022-06-05 15:13:26,524:DEBUG:certbot._internal.log:Root logging level set at 30

It seems the issue may be related to the virtual terminal processing in the misc.py code when certbot is invoked from powershell.

I get similar results when calling certbot from a PS script using the & (Powershell call operator).

When I look at a debug log from execution of the scheduled task add by the earlier "certbot run" with windows command:

Powershell.exe -NoProfile -WindowStyle Hidden -Command "certbot renew"

the first few lines of output are:

 2022-06-04 12:00:06,572:DEBUG:certbot._internal.main:certbot version: 1.24.0
 2022-06-04 12:00:06,572:DEBUG:certbot._internal.main:Location of certbot entry point: C:\Program Files (x86)\Certbot\bin\certbot.exe
 2022-06-04 12:00:06,572:DEBUG:certbot._internal.main:Arguments: ['--preconfigured-renewal']
 2022-06-04 12:00:06,572:DEBUG:certbot._internal.main:Discovered plugins: PluginsRegistry(PluginEntryPoint#manual,PluginEntryPoint#null,PluginEntryPoint#standalone,PluginEntryPoint#webroot)
 2022-06-04 12:00:06,666:DEBUG:certbot._internal.log:Root logging level set at 30

So, past the point of issue when invoked from console or execution of script in ISE.

Any ideas, on why the exception is happening and how this could be avoided?

@segmentlime came across this post (and the other, now closed), having the same issue with certbot/Windows/PS.

Using this delightfully detailed post:
https://stackoverflow.com/questions/8097354/how-do-i-capture-the-output-into-a-variable-from-an-external-process-in-powershe

...I came up with a workaround.

Format:
$var= cmd /c 'C:\Program Files\Certbot\bin\certbot.exe' --arg1 --arg2 '2>&1'

No errors or output of certbot.exe is logged to the console, yet the entirety of the output is stored in $var.

You sound like you are just as OCD about your code as I am.

Hope this helps.

We've made a lot of changes to Certbot since this issue was opened. If you still have this issue with an up-to-date version of Certbot, can you please add a comment letting us know? This helps us to better see what issues are still affecting our users. If there is no activity in the next 30 days, this issue will be automatically closed.

This issue has been closed due to lack of activity, but if you think it should be reopened, please open a new issue with a link to this one and we'll take a look.