【问题标题】:How can I use relative paths when using the Window's "runas" command使用 Window 的“runas”命令时如何使用相对路径
【发布时间】:2016-08-17 15:28:50
【问题描述】:

我正在尝试在我构建的文件夹系统中使用 runas 命令,以便以不同的用户身份运行多个脚本。当我输入要运行的脚本的整个文件路径时,例如:

runas /user:jmc C:\foo\script.bat

它工作正常,但是,当我尝试使用关系文件路径时,它失败了,说它找不到脚本。所以这些命令会导致它失败。

cd C:\foo
C:\Windows\System32\runas.exe /user:jmc .\script.bat

有谁知道如何在 runas 命令中使用关系文件路径? 提前谢谢!

【问题讨论】:

  • 你需要cd INSIDE runas 环境。当你运行 runas 时,新的 shell 基本上与运行 runas 的原始 shell 完全断开,新的 shell 将在新用户的主目录中启动,不是你在其中运行 runas 的那个。
  • 太棒了,感谢您的洞察力!

标签: windows batch-file command runas


【解决方案1】:

根据http://tmdean.blogspot.com/2015/07/preserving-current-working-directory.html,您可以这样做:

runas /netonly /u:domain\user "cmd /c \"cd \"%CD%\" ^& program"

【讨论】:

    【解决方案2】:

    avs099 的回答基本上是正确的,因为它解决了手头的问题,但我想补充一点,/netonly 参数不是解决此问题所必需的。 所以,

    runas /user:username "cmd /c \"cd \"%CD%\" ^& program"
    

    这就是真正需要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-03
      • 2012-09-05
      • 2013-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-04
      相关资源
      最近更新 更多