从vbscript获取bat中的错误级别 [英] Get errorlevel in bat from vbscript

查看:67
本文介绍了从vbscript获取bat中的错误级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定用户是否单击了UAC提示中的否",以及是否要不设置nul端口.

I’m trying to determine, whether the user clicked NO in the UAC-prompt and if so to not set up the nul-port.

我将此脚本称为一个批处理文件,如果用户单击否",我想退出该文件.

I'm calling this script form a batch-file, which I'd like to exit, if the user clicked no.

VBScript:

显式选项

Main 

Sub Main

    Dim oShell, objWMIService, servSpooler, objReg, objShellApp, result

    Const PrinterPort = "NUL:"
    Const HKLM = &h80000002

    If Not WScript.Arguments.Named.Exists("elevate") Then
        Set objShellApp = CreateObject("Shell.Application")
        objShellApp.ShellExecute WScript.FullName, WScript.ScriptFullName & " /elevate", "", "runas", 0
        WScript.Quit 
    End If

    result = isElevated()

    If result = True Then
        Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
        set servSpooler = objWMIService.Get("Win32_Service.Name='spooler'")
        Set objReg = GetObject("winmgmts:root\default:StdRegProv")

        servSpooler.StopService
        objReg.SetStringValue HKLM, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports", PrinterPort, ""
        servSpooler.StartService
    Else
        WScript.Quit 1
    End If

End Sub

Function isElevated
    Dim shell, whoami, whoamiOutput, strWhoamiOutput

    Set shell = CreateObject("WScript.Shell")
    Set whoami = shell.Exec("whoami /groups")
    Set whoamiOutput = whoami.StdOut
    strWhoamiOutput = whoamiOutput.ReadAll

    If InStr(1, strWhoamiOutput, "S-1-16-12288", vbTextCompare) Then
        isElevated = True
    Else
        isElevated = False
    End If
End Function

批次:

cscript "set_port.vbs"
IF ERRORLEVEL 1 (
    ECHO FAIL
    PAUSE
    EXIT
)

现在,我查看了此页面:

Now, I looked at this page:

http://www.robvanderwoude.com/errorlevel.php

和其他一些,我觉得我尝试了所有可能的组合.可能我还没有正确的组合.一些提示和帮助将不胜感激!

and some others and I feel like I tried every possible combination. Probably, I just haven’t had the correct combination yet. Some tips and help would be highly appreciated!

基本目标:确定用户是否在UAC提示中单击否",然后结束VBScript和批处理文件.

The basic goal: Determine, whether the user clicked NO in the UAC-prompt and then end the VBScript and batch-file.

更新:

好的,谢谢您提供的所有答案.我现在可以肯定是脚本.我再次在批处理文件中使用了错误级别,并且现在可以正常使用了.

Okay, thanks for all the answers so far. I'm pretty certain now it's the script. I use the errorlevel again in the batch-file and there it works just fine now.

对于VBScript: 为了使用户在UAC提示中单击否"时出现错误代码(例如1)(意味着当前文件未提升),我需要这样输入:

As for the VBScript: In Order to have an error code of let's say 1 when the user clicks NO in the UAC prompt (meaning the current file is not elevated), I need to put it like this:

If result = True Then
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
    set servSpooler = objWMIService.Get("Win32_Service.Name='spooler'")
    Set objReg = GetObject("winmgmts:root\default:StdRegProv")

    servSpooler.StopService
    objReg.SetStringValue HKLM, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports", PrinterPort, ""
    servSpooler.StartService
    WScript.Quit(0)
Else
    WScript.Quit(1)
End If

但是:在ShellExecute之后的第一个WScript.Quit中,我还需要放置WScript.Quit(1),对吗?因为否则,我永远都不会收到将错误传递到错误级别(或至少不大于0)的错误.

But: in the first WScript.Quit after the ShellExecute, I also need to put WScript.Quit(1), right? Because otherwise I never get an error to be passed to errorlevel (or at least not greater than 0).

所以:

    objShellApp.ShellExecute WScript.FullName, WScript.ScriptFullName & " /elevate", "", "runas", 0
    WScript.Quit(1)

我想,最大的问题是,在UAC promtp上单击否"并不会导致错误,因此我需要将WSCript.Quit(1)放在此处.

The big problen, I guess, is that clicking NO on the UAC promtp doesnt eally cause an error, so I need to put WSCript.Quit(1) there.

或者我反过来说再说一次:WScript.Quit(1)当用户单击是"并且脚本被提升并将WScript.Quit(0)放到其他位置时.

OR i do it the other way round and say: WScript.Quit(1) when the user clicked YES and the script is elevated and put WScript.Quit(0) everyhwere else.

但是,在第一种情况下,我总是获得错误级别1,在第二种情况下,我总是获得错误级别0.

However, in the first case I always get errorlevel 1 and in the second case always errorlevel 0.

-----------更新:

----------- UPDATE:

我的VBScript文件现在看起来像这样:

My VBScript file looks like this now:

    Option Explicit

    Main 

    Sub Main

    Dim objShell, objWMIService, servSpooler, objReg, objShellApp, result, oShell
    Dim whoami, strWhoamiOutput, whoamiOutput

        Const PrinterPort = "NUL:"
        Const HKLM = &h80000002

    If Not WScript.Arguments.Named.Exists("elevate") Then
            Set objShellApp = CreateObject("Shell.Application")
            objShellApp.ShellExecute WScript.FullName, WScript.ScriptFullName & " /elevate", "", "runas", 0
            WScript.Quit 10
WScript.Echo("Done")
    Else

        Set oShell = CreateObject("WScript.Shell")
        Set whoami = oShell.Exec("whoami /groups")
        Set whoamiOutput = whoami.StdOut
        strWhoamiOutput = whoamiOutput.ReadAll

        If InStr(1, strWhoamiOutput, "S-1-16-12288", vbTextCompare) Then
            Wscript.Echo("ADMIN")
            WScript.Echo("Port")
            Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
            set servSpooler = objWMIService.Get("Win32_Service.Name='spooler'")
            Set objReg = GetObject("winmgmts:root\default:StdRegProv")

            servSpooler.StopService
            objReg.SetStringValue HKLM, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports", PrinterPort, ""
            servSpooler.StartService
            WScript.Quit 1
        End if
        WScript.Echo("Done 2")

    End If

    End Sub

还有一个测试批次:

@echo off
cscript "test.vbs"
ECHO %errorlevel%
PAUSE

错误级别输出为10而不是1,尽管脚本已按预期退出,并且从未显示消息完成".

The errorlevel output is 10 and not 1, although the script is quit as intended and the message "Done" is never shown.

推荐答案

调试技术:

编写一个仅设置错误级别并退出的VBS脚本,然后将其与批处理脚本一起使用.

Write a VBS script that just sets the errorlevel and quits - and get that working with your batch script.

然后,您可以查看完整的vbs脚本.

Then you can massage your full vbs script.

这篇关于从vbscript获取bat中的错误级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆