|
2 | 2 |
|
3 | 3 | :: Script: ChMac |
4 | 4 | :: Filename: chmac.bat |
5 | | -:: Version: 1.2 |
6 | | -:: Last Modified: 29/06/2019 |
| 5 | +:: Version: 1.3 |
| 6 | +:: Last Modified: 14/06/2020 |
7 | 7 | :: Creation Date: 24/01/2010 |
8 | 8 | :: Author: wandersick |
9 | 9 | |
10 | | -:: Web: https://wandersick.blogspot.com |
| 10 | +:: Web: https://tech.wandersick.com |
11 | 11 | :: GitHub Repo: https://github.com/wandersick/chmac |
12 | | -:: Supported OS: From Windows XP to Windows 10 (limited support for 2000) |
| 12 | +:: Supported OS: From Windows XP to Windows 10 (limited support for Windows 2000) |
13 | 13 |
|
14 | 14 | :: Description: |
15 | 15 |
|
@@ -40,7 +40,7 @@ if defined debug2 echo on&set debug=1&echo :: Debugging mode 2 is ON. |
40 | 40 | :: set error code to 0 |
41 | 41 | set mErrCode=0 |
42 | 42 | set mAutoChangeInterval=None |
43 | | -set ChMacVersion=v1.2 |
| 43 | +set ChMacVersion=v1.3 |
44 | 44 |
|
45 | 45 | :: if elevated itself |
46 | 46 | :: if /i "%1" EQU "/2ndtime" set secondRun=1 |
@@ -128,22 +128,52 @@ if /i "%~1"=="/help" ( |
128 | 128 | ) |
129 | 129 |
|
130 | 130 |
|
131 | | -:: detect for admin rights (it's fine without admin rights with UAC as devcon can elevate itself -- unless UAC is disabled) |
132 | | -attrib -h "%windir%\system32" | find /i "system32" >nul 2>&1 |
133 | | -if %errorlevel% EQU 0 ( |
134 | | - set noAdmin=1 |
| 131 | +:: UAC check |
| 132 | +reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA | find /i "0x1" |
| 133 | +if %errorlevel% EQU 0 set UACenabled=1 |
| 134 | + |
| 135 | +:: detect if system has WSH disabled unsigned scripts |
| 136 | +:: if useWINSAFER = 1, the TrustPolicy below is ignored and use SRP for this option instead. So check if = 0. |
| 137 | +:: if TrustPolicy = 0, allow both signed and unsigned; if = 1, warn on unsigned; if = 2, disallow unsigned. |
| 138 | +for /f "usebackq tokens=3 skip=2" %%a in (`reg query "HKLM\SOFTWARE\Microsoft\Windows Script Host\Settings" /v UseWINSAFER 2^>nul`) do ( |
| 139 | + @if "%%a" EQU "0" ( |
| 140 | + @for /f "usebackq tokens=3 skip=2" %%i in (`reg query "HKLM\SOFTWARE\Microsoft\Windows Script Host\Settings" /v TrustPolicy 2^>nul`) do ( |
| 141 | + @if "%%i" GEQ "2" ( |
| 142 | + set noWSH=1 |
| 143 | + ) |
| 144 | + ) |
| 145 | + ) |
135 | 146 | ) |
136 | 147 |
|
137 | | -if defined ECPP set noAdminEcppMsg= Run RA from ECPP first. |
138 | | -if defined noAdmin ( |
139 | | - set mErrType=Error: No admin right. Please run as admin.%noAdminEcppMsg% |
140 | | - set mErrCode=7 |
141 | | - goto :error |
| 148 | +if defined noWSH ( |
| 149 | + echo. |
| 150 | + echo # ERROR: Windows Scripting Host is disabled. |
| 151 | + echo. |
| 152 | + pause |
| 153 | + goto :EOF |
142 | 154 | ) |
143 | 155 |
|
144 | | -:: elevate as admin or output error, unless its already been elevated 1 time |
145 | | -:: if defined noAdmin @if not defined secondRun ("%ecppDir%\Data\Batch\3rdparty\HP\elevate.cmd" "%comspec%" /c start "" /D "%ecppDir%\data\batch\tasks\ChMac\" "ChMac.bat" /2ndtime) else (echo.&echo :: Sorry. Admin rights are required.&echo.&pause&exit) |
146 | | -:: (cancelled because devcon.exe wouldn't be in path) |
| 156 | +:: detect if system supports "attrib" |
| 157 | +attrib >nul 2>&1 |
| 158 | +if "%errorlevel%"=="9009" set noAttrib=1 |
| 159 | + |
| 160 | +:: detect admin rights |
| 161 | +if defined noAttrib goto :skipAdminCheck |
| 162 | +attrib -h "%windir%\system32" | find /i "system32" >nul 2>&1 |
| 163 | +if %errorlevel% EQU 0 ( |
| 164 | + if "%UACenabled%" EQU "1" ( |
| 165 | + REM only when UAC is enabled can this script be elevated. Otherwise, non-stop prompting will occur. |
| 166 | + cscript //NoLogo ".\Data\_elevate.vbs" "%CD%\" "%CD%\chmac.bat" >nul 2>&1 |
| 167 | + goto :EOF |
| 168 | + ) else ( |
| 169 | + echo. |
| 170 | + echo ** WARNING: Script running without admin rights. Cannot continue. |
| 171 | + echo. |
| 172 | + pause |
| 173 | + goto :EOF |
| 174 | + ) |
| 175 | +) |
| 176 | +:skipAdminCheck |
147 | 177 |
|
148 | 178 | :: ======================================================== parameter and input check |
149 | 179 |
|
|
0 commit comments