5

如何编写一个批处理文件来运行检查是否通过以下方式启用了 UAC:

REG QUERY HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System\ /v EnableLUA

如果结果为:

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System EnableLUA    REG_DWORD    **0x1**)

如果结果为:

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System EnableLUA    REG_DWORD    **0x0**)

然后脚本应根据结果运行不同的命令。

4

2 回答 2

2

You could simply search for one or the other value with either FIND or FINDSTR and invoke commands depending on the result of the search. The pattern would basically be like this:

REG QUERY HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System\ /v EnableLUA | (
  FIND "0x1" >NUL
) && (
  command(s)_to_run_when_UAC_is_enabled
) || (
  command(s)_to_run_when_UAC_is_disabled
)

I.e. the REG produces an output which is passed, using the "pipe" (|) to the input of FIND. FIND looks for 0x1 in its input, and, depending on the result of the search, one of the following bracketed blocks of commands is executed.

The command && command || command is a standard mechanism that allows you to selectively run commands, a kind of replacement for IF. The first command produces a result. The command just after && runs if the result is "success", and the command just after || runs in case of a fail.

If you need to perform actions in both cases, use both && and || after the command generating the result, but if only one kind of result should be reacted to, you can leave out either && or ||.

于 2012-06-30T22:09:34.820 回答
1

查看有关EnableLUA 此处的 Microsoft 文档:

@echo off
for /f "skip=2 tokens=3" %%a in ('reg query HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA') do if "%%a" equ "0x0" (
rem When UAC is disabled
) ELSE (
rem When UAC is enabled
)
于 2020-02-22T03:21:01.977 回答