在命令中使用变量(PowerShell)

Rog*_*nce 5 variables powershell quoting string-interpolation

$computer = gc env:computername

# Argument /RU '$computer'\admin isn't working.
SchTasks /create /SC Daily /tn "Image Verification" /ST 18:00:00 /TR C:\bdr\ImageVerification\ImageVerification.exe /RU '$computer'\admin /RP password
Run Code Online (Sandbox Code Playgroud)

基本上我需要在计划任务中提供计算机名称...

先感谢您!

Kei*_*ill 11

单引号字符串不会在PowerShell中扩展变量.尝试双引号字符串,例如:

"$computer\admin"
Run Code Online (Sandbox Code Playgroud)

  • 或者在这种情况下根本没有引号.无论如何,PowerShell都会自动处理引用. (3认同)
  • 是的,这就是我通常会做的。只是想指出单引号字符串和双引号字符串之间的区别。:-) (2认同)