Tormod Haugene Tormod Haugene - 1 year ago 77
Bash Question

Using variable in execution path

I am trying to store a folder path as a variable in PowerShell, so that I can reuse that path for various task throughout my script:

# Local storage location

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

# Execute the executable using the $localTempFolder variable

The last statement gives the following error:

.\$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

I have tried adding various combinations of parentheses, quotes etc. to the last statement, but does not seem to get it right. What am I doing wrong, and where do I go from here?

Answer Source

As PetSerAl pointed it out, you are missing the call operator &. I would also suggest you to use the Join-Path cmdlet to combine a path:

& (Join-Path $env:localTempFolder 'foo.exe')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download