Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 在执行路径中使用变量_Shell_Powershell - Fatal编程技术网

Shell 在执行路径中使用变量

Shell 在执行路径中使用变量,shell,powershell,Shell,Powershell,我正在尝试将文件夹路径作为变量存储在PowerShell中,以便在整个脚本中对各种任务重用该路径: # Local storage location $env:localTempFolder="TEMP" # Download some executable and store it in the local temp folder $env:downloadURL="http://somepage.com/foo.exe" Invoke-WebRequest $env:downloa

我正在尝试将文件夹路径作为变量存储在PowerShell中,以便在整个脚本中对各种任务重用该路径:

# Local storage location    
$env:localTempFolder="TEMP"

# Download some executable and store it in the local temp folder
$env:downloadURL="http://somepage.com/foo.exe"
Invoke-WebRequest $env:downloadURL -OutFile $env:localTempFolder/foo.exe

# Execute the executable using the $localTempFolder variable
./$env:localTempFolder/foo.exe
最后一条语句给出了以下错误:

.\$env:localTempFolder\.exe : The term '.\$env:pci_TEMP\foo.exe' is not
recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify
that the path is correct and try again.

At line:1 char:1
+ .\$env:pci_TEMP\foo.exe
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (.\$env:localTempFolder\foo.exe:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
我曾尝试在最后一条语句中添加各种括号、引号等组合,但似乎没有做到。我做错了什么?我该怎么办?

因为您缺少呼叫接线员
&
。我还建议您使用
Join-Path
cmdlet组合路径:

& (Join-Path $env:localTempFolder 'foo.exe')

&./$env:localTempFolder/foo.exe
@PetSerAl,这很简单!非常感谢。你可以把它作为一个答案吗?我想说,是的,而不是在这里发布相同的答案。您需要调用运算符来展开命令中的变量,否则它将被解释为常量(不可展开)字符串。谢谢您的回答!但是,我为什么要使用Join-Path cmdlet,而不仅仅是直接连接它们呢?这只是一个提示。在本例中,您可能不使用它,但通常我使用它,所以我不必担心后面的斜杠。好的,很高兴知道!谢谢你的澄清。