【问题标题】:Reading the answer from the query "wmic"从查询“wmic”中读取答案
【发布时间】:2021-12-09 11:39:48
【问题描述】:

我无法从命令中读取返回码

    wmic computersystem where name="%computername%" rename name=aaaaa

关键是,我想将错误通知给没有管理员权限使用它的人。

如何将“ReturnValue = 5”发送到errorlevel或goto?

cmd画面

【问题讨论】:

  • 在运行 wmic 命令之前测试权限级别,可能使用NET SESSION >nul 2>&1 并在此处检查错误级别
  • 不管您最初没有注意发布与您在图像中使用的完全相同的代码,并且如果您阅读了您正在使用的命令的使用信息,您会看到它应该是CALL Rename 而不仅仅是Rename,WMIC 重命名方法,(我认为是从Vista 开始),要求它是以管理员身份运行。因此,如果您的最终用户在没有管理员权限的情况下运行它,如您所说,它将始终返回 5 并且不会成功。之后您还需要重新启动他们的 PC 以使更改生效。
  • @user2956477 非常感谢一个非常好的 ide。 :) Compo 你说得对,我会改变的。

标签: batch-file cmd command-line command-line-arguments


【解决方案1】:

如何将“ReturnValue = 5”发送到errorlevel或goto?

for /f "skip=2 tokens=2 delims==;" %%a in ('wmic computersystem where name^="%computername%" rename name^=aaaaa 2^>nul') do cmd /c exit %%a
echo %errorlevel%

GOTO:

wmic computersystem where name="AKOYA-P4" rename name=aaa |find "ReturnValue = 5;" >nul && goto :label

【讨论】:

  • 我只是结合了“tokens”和“delims”,但没有成功,我们必须在这里添加“skip”:) 它现在应该可以工作了。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-19
  • 2016-05-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多