混合批次VBS以管理员身份自动运行 [英] Hybrid batch & VBS autorun as Administrator
问题描述
混合批次&中出现奇怪的错误 未终止实体引用。 VBS,通过使用此代码,然后继续将文件解压缩到系统驱动器上的文件夹中。混合结构的结构类似于建议的
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.
尝试添加CDATA VBS块但未成功。在此处使用-a / 38600712#38600712>并进行更改。重组批处理以将:GetAdminRights 函数替换为其中的同一部分无济于事。有什么想法,这是怎么了?
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
推荐答案
此固定脚本效果很好。它代表一个混合脚本示例,该示例可以在Cmd会话期间将用户提升为管理员权限,并允许运行作为VBS作业添加的其他用户任务。字符< >必须在混合的批处理段中避免,因为它们被WSF批处理段标识为忽略。
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>
这篇关于混合批次VBS以管理员身份自动运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!