Skip to content

Commit 866d138

Browse files
committed
Version 1.3 - 14 Jun 2020
- Elevate as administrator as needed when User Account Control (UAC) is enabled in the system
1 parent ed1933e commit 866d138

File tree

4 files changed

+78
-27
lines changed

4 files changed

+78
-27
lines changed

Data/_elevate.vbs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
' Subscript of Cmd Dict by wanderSick
2+
' See main script -- fy.cmd -- for more details.
3+
4+
' Used to run desired programs as administrator (for Vista or later with UAC)
5+
' Based on http://www.winhelponline.com/articles/185/1/VBScripts-and-UAC-elevation.html
6+
7+
Dim objShell, FSO
8+
Dim strFolder, strFile
9+
On Error Resume Next
10+
Set objShell = CreateObject("Shell.Application")
11+
Set FSO = CreateObject("Scripting.FileSystemObject")
12+
strWorkDir = WScript.Arguments(0)
13+
strFile = WScript.Arguments(1)
14+
strArg = "/c start " & chr(34) & chr(34) & " /d " & chr(34) & strWorkDir & chr(34) & " " & chr(34) & strFile & chr(34)
15+
'Debug line
16+
'Msgbox strWorkDir & strFile & vbcrlf & strArg
17+
If FSO.FileExists(strFolder & strFile) Then
18+
objShell.ShellExecute "cmd.exe", strArg, "", "runas", 1
19+
Else
20+
MsgBox "File " & strFile & " not found."
21+
End If

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ All Windows operating systems from Windows 2000 and up (Windows 10 1809 at the m
211211

212212
Admin rights are required for editing MAC addresses, disabling and re-enabling network adapters.
213213

214-
- ChMac does not automatically elevate itself if there is no admin rights. Although there is error checking mechanism for being non-admin, it would be better to make sure admin rights are available before executing ChMac
214+
- Since version 1.3, ChMac automatically elevates itself if there is no admin rights when User Account Control (UAC) is enabled in the system
215215

216216
### Dependencies
217217

@@ -301,8 +301,9 @@ Return code: 7
301301

302302
## Release History
303303

304-
| Ver | Date | Changes | MD5 |
305-
| --- | --- | --- | --- |
306-
| 1.2 | 20190629 | - Improve compatibility on Windows 10 by changing binaries of GNU Linux utilities<br>- Handle cases when the system PATH variable contains parenthesis | 28d0f84846ee5a0c1aa0e0b25c6a3381 |
307-
| 1.1 | 20120916 | Interim release | |
308-
| 1.0 | 20100124 | First released in 2010 | |
304+
| Ver | Date | Changes |
305+
| --- | --- | --- |
306+
| 1.3 | 20200614 | - Elevate as administrator as needed when User Account Control (UAC) is enabled in the system |
307+
| 1.2 | 20190629 | - Improve compatibility on Windows 10 by changing binaries of GNU Linux utilities<br>- Handle cases when the system PATH variable contains parenthesis |
308+
| 1.1 | 20120916 | Interim release |
309+
| 1.0 | 20100124 | First released in 2010 |

Readme.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
[ ChMac v1.2 ]
2+
[ ChMac v1.3 ]
33

44
https://wandersick.blogspot.com | [email protected]
55

@@ -106,9 +106,8 @@
106106
# Admin rights are required for editing MAC addresses, disabling and re-
107107
enabling network adapters
108108

109-
- ChMac does not automatically elevate itself if there is no admin rights.
110-
Although there is error checking mechanism for being non-admin, it would
111-
be better to make sure admin rights are available before executing ChMac
109+
- Since version 1.3, ChMac automatically elevates itself if there is no
110+
admin rights when User Account Control (UAC) is enabled in the system
112111

113112
# ChMac wraps around DevCon (optional), OS-native and GNU Linux utilities
114113

chmac.bat

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
:: Script: ChMac
44
:: Filename: chmac.bat
5-
:: Version: 1.2
6-
:: Last Modified: 29/06/2019
5+
:: Version: 1.3
6+
:: Last Modified: 14/06/2020
77
:: Creation Date: 24/01/2010
88
:: Author: wandersick
99
10-
:: Web: https://wandersick.blogspot.com
10+
:: Web: https://tech.wandersick.com
1111
:: 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)
1313

1414
:: Description:
1515

@@ -40,7 +40,7 @@ if defined debug2 echo on&set debug=1&echo :: Debugging mode 2 is ON.
4040
:: set error code to 0
4141
set mErrCode=0
4242
set mAutoChangeInterval=None
43-
set ChMacVersion=v1.2
43+
set ChMacVersion=v1.3
4444

4545
:: if elevated itself
4646
:: if /i "%1" EQU "/2ndtime" set secondRun=1
@@ -128,22 +128,52 @@ if /i "%~1"=="/help" (
128128
)
129129

130130

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+
)
135146
)
136147

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
142154
)
143155

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
147177

148178
:: ======================================================== parameter and input check
149179

0 commit comments

Comments
 (0)