"unexpected token" in PowerShell when the executable is fully run

Just try to better understand why the second paragraph below does not work. The first element is simple, the second seems clearer, the third seems unintuitive.

# My path includes pscp so this works. pscp.exe -i $PRIVATE_KEY $file ${PROXY_USER}@${PROXY_HOST}:${PROXY_DIR} # This does not work. I get unexpected token error. Why? What does that mean? $PUTTY_PATH\pscp.exe -i $PRIVATE_KEY $file ${PROXY_USER}@${PROXY_HOST}:${PROXY_DIR} # & is required to solve the problem. & "$PUTTY_PATH\pscp.exe" -i $PRIVATE_KEY $file ${PROXY_USER}@${PROXY_HOST}:${PROXY_DIR} 
+4
source share
3 answers

This is because it is also considered a parse error:

 "foo"\pscp.exe 

While this correctly analyzes how you found:

 "$PUTTY_PATH\pscp.exe" 

This resolves a valid string, but as you already noticed, the string is not executed. You must use the & call operator to invoke the command called the next line.

+9
source

It takes \ to be part of the variable name, and complains because it is not legal. If you use this snippet as I would, putting it in a .ps1 file in your path, then I would simply go to $ putty_path if you don't want pscp.exe in your global PATH env var.

0
source

I just guess, but I have a feeling that you can abuse curly braces. Are you trying to replace the PROXY_USER environment variable instead? Typically, braces are used to start a new block of statements.

 $Env:PROXY_USER 

Alternatively, you can encapsulate this proxy information inside a string to make sure that it is treated as a single argument:

 "$Env: PROXY_USER@ $Env:PROXY_HOST:$Env:PROXY_DIR" 
0
source

Source: https://habr.com/ru/post/1302773/


All Articles