简体   繁体   English

执行脚本时使用 $env:USERNAME 或其他变量

[英]Using $env:USERNAME or other variables when executing a script

So I've been writing a script in powershell, and I'd like to make a few alterations to allow people to use it with minimal effort on their part.所以我一直在用 powershell 编写一个脚本,我想做一些改动,让人们可以用最少的努力来使用它。 Part of this includes adjusting the script so that instances of my username in paths are replaced with $env:USERNAME .其中一部分包括调整脚本,以便将路径中的用户名实例替换为$env:USERNAME

For example, C:\Users\{username}\Downloads\executable.exe例如, C:\Users\{username}\Downloads\executable.exe

would become C:\Users\$env:USERNAME\Downloads\executable.exe将变为C:\Users\$env:USERNAME\Downloads\executable.exe

and the outcome would be the same.结果是一样的。

I repeatedly get the error The term 'C:\Users\$env:USERNAME\Downloads\executable.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.我反复收到错误The term 'C:\Users\$env:USERNAME\Downloads\executable.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. The term 'C:\Users\$env:USERNAME\Downloads\executable.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.

When I calling it as part of an assigned variable, I get The term C:\Users\{username}\Downloads\executable.exe is not ... , so the path is processed correctly in this case.当我将它作为分配变量的一部分调用时,我得到The term C:\Users\{username}\Downloads\executable.exe is not ... ,因此在这种情况下正确处理了路径。

I've used variables in paths in bash before with 0 issue, so I'm unsure of where I'm going wrong.我之前在 bash 中的路径中使用了变量,但问题为 0,所以我不确定我哪里出错了。

I've tried assigning it to a variable and calling it, and assigning $env:USERNAME and including that variable in the path but neither worked.我已经尝试将它分配给一个变量并调用它,并分配$env:USERNAME并将该变量包含在路径中,但都没有奏效。

I've tried using $env:HOMEPATH too.我也尝试过使用$env:HOMEPATH

I've tried assigning to a variable as a string (with double quotes), and using Invoke-command $command and Invoke-Expression $command but no dice.我尝试将变量分配为字符串(带双引号),并使用Invoke-command $commandInvoke-Expression $command但没有骰子。

How would I execute a script or an executable with a variable in the path name?如何使用路径名中的变量执行脚本或可执行文件? Is this possible in powershell?这在powershell中可能吗?


Mathias R. Jensen's answer fixed my issue, my code now works. Mathias R. Jensen 的回答解决了我的问题,我的代码现在可以工作了。

#Constructed path as advised
$TestPath = Join-Path $env:USERPROFILE \Downloads\executable.exe

Get-date | Out-File -FilePath $env:USERPROFILE\test\testfile.txt -Append
#Invoked executable
& $TestPath | Out-File -FilePath $env:USERPROFILE\test\testfile.txt -Append

For constructing paths relative to the users profile folder, use $env:USERPROFILE , and split the operation into 2 separate steps:要构建相对于用户配置文件文件夹的路径,请使用$env:USERPROFILE ,并将操作拆分为 2 个单独的步骤:

  • Construct path to executable构建可执行文件的路径
  • Invoke with the & call operator:使用&调用运算符调用:
# Construct path
$exePath = Join-Path $env:USERPROFILE 'Downloads\executable.exe'

# Invoke executable
& $exePath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM