[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[系统相关] 批处理运行中怎样检测自身所处的系统是否为安全模式、Windows RE等等?

本帖最后由 huyou96 于 2016-10-24 16:36 编辑

批处理运行中怎样检测自身所处的系统环境是否为正常模式、安全模式、Windows PE、Windows RE?

批处理脚本在执行过程中,怎样检测它自身所处的操作系统环境是符合以下几种中的哪一种呢?
正常模式
安全模式
Windows RE或Windows PE


正常模式就是指一般正常运行状态下的电脑所处的操作系统模式。
其他的模式呢,定义和概念就无需我多费舌解释了吧?
有不明白的朋友,可以去参看如下微软或维基百科的定义
(带提一下:发现微软中国竟然没有关于简体中文方面的安全模式的定义或解释,当然微软的英文有,微软中国大咧咧不认真的耻辱啊……)

安全模式 (维基中文)
Safe Mode
【注】该英文微软链接在论坛反复提交了几次都不行,只好把网址留下:
https://technet.microsoft.com/en-us/library/cc976736.aspx

什么是 Windows PE?
What is Windows PE? (英文)

Windows 恢复环境 (Windows RE)
什么是 Windows RE?
Windows Recovery Environment (Windows RE) 英文
What is Windows RE? (英文)


我的意思大致是像下面这样:

---

rem 批处理自身先要找到能检测自身所处的系统环境
rem 是否为正常模式、安全模式、Windows PE、Windows RE的方法
rem 并把得到的所处系统模式的参数值赋给变量

set os_running_mode=所处系统模式的参数值

if %os_running_mode% equ "正常模式" (
echo 我正跑在正常模式呢
)

if %os_running_mode% equ "安全模式" (
echo 我正跑在安全模式呢
)

if %os_running_mode% equ "Windows RE" (
echo 我正跑在Windows RE呢
)

if %os_running_mode% equ "Windows PE" (
echo 我正跑在Windows PE呢
)

---

当然上面这个只是模拟的伪代码,为了说明我的本意,
不知有没有办法能做到这个?

在安全模式下,系统将修改注册表中的一处键值,来记录当前登录的模式
HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Option
有一个键值OptionValue,类型是REG_DWORD,安全模式的键值是1

正常模式是没有没有 HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Option的,只有 HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot

所以可以通过下面的代码来判断是否在正常模式
  1. reg query HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Option /v OptionValue 2>nul ||echo.正常模式
复制代码
win RE 的OptionValue键值我不太清楚,你可以自己用强制 断电的方式获取键值,然后判断OptionValue的键值来判断是RE还是安全模式

至于pe或win7,8,10,用ver就可以判断了吧,具体版本号不清楚,你有pe系统自己查一下就好了

TOP

回复 2# flyinnet9




    谢谢!
我手头也暂无现成的Windows PE,那么可先不去管Windows PE,但最最起码希望能有方法把Windows RE与正常模式能给区别开来。
您提供的方法用于剔除安全模式没问题,
诚如您所言,在安全模式下HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SafeBoot是拥有Option子键及其下的OptionValue键值(等于0x1,Reg_DWord类型值);
而在Windows RE与正常模式下,都是不存在Option这个键的。


如此,您提供的方案只是部分解决了我提出的问题,
它可以将安全模式跟Windows RE与正常模式区别开来,
但却无助于将Windows RE跟正常模式二者分别出来,因为二者在该注册表位置的表现完全相同。



附上我测试的结果。

在仨模式下分别运行这个指令
reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SafeBoot
结果分别如下

---

Safe mode

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SafeBoot
    AlternateShell    REG_SZ    cmd.exe

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SafeBoot\Minimal
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SafeBoot\Network
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SafeBoot\Option

---

Windows RE

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SafeBoot
    AlternateShell    REG_SZ    cmd.exe

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SafeBoot\Minimal
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SafeBoot\Network

---

Normal mode

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SafeBoot
    AlternateShell    REG_SZ    cmd.exe

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SafeBoot\Minimal
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SafeBoot\Network

---

在仨模式下分别运行这个指令
reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SafeBoot\Option /v OptionValue
结果分别如下

---

Safe mode

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SafeBoot\Option
    OptionValue    REG_DWORD    0x1

---

Windows RE

ERROR: The system was unable to find the specified registry key or value.

---

Normal mode

ERROR: The system was unable to find the specified registry key or value.

---


期望各位大虾能进一步提供区分开Windows RE与正常模式的有效方法,以将该问题完全解决,谢谢!

TOP

本帖最后由 huyou96 于 2016-10-25 12:51 编辑



Hi, everyone. 嗨,大家好。

暂时先这样啦:

CheckOS.bat
  1. @echo off
  2. powershell -help > nul 1>nul 2>nul
  3. if not "%ErrorLevel%" equ "0" (
  4. echo Hi, I'm running in Windows RE or Windows PE
  5. echo 嗨,我正跑在Windows RE或Windows PE呢
  6. pause
  7. exit 0
  8. )
  9. :::::: reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SafeBoot\Option > nul 1>nul 2>nul ::::::
  10. reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SafeBoot\Option /v OptionValue /t Reg_DWord > nul 1>nul 2>nul
  11. if "%ErrorLevel%" equ "0" (
  12. echo Hi, I'm running in safe mode
  13. echo 嗨,我正跑在安全模式呢
  14. pause
  15. exit 0
  16. ) else (
  17. echo Hi, I'm running in normal mode
  18. echo 嗨,我正跑在正常模式呢
  19. pause
  20. exit 0
  21. )
复制代码
再谢flyinnet9大虾的指点!
也只能暂时这样咯……
以后有空再来完善它吧……

TOP

返回列表