1

I'm trying to launch the popular flashcard app Anki2 on my work laptop that runs Windows 11. Unfortunately the app is unable to connect to the ankiweb.net servers and syncronize my progress with that of my other mobile devices. I've done some digging and tried these two scripts -- only the .bat works, the powershell doesn't, why? Can you give me some pointers please, so that I can fix it?

anki-proxy.bat:

@echo off &SetLocal DisableDelayedExpansion
set /p USERNAME=<myuser>
set /p PASSWORD=<passwrd>


set "https_proxy=http://<myuser>:<passwrd>@mycompanyproxy-address:9090"
cd "C:\Users\<myuser>\AppData\Local\Programs\Anki"
start anki.exe
pause

anki2-proxy.ps1:

$proxyCreds=get-credential -username $env:username -message "Please put your Proxy password: "

Set-Variable http_proxy="http://{$proxyCreds.USERNAME}:{$proxyCreds.PASSWORD}@mycompanyproxy-address:9090"

Start "C:\Users\<myuser>\AppData\Local\Programs\Anki\anki.exe"

In the latter case I get the usual:

Error details: ⁨error sending request for url (): error trying to connect: tcp connect error

but in the former, the synchronisation works and I'm able to proceed with the learning.

  • 2
    FYI, in your batch file you should always use the `/D` option with the `CD` unless you have already specifically defined a working drive and directory. Also you can refine that location further by using a built-in environment variable, i.e. ```CD /D "%LocalAppData%\Programs\Anki"```. Although for your use above, you could probably have just done it without, and defined the working directory in the `Start` command line ```Start /D "%LocalAppData%\Programs\Anki" anki.exe```. Also, i'ts likely that you don't need to change directory anyhow, ```Start "" "%LocalAppData%\Programs\Anki\anki.exe"```! – Compo Jul 17 '23 at 20:15

1 Answers1

0

Replace your Set-Variable call with the following:

$env:https_proxy = "http://$($proxyCreds.UserName):$($proxyCreds.GetNetworkCredential().Password)@mycompanyproxy-address:9090"
  • You must define an environment variable ($env:https_proxy), which Set-Variable does not do (it creates variables that are visible to the PowerShell session only, not also to child processes).[1]

    • As an aside: Set-Variable requires the variable name and value to be passed as separate arguments, not as single argument with = as the separator (something like Set-Variable var="value" defines a variable whose verbatim name is var=value, and which has no value ($null)).

    • Only in direct variable assignments is the variable name (which must be prefixed with $) separated from its value with = (e.g., $var = 'value')

  • To embed expressions such as $proxyCreds.UserName inside "...", an expandable (double-quoted) string, you must enclose them in $(...), the subexpression operator, as shown above.

    • For a summary of the syntax and rules of PowerShell's string expansion (interpolation), see this answer.
  • You need an - inherently insecure - plain-text representation of your password, which the .Password property of a [pscredential] instance (as returned by Get-Credential) does not provide - it returns a [securestring] instance.

    • To obtain the plain-text form of the password, you must call .GetNetworkCredential() and access the .Password property on the resulting System.Net.NetworkCredential instance.

[1] Note that there is no such distinction in cmd.exe (batch files): all variables are implicitly and invariable environment variables.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • thanks for all your explanation, unfortunate even after saving the changes you suggested, I still get: `Error details: ⁨error sending request for url (): error trying to connect: tcp connect error: Impossibile stabilire la connessione.` which corresponds to a timeout error `os error 10060`. the .bat file doesn't give this problem, though it doesn't hide credentials. – WobblyWindows Jul 20 '23 at 06:39
  • @WobblyWindows, there was a typo in the environment variable name in your PowerShell code, which I didn't notice and also initially used in this answer: `http_proxy` instead of `https_proxy`. I've updated the answer to use `https_proxy`; please try again. – mklement0 Jul 20 '23 at 11:49
  • tried it, still not working. This time, however the error I'm getting is different. `Please check your internet connection. Error details: ⁨error sending request for url (): error trying to connect: dns error: Host unknown. (os error 11001)⁩` – WobblyWindows Jul 26 '23 at 18:39
  • Here's more, from the prompt: `HTTPSConnectionPool(host='ankiweb.net', port=443): Max retries exceeded with url: /shared/updates/1463041493,1771074083?v=3 (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError(': Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))) update check failed` – WobblyWindows Jul 26 '23 at 18:43
  • @WobblyWindows, if the batch file works (and no additional environment variables are required beyond the one named `https_proxy`), the implication is that the _value_ of this variable in the PowerShell script is not correctly constructed. Insert `echo "%https_proxy"` in your batch file to echo the correct value, then compare it to the value of `$env:https_proxy` in your PowerShell script. I know nothing about ankiweb.net and its proxy configuration, so I can't help there, but likely culprits are an incorrect host name, port, or credentials. – mklement0 Jul 26 '23 at 20:23
  • still can't get it to work, but thanks for all your suggestions @mklement0 – WobblyWindows Aug 08 '23 at 20:16
  • I'm sorry to hear it; you're welcome. Have you tried comparing the echoed URLs to see if they match? – mklement0 Aug 08 '23 at 20:32