问题
I'm getting strange error "Unterminated entity reference" in a hybrid batch & VBS, which popups the UAC "Run as Admin" Dialog using an adaptation of this code, and then proceeds with unzipping a file to a folder on the system drive. The hybrid is structured similar to suggested here, and changing it is undesirable.
Tried adding CDATA VBS blocks without success. Restructuring the batch to replace :GetAdminRights function with the same section inside it doesn't help. Any ideas, what's wrong here?
<!-- : Begin batch script
@echo off
setlocal EnableExtensions EnableDelayedExpansion
set vbs="%temp%\_.vbs" & set "dir=C:\Unzip"
set "file=%USERPROFILE%\Downloads\archive.zip\"
call :GetAdminRights
cscript //nologo "%~f0?.wsf" //job:UNZ "%dir%" "%file%"
exit /b
:GetAdminRights
REM --> Check for permissions
IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" (
>nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system"
) ELSE (>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system")
REM --> If error flag set, user don't have admin permissions
if '%errorlevel%' NEQ '0' (echo Requesting administrative privileges...
) else (set "adm=1")
if not defined adm (set params = %*:"=""
cscript //nologo "%~f0?.wsf" //job:ADM %~s0 !params!
) else (pushd "%CD%" & CD /D "%~dp0")
exit /b
----- Begin wsf script --->
<package>
<job id="ADM"><script language="VBScript">
<![CDATA[
Set UAC = CreateObject("Shell.Application")
UAC.ShellExecute "cmd.exe", "/c ""wscript.Arguments(0)"" wscript.Arguments(1)", "", "runas", 1
]]>
</script></job>
<job id="UNZ"><script language="VBScript">
<![CDATA[
set fso = CreateObject("Scripting.FileSystemObject")
If NOT fso.FolderExists(wscript.Arguments(0)) Then
fso.CreateFolder(wscript.Arguments(0))
End If
set objShell = CreateObject("Shell.Application")
set FilesInZip = objShell.NameSpace(wscript.Arguments(1)).items
objShell.NameSpace(wscript.Arguments(0)).CopyHere(FilesInZip)
set fso = Nothing
set objShell = Nothing
]]>
</script></job>
</package>
::Error
\test.bat?.wsf(44, 12) Windows Script Host: Unterminated entity reference - matching ';' not found
回答1:
This fixed script works well. It represents a hybrid script example, which can elevate a user to Admin rights for the duration of the Cmd session, and allows to run other user tasks added as VBS jobs. Characters < > must be avoided in batch segments of the hybrid, since they identify ignored by WSF batch sections.
<!-- : Begin batch script
@echo off
setlocal EnableExtensions EnableDelayedExpansion
CD /D "%~dp0"
set "dir=%temp%\Unzip" & set "file=%USERPROFILE%\Downloads\archive.zip"
if not "%1"=="ADR" (call :GetAdminRights
if defined adm cscript //nologo "%~f0?.wsf" //job:ADM "/c" "%~sf0" "ADR" )
echo/ & >nul 2>&1 net file && (echo "!errorlevel!" Got admin rights & echo/) ^
|| (echo "!errorlevel!" No admin rights & goto :end)
:: add your code here
echo Performing admin tasks & echo/
cscript //nologo "%~f0?.wsf" //job:UNZ "%dir%" "%file%"
if !errorlevel! equ 0 echo All tasks completed.
:end
timeout /t 5 >nul
exit /b
:GetAdminRights
REM Check for permissions
echo/ & >nul 2>&1 net session && (echo "!errorlevel!" Got admin rights) ^
|| (echo "!errorlevel!" No admin rights) & echo/
REM If error flag set, user don't have admin permissions
if '!errorlevel!' NEQ '0' (set "adm=0" & echo Requesting admin rights...)
exit /b
----- Begin wsf script --->
<package>
<job id="ADM"><script language="VBScript">
Set UAC = CreateObject("Shell.Application")
args = ""
For Each strArg in WScript.Arguments
args = args & strArg & " "
Next
UAC.ShellExecute "cmd.exe", args, "", "runas", 1
</script></job>
<job id="UNZ"><script language="VBScript">
set fso = CreateObject("Scripting.FileSystemObject")
If NOT fso.FolderExists(wscript.Arguments(0)) Then
fso.CreateFolder(wscript.Arguments(0))
End If
set objShell = CreateObject("Shell.Application")
set FilesInZip = objShell.NameSpace(wscript.Arguments(1)).items
objShell.NameSpace(wscript.Arguments(0)).CopyHere(FilesInZip)
set fso = Nothing
set objShell = Nothing
</script></job>
</package>
来源:https://stackoverflow.com/questions/38621603/hybrid-batch-vbs-autorun-as-administrator