MarkBe

RunAsWait @ComSpec Batch not running RegCheck correctly

6 posts in this topic

I am using autoIT to elevate a batch script that installs applications and patches. This needs to run on both x86 and x64 computers so I compiled the AutoIT in x86

RunAsWait ($USERNAME, $DOMAIN, $PASSWORD, $RUN_LOGON_NOPROFILE, @ComSpec & " /k " & $SourcePath & $SoftwareBatchInstaller, @SystemDir)

To speed the install process the batch script that I am calling (SoftwareBatchInstaller) contains a bunch of RegCheck commands to look for the uninstall key; skips that install if found. Then it can be used to install further patches just by running the AutoIT again

@ECHO off
SETLOCAL enableextensions

SET InstallPath=\\server\share\
SET RegApp1=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{123123-123-123-123-123123123123123}
SET RegApp2=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{123123-123-123-123-123123123123456}
SET FileApp1=app1.exe
SET FileApp2=app2.exe

:: Install App1
SET RegCheck=%RegApp1%
SET InstallFile=%FileApp1%
CALL :InstallSW

:: Install App2
SET RegCheck=%RegApp2%
SET InstallFile=%FileApp2%
CALL :InstallSW

EXIT

:InstallSW
REG QUERY %RegCheck% >nul 2>nul
IF NOT ERRORLEVEL 1 (EXIT /B)
ECHO.
ECHO Installing %InstallFile% 
START "%InstallFile%" /wait "%InstallPath%%InstallFile%" /s /v"/qn /norestart"
EXIT /B

The trouble I am having is that the batch script runs correctly when elevated on a x64 machine but when called from the AutoIT the RegCheck does not find the keys it's looking for so runs the install again of software that is already installed

Any ideas on a better way to do this? Am I missing a switch on the @ComSpec that will run the batch as is rather than "converting" it to x64?

Share this post


Link to post
Share on other sites



When dealing with the registry on a 64bit machine, I typically will do something like this:

Local $sKey = ((@OSArch = "X64") ? "HKLM64" : "HKLM") & "\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
Local $sSubKey = ""

    For $i = 1 To 100
        $sSubKey = RegEnumKey($sKey, $i)
        ConsoleWrite($sSubKey & @CRLF)
    Next

 


When you're dead, you don't know you're dead - it's only difficult for those that know you. It's the same way when you're stupid...

My Scripts: SCCM UDFInclude Source with Compiled Script, Windows Firewall UDF

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks for the help. Rather than getting the batch to run correctly, I moved to installing the application using AutoIT rather than calling a batch script and I got most of the way done (registry checks, uninstall of old versions) except the .exe installer does not install. It pops up the "installing" window but does not get to the UAC prompt. Please can you check my syntax?

#NoTrayIcon
#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>

Local $SoftwareName="7-Zip"
Local $SourcePath="\\server\7Zip\"
Local $x86Installer="7z1604.exe"
Local $x64Installer="7z1604-x64.exe"
Local $InstallerSwitches=" /S"

Local $USERNAME="User"
Local $PASSWORD="Password"
Local $DOMAIN="OurDomain"

; Installation of software. Calls the EXE with elevated rights
Local $SWInstaller=((@OSArch = "X86") ? $x86Installer : $x64Installer)
MsgBox (48, "Installing", "Installing " & $SoftwareName, 3)
RunAsWait ($USERNAME, $DOMAIN, $PASSWORD, $RUN_LOGON_NOPROFILE, $SourcePath & $SWInstaller & $InstallerSwitches, @SystemDir)

 

Edited by MarkBe

Share this post


Link to post
Share on other sites
On 28/10/2016 at 2:33 AM, JLogan3o13 said:

Try adding #RequireAdmin at the top of your script.

I did try your suggestion but didn't work for non-admin user. I am pushing this app out to users that don't have admin rights, hence the run-as element. Am I correct in that the #RequireAdmin tag will only allow the script to run if the user is an admin?

I am also compiling the script in x86 to allow this to be run on both architectures (in case that makes a difference). Publishing the .exe through group policy .zap file so can't check for system architecture before .exe is run

Share this post


Link to post
Share on other sites

Managed to get this working. copied to local drive, then used Comspec to run the installer

#NoTrayIcon
#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
#include <FileConstants.au3>

;##########################################################################################################
; 7-Zip installation script
; Author : Mark Beaven
;##########################################################################################################

;Variables
; ---------------------------------------------------------------------------------------------------------

Local $SoftwareName="7-Zip"

; Error messages (shouldn't need to alter these)
Local $UninstallingOldMessage="Uninstalling old versions of " & $SoftwareName & "."
Local $AlreadyInstalledMessage=$SoftwareName & " is already the current version."
Local $InstallingNewMessage="Installing " & $SoftwareName & "."
Local $EndMessage=$SoftwareName & " is now installed. Click 'OK' to close this window."
Local $FailMessage=$SoftwareName & " failed to install. Please report error to your local IT department."

; Source path (Don't forget the "\" at the end of the path)==> \\Servername\Share\
Local $SourcePath="\\Servername\Share\7Zip\"
Local $WorkingDirectory="C:\InstallCache\7zip\"
Local $x86Installer="7z1604.exe"
Local $x64Installer="7z1604x64.exe"
Local $InstallerSwitches=" /S"

; Registry location and values that are checked for current software version. Don't use paths with hash values
; Ensure the x64 key is preceeded with 'HKLM64'
Local $RegKey86="HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\7-Zip"
Local $RegKey64="HKLM64\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\7-Zip"
Local $RegValueName="DisplayVersion"
Local $RegValueData="16.04"

; Reg keys and values that are checked for old versions
Local $920RegKey86="HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{23170F69-40C1-2701-0920-000001000000}"
Local $920RegKey64="HKLM64\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{23170F69-40C1-2702-0920-000001000000}"
Local $920RegKey86on64="HKLM64\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{23170F69-40C1-2701-0920-000001000000}"
Local $920RegVerString="DisplayVersion"
Local $920RegVerValue="9.20.00.0"

;Uninstall strings
Local $920UninstallStringx86="MsiExec.exe /X{23170F69-40C1-2701-0920-000001000000} /qb- /norestart"
Local $920UninstallStringx64="MsiExec.exe /X{23170F69-40C1-2702-0920-000001000000} /qb- /norestart"

; Variables to set to NUL
Local $CheckVersion86=""
Local $CheckVersion64=""
Local $CheckVersion86on64=""

;Credentials
; ---------------------------------------------------------------------------------------------------------

Local $USERNAME="AdminUser"
Local $PASSWORD="AdminPWD"
Local $DOMAIN="Domain"


;##########################################################################################################
; Excecution
;##########################################################################################################

; Check for and uninstall old versions
Local $CheckVersion86=RegRead ($920RegKey86, $920RegVerString)
Local $CheckVersion64=RegRead ($920RegKey64, $920RegVerString)
Local $CheckVersion86on64=RegRead ($920RegKey86on64, $920RegVerString)
If $CheckVersion86=$920RegVerValue Or $CheckVersion86on64=$920RegVerValue Then
    MsgBox ($MB_ICONWARNING, "Uninstalling", $UninstallingOldMessage, 3)
    RunAsWait ($USERNAME, $DOMAIN, $PASSWORD, $RUN_LOGON_NOPROFILE,$920UninstallStringx86, @SystemDir)
EndIf
If $CheckVersion64=$920RegVerValue Then
    MsgBox ($MB_ICONWARNING, "Uninstalling", $UninstallingOldMessage, 3)
    RunAsWait ($USERNAME, $DOMAIN, $PASSWORD, $RUN_LOGON_NOPROFILE,$920UninstallStringx64, @SystemDir)
EndIf

; Variables to set to NUL (again)
Local $CheckVersion86=""
Local $CheckVersion64=""

; Check existing version of Software
Local $CheckVersion86=RegRead ($RegKey86, $RegValueName)
Local $CheckVersion64=RegRead ($RegKey64, $RegValueName)

If $CheckVersion86=$RegValueData Or $CheckVersion64=$RegValueData Then
    MsgBox ($MB_ICONWARNING, "Installation aborted", $AlreadyInstalledMessage)
    Exit
EndIf

; Installation of software. Calls the EXE with elevated rights
Local $SWInstaller=((@OSArch = "X86") ? $x86Installer : $x64Installer)
MsgBox ($MB_ICONINFORMATION, "Installing", $InstallingNewMessage, 3)
FileCopy ($SourcePath & $SWInstaller, $WorkingDirectory & $SWInstaller,$FC_CREATEPATH )
RunAsWait ($USERNAME, $DOMAIN, $PASSWORD, $RUN_LOGON_NOPROFILE, @ComSpec & ' /k START "' & $SoftwareName & ' Installer" /wait "' & $WorkingDirectory & $SWInstaller & '"' & $InstallerSwitches & ' && EXIT', @SystemDir, @SW_HIDE)

;Check install went OK
Local $CheckVersion86=RegRead ($RegKey86, $RegValueName)
Local $CheckVersion64=RegRead ($RegKey64, $RegValueName)

If $CheckVersion86=$RegValueData Or $CheckVersion64=$RegValueData Then
    MsgBox ($MB_ICONINFORMATION, "Installation completed", $EndMessage)
Else
    MsgBox ($MB_ICONWARNING, "Installation failed", $FailMessage)
EndIf

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now