vbs script and RunAs command

Home Forums Scripting Windows Script Host vbs script and RunAs command

This topic contains 6 replies, has 4 voices, and was last updated by Avatar handrian 4 years, 5 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • Avatar
    balubeto
    Member
    #165350

    Hi

    In Windows 7 SP1 64 bit, as I should do to run a .vbs script with elevated privileges with RunAs command?

    Thanks

    Bye

    Avatar
    Ossian
    Moderator
    #190671
    balubeto;n490311 wrote:
    Hi

    In Windows 7 SP1 64 bit, as I should do to run a .vbs script with elevated privileges with RunAs command?

    Thanks

    Bye

    Do you mean “should I run scripts with elevated permissions”? If so, it really depends on what the script does?
    Or do you mean “how do I run scripts with elevated permissions”? If so, please use :google:

    Thanks

    Bye

    Avatar
    handrian
    Member
    #382849
    Ossian;n490313 wrote:
    Do you mean “should I run scripts with elevated permissions”? If so, it really depends on what the script does?
    Or do you mean “how do I run scripts with elevated permissions”? If so, please use :google:

    Thanks

    Bye

    Since I do not know the vbs language, as I should do to run this script

    Code:
    Option Explicit

    Dim objFSO
    Const ForAppending = 8
    set objFSO = CreateObject(“Scripting.FileSystemObject”)

    Dim bElevate: bElevate = False
    If WScript.Arguments.Count > 0 Then If WScript.Arguments(WScript.Arguments.Count-1) “|” then bElevate = True
    if bElevate Or WScript.Arguments.Count = 0 Then ElevateUAC

    UninstallProduct “Nitro PDF Professional 7” ‘ Pro 7 beta
    UninstallProduct “Nitro PDF Professional” ‘ Pro 6
    UninstallProduct “Nitro Pro 8” ‘ Pro 8
    UninstallProduct “Nitro Pro 9” ‘ Pro 9
    UninstallProduct “Nitro Pro 10” ‘ Pro 10
    UninstallProduct “Nitro Pro” ‘ Pro 7
    UninstallProduct “Nitro Reader 3” ‘ Reader 3
    UninstallProduct “Nitro Reader 4” ‘ Reader 4
    UninstallProduct “Nitro PDF Reader” ‘ Reader 1
    UninstallProduct “Nitro PDF Reader 2” ‘ Reader 2 beta
    UninstallProduct “Nitro Reader” ‘ Reader 2
    UninstallProduct “Nitro PDF DMS” ‘ DMS Beta
    UninstallProduct “Nitro DMS” ‘ DMS
    UninstallProduct “Nitro PDF Express” ‘ Express 1
    UninstallProduct “Nitro PDF Express” ‘ Express 2
    UninstallProduct “Nitro OCR Expansion” ‘ OCR Expansion

    Dim WshShell, oExec
    Set WshShell = CreateObject(“WScript.Shell”)
    WshShell.Exec(“REG DELETE “”HKCUSoftwareNitro PDF”” /f”)
    WshShell.Exec(“REG DELETE “”HKLMSoftwareNitro PDF”” /f”)
    WshShell.Exec(“REG DELETE “”HKCUSoftwareNitro”” /f”)
    WshShell.Exec(“REG DELETE “”HKLMSoftwareNitro”” /f”)
    WshShell.Exec(“REG DELETE “”HKLMSoftwareWow6432NodeNitro PDF”” /f”)
    WshShell.Exec(“REG DELETE “”HKLMSoftwareWow6432NodeNitro”” /f”)
    WshShell.Exec(“cmd /c rmdir /q /s “”%AppData%Nitro PDF”””)
    WshShell.Exec(“cmd /c rmdir /q /s “”%AppData%Nitro”””)
    WshShell.Exec(“cmd /c rmdir /q /s “”%ProgramData%Nitro PDF”””)
    WshShell.Exec(“cmd /c rmdir /q /s “”%ProgramData%Nitro”””)

    Sub UninstallProduct(productName)
    Dim productCode, property, installer, BurnGUID

    Set installer = Wscript.CreateObject(“WindowsInstaller.Installer”)

    For Each productCode In installer.Products
    ‘WScript.Echo “Product Name ” & productName & “Product Code ” & productCode &” : ” & LCase(installer.ProductInfo(productCode, “ProductName”))
    If InStr(1, LCase(installer.ProductInfo(productCode, “ProductName”)), LCase(productName)) Then Exit For
    Next

    If Not IsEmpty(productCode) Then
    Dim WshShell, UninstallString
    Set WshShell = CreateObject(“WScript.Shell”)
    if productName=”Nitro Pro 9″ Or productName=”Nitro Pro 10″ then
    WriteToLog(“Try to find Burn GUID in SOFTWAREClassesInstallerDependencies”&productCode&”Dependents”)
    BurnGUID = getRegistryKey(“SOFTWAREClassesInstallerDependencies”&productCode&”Dependents”, “”)

    If IsEmpty(BurnGUID) Or BurnGUID=”” Then
    WriteToLog(“Try to find Burn GUID in SOFTWAREWow6432NodeClassesInstallerDependencies”&productCode&”Dependents”)
    BurnGUID = getRegistryKey(“SOFTWAREWow6432NodeClassesInstallerDependencies”&productCode&”Dependents”, “”)
    End If

    if Not IsEmpty(BurnGUID) And Not BurnGUID=”” Then
    WriteToLog(productName&” Burn GUID:”&BurnGUID)
    ‘WriteToLog(“Try to find UninstallString in SOFTWAREWow6432NodeMicrosoftWindowsCurrentVersionUninstall”&BurnGUID)
    UninstallString = getRegistryValue(“SOFTWAREWow6432NodeMicrosoftWindowsCurrentVersionUninstall”&BurnGUID, “UninstallString”, “”)
    If IsEmpty(UninstallString) Or UninstallString=”” Then
    ‘WriteToLog(“Try to Burn UninstallString in SOFTWAREMicrosoftWindowsCurrentVersionUninstall”&BurnGUID)
    UninstallString = getRegistryValue(“SOFTWAREMicrosoftWindowsCurrentVersionUninstall”&BurnGUID, “UninstallString”, “”)
    End If
    ‘WriteToLog(“UninstallString:”&UninstallString)
    If Not IsEmpty(UninstallString) Then
    WriteToLog(“Running command:”&UninstallString)
    WshShell.Run UninstallString, , True
    End If
    else
    WriteToLog(“msiexec /x ” & productCode & ” /qb”)
    WshShell.Run “msiexec /x ” & productCode & ” /qb”, , TRUE
    End If
    else
    WriteToLog(“msiexec /x ” & productCode & ” /qb”)
    WshShell.Run “msiexec /x ” & productCode & ” /qb”, , TRUE
    End If

    End If
    End Sub

    Sub ElevateUAC
    Dim sParms, oShell
    sParms = ” |”
    If WScript.Arguments.Count > 0 Then
    For i = WScript.Arguments.Count-1 To 0 Step -1
    sParms = ” ” & WScript.Arguments(i) & sParms
    Next
    End If

    Set oShell = CreateObject(“Shell.Application”)
    oShell.ShellExecute “cscript.exe”, “””” & WScript.ScriptFullName & “””” & sParms, , “runas”, 1
    WScript.Quit
    End Sub

    function getRegistryKey (strRegistryKey, strDefault )
    Dim value, strComputer
    strComputer = “.”
    Const HKEY_LOCAL_MACHINE = &H80000002
    Dim oReg : Set oReg = GetObject(“winmgmts:{impersonationLevel=impersonate}!\.rootdefault:StdRegProv”)
    Dim oShell : Set oShell = CreateObject(“WScript.Shell”)
    Dim sPath, aSub, sKey
    oReg.EnumKey HKEY_LOCAL_MACHINE, strRegistryKey, aSub
    If IsArray( aSub ) Then
    For Each sKey In aSub
    value=sKey
    ‘wscript.echo “Burn GUID:”&sKey
    Next
    getRegistryKey=value
    else
    getRegistryKey= strDefault
    end if
    end function

    function getRegistryValue (strRegKey, strRegName, strDefault )
    Dim strComputer
    strComputer = “.”
    Const HKEY_LOCAL_MACHINE = &H80000002

    Dim oReg : Set oReg = GetObject(“winmgmts:{impersonationLevel=impersonate}!\.rootdefault:StdRegProv”)
    Dim oShell : Set oShell = CreateObject(“WScript.Shell”)
    Dim arrValueNames, arrTypes, sKey, strValueName, strValue
    oReg.EnumValues HKEY_LOCAL_MACHINE, strRegKey, arrValueNames, arrTypes

    If IsArray( arrValueNames ) Then
    For Each sKey In arrValueNames
    strValueName = sKey
    oReg.GetStringValue HKEY_LOCAL_MACHINE, strRegKey, strValueName, strValue
    if strValueName=strRegName then
    ‘WriteToLog(“getRegistryValue: “&strValueName&” – “&strValue)
    getRegistryValue=strValue
    End If
    Next
    else
    ‘WriteToLog(“getRegistryValue Default”)
    getRegistryValue= strDefault
    end if
    end function

    REM Function CheckArchitecture()
    REM Dim strValue,objShell
    REM Const strKey = “HKLMSYSTEMCurrentControlSetControlSession ManagerEnvironmentPROCESSOR_ARCHITECTURE”
    REM Set objShell = CreateObject(“WScript.Shell”)
    REM strValue = objShell.RegRead(strKey)
    REM Select Case UCase(strValue)
    REM Case “X86”
    REM CheckArchitecture = 1
    REM Case “AMD64”
    REM CheckArchitecture = 0
    REM End Select
    REM End Function

    Sub WriteToLog(strLogMessage)
    Const ForAppending = 8

    dim shell, tempdir, strLogFileName, strLogEntryTime, objLogFileTransaction
    set shell = WScript.CreateObject(“WScript.Shell”)
    tempdir = shell.ExpandEnvironmentStrings(“%temp%”)

    strLogFileName = tempdir&”Cleanup-burn.log”
    strLogEntryTime = NOW

    if objFSO.FileExists(strLogFileName) Then
    Set objLogFileTransaction =objFSO.OpenTextFile(strLogFileName,ForAppending)
    Else
    Set objLogFileTransaction =objFSO.CreateTextFile(strLogFileName)
    End if

    objLogFileTransaction.WriteLine strLogEntryTime&” “&strLogMessage
    objLogFileTransaction.Close
    WScript.StdOut.WriteLine strLogMessage
    WScript.StdOut.WriteLine “”
    End Sub

    with elevated permissions?

    Thanks

    Bye

    Avatar
    Ossian
    Moderator
    #190672

    You could always try running it without elevated permissions and if (and only if) it fails, run it with.

    I presume you have checked the script and made sure it does not, for example, contain “objVolume.Format(“NTFS”)” anywhere, or any other unexpected surprises

    Rems
    Rems
    Moderator
    #228459
    balubeto;n490311 wrote:
    Hi

    In Windows 7 SP1 64 bit, as I should do to run a .vbs script with elevated privileges with RunAs command?

    Thanks

    Bye

    ) Do you manually start the script? Then you can try the sample script you showed in your second post (just double click on the vbs file, it re-launces itself elevated). But you still have be logged in to Windows with an admin account to be able to uninstall most applications.

    Alternatively, you could add “Run as admin” to the ‘right click menu’ for vbs files on each computer.

    ) If the script is auto launched by a GPO, then make it run as computer Start-up Script. The script will then be executed by the ‘System account’ (Neither the System account nor the built-in Administrator account are subject to UAC).

    Avatar
    handrian
    Member
    #382850

    From the command prompt I tried to run this command:

    Code:
    C:Windowssystem32>runas /profile /user:Administrator “cscript.exe C:UsersPublicDownloadscleanupcleanup-oct17.vbs
    Enter the password for Administrator:

    but I get this error:

    Code:
    Attempting to start the cscript.exe C:UsersPublicDownloadscleanupcleanup-oct17.vbs as “VMWIN7SP1ENTX64Administrator” user …
    RUNAS ERROR: Unable to run – cscript.exe C:UsersPublicDownloadscleanupcleanup-oct17.vbs
    1327: Logon failure: user account restriction. Possible causes could be: the password field blank not allowed, restrictions on access hours or application of policy restrictions.

    Why?

    By case, I should enable the hidden Administrator account?

    Thanks

    Bye

    Rems
    Rems
    Moderator
    #228461
    balubeto;n490319 wrote:
    By case, I should enable the hidden Administrator account?

    Why? the admin account launching the script does not necessarily have to be thé built-in Administrator account. You can use any account that is a member of the local group Administrators.

    The account you use for this should of course be an enabled account and the password should not be expired. For the Runas service it is required that this admin account do has a password.
    The account may or not necessary needs elevation to launch the script.

    If you intend to run this script using the built-in administrator account (or the system account) then you better uncomment the line:
    REM if bElevate Or WScript.Arguments.Count = 0 Then ElevateUAC

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.