从VB脚本运行批处理文件时出错

时间:2021-09-03 02:19:39

VB scripting is completely alien to me but today landed in a situation to write a small one. I need Admin rights to run my .bat file. So I am trying to elevate to Admin rights if not have them. With the help of SO and Google I reached upto:

VB脚本对我来说完全陌生,但今天陷入了写一个小脚本的情况。我需要管理员权限来运行我的.bat文件。所以我试图提升为管理员权利,如果没有他们。在SO和谷歌的帮助下,我达到了:

Function Length()

Set WshShell = WScript.CreateObject("WScript.Shell") 
If WScript.Arguments.length = 0 Then 
    Set ObjShell = CreateObject("Shell.Application") 
    ObjShell.ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """" & " RunAsAdministrator", , "runas", 1 
Else
    Dim shell
    set shell=createobject("wscript.shell")
    shell.run "ExtractFiles.bat"
End If

End Function

Length

Here, this .vbs and ExtractFiles.bat are saved in same folder. I opened 2 command prompts. One in Admin mode and other normal. When running this script thorugh command prompt in Admin mode, I am getting success. But in normal mode, first I get a window to switch to Admin mode and I press Yes on it. Then I get below error:

这里,此.vbs和ExtractFiles.bat保存在同一文件夹中。我打开了2个命令提示符。一个在管理员模式和其他正常。在管理员模式下运行此脚本thorugh命令提示符时,我获得了成功。但在正常模式下,首先我会有一个窗口切换到管理模式,然后我按下是。然后我得到以下错误:

从VB脚本运行批处理文件时出错

Can anyone point me to correct code. I am getting error in line shell.run "ExtractFiles.bat". Please help!

任何人都可以指出我正确的代码。我在行shell.run“ExtractFiles.bat”中收到错误。请帮忙!

As I have also mentioned the requirement, a different approach is also welcome. In this problem, I am not sure how I am able to run the bat file in admin mode and failing in normal mode.

正如我也提到了这一要求,也欢迎采用不同的方法。在这个问题中,我不知道如何在管理模式下运行bat文件并在正常模式下失败。

3 个解决方案

#1


0  

Add the Admin VBS code into your bat file. Here is my routine for the job.

将管理员VBS代码添加到您的bat文件中。这是我的工作惯例。

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Admin <Return> [Needed] [Success]
:: Check for Administrator privileges and request privileges if Needed 'true'.
:::: Usage: call :Admin xReturn true
:: Return success value, if user is Admin. Default `true` if Success not set.
setlocal
set "xVBUAC=%Temp%\AdminUAC.vbs"
set "xSuccess=true"
set "xAdmin=false"
if not "%~3"=="" set "xSuccess=%~3"

:: Check for Access
::net session >nul 2>&1
>nul 2>&1 "%SystemRoot%\system32\cacls.exe" "%SystemRoot%\system32\config\system"
if %ErrorLevel% EQU 0 set "xAdmin=%xSuccess%"

:: Execute UAC
if /i not "%xAdmin%"=="%xSuccess%" if not "%~2"=="" if /i "%~2"=="true" (
    echo Set UAC = CreateObject^("Shell.Application"^) > "%xVBUAC%"
    echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%xVBUAC%"
    if exist "%xVBUAC%" (
        "%xVBUAC%"
        rem if %ErrorLevel% EQU 5 echo Access Denied. Launching UAC.
        del "%xVBUAC%"
    )
)
endlocal & if not "%~1"=="" set "%~1=%xAdmin%"
goto :eof

How to Use it

如何使用它

:: Example Admin check

@echo off
setlocal EnableExtensions

call :Admin xReturn true 1
if not "%xReturn%"=="1" goto End

:: Do my .bat stuff here.

goto End

:: TODO Place the admin function here.

:End

#2


0  

Depending on how you launch the VBScript the directory in which the scripts reside isn't necessarily the working directory. Try this:

根据您启动VBScript的方式,脚本所在的目录不一定是工作目录。试试这个:

Set fso = CreateObject("Scripting.FileSystemObject")
scriptDir = fso.GetParentFolderName(WScript.ScriptFullName)
shell.run "%COMSPEC% /c """ & fso.BuildPath(scriptDir, "ExtractFiles.bat") & """"

#3


0  

What you enter in the command prompt? Is it...

你在命令提示符中输入了什么?是吗...

InstallACS.vbs ExtractFiles.bat

Your script works just fine on XP x64 (if that important) never mind how I'll run it - from the sell or from the console, and also work with and without argument.

你的脚本在XP x64上运行得很好(如果这很重要)从不介意我将如何运行它 - 从销售或控制台,也可以使用和不使用参数。

#1


0  

Add the Admin VBS code into your bat file. Here is my routine for the job.

将管理员VBS代码添加到您的bat文件中。这是我的工作惯例。

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Admin <Return> [Needed] [Success]
:: Check for Administrator privileges and request privileges if Needed 'true'.
:::: Usage: call :Admin xReturn true
:: Return success value, if user is Admin. Default `true` if Success not set.
setlocal
set "xVBUAC=%Temp%\AdminUAC.vbs"
set "xSuccess=true"
set "xAdmin=false"
if not "%~3"=="" set "xSuccess=%~3"

:: Check for Access
::net session >nul 2>&1
>nul 2>&1 "%SystemRoot%\system32\cacls.exe" "%SystemRoot%\system32\config\system"
if %ErrorLevel% EQU 0 set "xAdmin=%xSuccess%"

:: Execute UAC
if /i not "%xAdmin%"=="%xSuccess%" if not "%~2"=="" if /i "%~2"=="true" (
    echo Set UAC = CreateObject^("Shell.Application"^) > "%xVBUAC%"
    echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%xVBUAC%"
    if exist "%xVBUAC%" (
        "%xVBUAC%"
        rem if %ErrorLevel% EQU 5 echo Access Denied. Launching UAC.
        del "%xVBUAC%"
    )
)
endlocal & if not "%~1"=="" set "%~1=%xAdmin%"
goto :eof

How to Use it

如何使用它

:: Example Admin check

@echo off
setlocal EnableExtensions

call :Admin xReturn true 1
if not "%xReturn%"=="1" goto End

:: Do my .bat stuff here.

goto End

:: TODO Place the admin function here.

:End

#2


0  

Depending on how you launch the VBScript the directory in which the scripts reside isn't necessarily the working directory. Try this:

根据您启动VBScript的方式,脚本所在的目录不一定是工作目录。试试这个:

Set fso = CreateObject("Scripting.FileSystemObject")
scriptDir = fso.GetParentFolderName(WScript.ScriptFullName)
shell.run "%COMSPEC% /c """ & fso.BuildPath(scriptDir, "ExtractFiles.bat") & """"

#3


0  

What you enter in the command prompt? Is it...

你在命令提示符中输入了什么?是吗...

InstallACS.vbs ExtractFiles.bat

Your script works just fine on XP x64 (if that important) never mind how I'll run it - from the sell or from the console, and also work with and without argument.

你的脚本在XP x64上运行得很好(如果这很重要)从不介意我将如何运行它 - 从销售或控制台,也可以使用和不使用参数。