Window的ShellExecute函数可以接受WSF脚本参数吗?

时间:2016-07-29 14:52:20

标签: batch-file shellexecute hybrid wsh

下面的批处理混合脚本旨在在运行某些需要提升权限的任务之前自动调用管理员权限。它会弹出UAC提示,但无论用户选择如何,都不会授予管理员权限。

我想知道Window中使用的ShellExecute函数是否可以接受WSF参数或其他可扩展参数?在这种情况下,它是批处理文件名。如果可以,如果没有重组旨在使用不同的方法,脚本应如何更改?

<!-- : Begin batch script
@echo off
setlocal EnableExtensions EnableDelayedExpansion
CD /D "%~dp0" & echo "%*"
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 "%~nx0")
>nul 2>&1 net file && (echo/ & echo "!errorlevel!") || ^
(echo/ & echo "!errorlevel!" & goto :end)

:: add your code here
echo Performing admin tasks
echo Hello >C:\test.txt

:end
timeout 5
exit /b

:GetAdminRights
REM Check for permissions
>nul 2>&1 net file
REM If error flag set, user don't have admin permissions
if '!errorlevel!' NEQ '0' (echo Requesting administrative privileges...
    set "adm=0"
    echo/ & echo "!errorlevel!" "%~nx0" "%~dp0" & echo/)
exit /b

----- Begin wsf script --->
<package>
    <job id="ADM"><script language="VBScript">
        Set UAC = CreateObject("Shell.Application") 
        WScript.Echo wscript.Arguments(0)
        UAC.ShellExecute "cmd.exe", "/c ""wscript.Arguments(0)"" ADR", "", "runas", 1 
    </script></job>
</package>  

:: Error in UAC Prompt (shown in details. Can't expand batch name correctly.)
Program location: "C:\Windows\System32\cmd.exe /c "wscript.Arguments(0)" ADR

2 个答案:

答案 0 :(得分:0)

是的,它可以。诀窍是在cscript调用中提交在管理员模式下重启Cmd所需的所有Cmd.exe参数,并在批处理的WSF部分中正确读取它们。

<!-- : Begin batch script
@echo off
setlocal EnableExtensions EnableDelayedExpansion
CD /D "%~dp0" & echo "%*"
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 Hello >C:\tst.txt

:end
timeout /t 5 >nul
exit /b

:GetAdminRights
REM Check for permissions
echo/ & >nul 2>&1 net session && (echo Got admin rights) ^
 || (echo No admin rights) & echo/
REM If error flag set, user don't have admin permissions
if '!errorlevel!' NEQ '0' (echo Requesting admin rights...
    set "adm=0" & echo/ & echo "!errorlevel!" "%~nx0" "%~dp0" & echo/)
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 
        WScript.Echo args
        UAC.ShellExecute "cmd.exe", args, "", "runas", 1 
    </script></job>
</package>  

答案 1 :(得分:0)

这是一个批处理脚本'Elevate.bat',以管理员身份执行在参数字符串中传递的任何命令。 如果命令包含特殊字符,请用'^'对其进行转义。 例如:

Elevate whoami /groups ^| find "S-1-16-12288"`

Elevate.bat:

    <!-- : --- Self-Elevating Batch Script ---------------------------
    @whoami /groups | find "S-1-16-12288" > nul && goto :admin
    @set "ELEVATE_CMDLINE=cd /d "%cd%" & call "%~f0" %*"
    @cscript //nologo "%~f0?.wsf" //job:Elevate & exit /b

    -->
    <job id="Elevate"><script language="VBScript">
      Set objShell = CreateObject("Shell.Application")
      Set objWshShell = WScript.CreateObject("WScript.Shell")
      Set objWshProcessEnv = objWshShell.Environment("PROCESS")
      strCommandLine = Trim(objWshProcessEnv("ELEVATE_CMDLINE"))
      objShell.ShellExecute "cmd", "/c " & strCommandLine, "", "runas"
    </script></job>
    :admin -----------------------------------------------------------

    @echo off
    echo Running as elevated user.
    echo Script file : %~f0
    echo Arguments   : %*
    echo Working dir : %cd%
    echo.
    :: administrator commands here
    :: e.g., run shell as admin
    %*
    pause

我用它来创建这样的符号链接:

Elevate.bat mklink /d common g:\PerforceData\devel\common
Elevate.bat mklink build.xml g:\PerforceData\devel\build.xml