在执行路径中使用变量

时间:2016-10-13 08:10:22

标签: 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: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

我尝试在最后一个语句中添加括号,引号等的各种组合,但似乎没有把它弄好。我做错了什么,我从哪里开始?

1 个答案:

答案 0 :(得分:1)

作为PetSerAl pointed it out,,您错过了呼叫运营商 &。我还建议您使用Join-Path cmdlet组合路径:

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