You are not logged in.

#1 20 May 2014 10:09

VBSNewb
New Member
Registered: 14 May 2014
Posts: 1

return errorlevel from VBS to BATCH

Hello!

This is my VBScript:

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


And this is my batch:

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

And it always echos 10, even though it vbscript processes correctly.

My goal: Determine if the user clicked NO in the UAC prompt and then exit the batch with an error message ("no admin rights" or whatever).

It don't know how to make the vbscript not return the errorlevel of the first WScript.Quit but of the second one...

Help is highly appreciated!!

Offline

Board footer

Powered by