Jump to content

Please help! Converting batch file into AutoIT Script!


sabbas
 Share

Recommended Posts

I've the following batch file that I use to update windows machines manually:

REM Internet Explorer 11 Installation Script Updated 25/01/2015

@ECHO OFF

SET IE11=D:\TEMP\Updates\IE11
SET Important=D:\TEMP\Updates\Important
SET Optional=D:\TEMP\Updates\Optional


For %%u in ("%IE11%\*.msu") Do (
ECHO Installing Update: %%u
ECHO 
START /WAIT wusa "%%u" /quiet /norestart
)

Echo Internet Explorer 11 Prerequisites Finished.
ECHO ............................................
ECHO Installing Internet Explorer 11

START /WAIT wusa "%IE11%\IE11-Windows6.1-x64-en-us.exe" /quiet /norestart

Pause

Exit

Could someone please help me convert this into a "managable" script? I say managable because I want this to be a starting point for me learning AuotIT. So if you can add comments as to what's happening in your script, that would be greatly appreciated.

Thank you all and especially the creators!

regards,

Shereef

Edited by sabbas
Link to comment
Share on other sites

Im no expert at this sabbas but try this:

#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>

; Internet Explorer 11 Installation Script Updated 25/01/2015

;@ECHO OFF

;SET IE11=D:\TEMP\Updates\IE11
;SET Important=D:\TEMP\Updates\Important
;SET Optional=D:\TEMP\Updates\Optional
EnvSet("IE11", "D:\TEMP\Updates\IE11")
EnvSet("Important", "D:\TEMP\Updates\Important")
EnvSet("Optional", "D:\TEMP\Updates\Optional")

    Local $sEnvVar = EnvGet("IE11") ;retrieve the variable set for IE11 and set $sEnvVar as the same


;For %%u in ("%IE11%\*.msu") Do (
Example()

Func Example()
    ; List all the files and folders in the IE11 directory using the default parameters.
    Local $aFileList = _FileListToArray($sEnvVar, "*msu") ; retrieves any files from directory with extension *.msu and lists them in an array
    If @error = 1 Then
        MsgBox($MB_SYSTEMMODAL, "", "Path was invalid.") ; if filepath is invalid show this message
        Exit
    EndIf
    If @error = 4 Then
        MsgBox($MB_SYSTEMMODAL, "", "No file(s) were found.") ; if no files match *.msu then show this message
        Exit
    EndIf

    For $i = 1 To $aFileList[0] ; loop through the array to retrieve each result
    MsgBox(0, "Notification", "Installting Update: " & $aFileList[$i], 1) ; Display a message to you showing which update is installing, the box will disappear after 1 second or if you click "OK"
    ;ECHO
    ;START /WAIT wusa "%%u" /quiet /norestart
    RunWait(@SystemDir & "\wusa.exe " & $sEnvVar & "\" & $aFileList[$i] & " /quiet /norestart")
    ConsoleWrite(@SystemDir & "\wusa.exe " & $sEnvVar & "\" & $aFileList[$i] & " /quiet /norestart" & @CRLF) ; just to show what line is being passed to the run above (helps troubleshooting)

    ;Echo Internet Explorer 11 Prerequisites Finished.
    ;ECHO ............................................
    MsgBox(0, "Notification", "Internet Explorer 11 Prerequisites Finished", 1) ; Display a message to you showing prerequisites are finished, the box will disappear after 1 second or if you click "OK"

    Next

EndFunc   ;==>Example

    ;ECHO Installing Internet Explorer 11
    MsgBox(0, "Notification", "Installing Internet Explorer 11", 1) ; Display a message to you showing IE11 install will begin, the box will disappear after 1 second or if you click "OK"

    ;START /WAIT wusa "%IE11%\IE11-Windows6.1-x64-en-us.exe" /quiet /norestart
    ;Local $sEnvVar = EnvGet("IE11") ;retrieve the variable set for IE11 and set $sEnvVar as the same
    RunWait(@SystemDir & "\wusa.exe " & $sEnvVar & "\IE11-Windows6.1-x64-en-us.exe /quiet /norestart")
    ConsoleWrite(@SystemDir & "\wusa.exe " & $sEnvVar & "\IE11-Windows6.1-x64-en-us.exe /quiet /norestart") ; just to show what line is being passed to the run above (helps troubleshooting)

    ;Pause
    MsgBox(0, "Notification", "Finished installing Internet Explorer 11", 0) ; Display a message to you showing IE11 install will begin, the box will stay onscreen until you click "OK" replacing your pause

Exit

Good luck!

Ta

Dan

Edited by goss34
Link to comment
Share on other sites

Hi Dan,

Thank you very much for your contribution. Special thanks for adding comments! I started working on my script using your code as a base. After 4 days, I've reached the 'form' that I need for my script. Now, I need you or other generous people's help to bring 'function' :)

1. I don't want current file to be shown as 'CurrentFolderCurrentFile'. Is there a way I can show only 'CurrentFile'?

2. I want the process to check if the filename contains KB* with a - before and after. For example, Windows6.1-KB2670838-x64.exe. It should check for KB2670838 in filename and otherwise skip that file even if it has MSU or EXE extension

3. I want a progress bar, if possible. Not necessary

4. status of the updates after they are run written to a file. errors to one file and success to another using success.datetime.txt and error.datetime.txt

#include <FileParts.au3>
#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>

; Scan Script Folder Recursively and Generate File List Array for files with extenstion MSU and EXE
$uFile = _FileListToArrayRec(@ScriptDir, "*.msu;*.exe", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_SORT)

; Check if the Folder or File Actually Exist
If @error = 1 Then
        MsgBox (48+1, "Error!", "Folder does not exist!", 0)
        Exit
    EndIf
    If @error = 4 Then
        MsgBox (48+1, "Error!", "Files don't exist!", 0)
        Exit
    EndIf

; Setup a general splash screen that will show the current update being installed
SplashTextOn("Installing Update...", "", 500, 100, -1, -1, 0, "", 14, 500)

; loop through the number of files from the array
For $u = 1 To $uFile[0]
;check for extension match
If StringRight($uFile[$u], 3) = "msu" Then
;rewrite splash screen with the current file being processed
    ControlSetText ("Installing Update...", "", "Static1", $uFile[$u])
;Execute the selected file
        RunWait(@SystemDir & "\wusa.exe " &@WorkingDir&"\"&$uFile[$u]& " /quiet /norestart")
    ElseIf StringRight($uFile[$u], 3) = "exe" Then
    ControlSetText ("Installing Update...", "", "Static1", $uFile[$u])
        RunWait(@SystemDir & "\wusa.exe " &@WorkingDir&"\"&$uFile[$u]& " /q /norestart")
    Else
    ControlSetText ("Installing Update...", "", "Static1", $uFile[$u] & " is not a valid update!")
    EndIf
Next
; switch off the splash screen
SplashOff()

Thank you.

with warm regards,

Shereef

Link to comment
Share on other sites

@1: Have you heard of Regular Expressions? It's very powerful, read up on it. We simply manipulate the filename string to remove everything up to and including the last backslash by using StringRegExpReplace(). The result of that function call is the manipulated string, so you can store that in a temp variable and use it for your output. Like this:

$filename = "c:\x\y\z.txt"
$temporaryFilename = StringRegExpReplace($filename, ".*\\", "")
MsgBox(0, 0, $temporaryFilename)

(The helpfile of StringRegExp() has a LOT of interesting information. Be warned: it's pretty much a separate study :) Highly recommended though. Understanding it will be helpful for the rest of your life though, many programming languages and even many text editors can be used so much more effectively if you know RegEx.)

@2: simply change your file specification to *-KB*-*

You can use a regex match with StringRegExp() or a string match with StringInStr() to check the file name and skip it if it doesn't match your pattern. But for your case that's probably not necessary if you can do it through the filespec. Example in @3 uses StringRegExp to do what you want, just to demo.

 

@3 (and well, let's do the regex option of @2 too while we're at it):

$uFile = _FileListToArrayRec(@ScriptDir, "*.msu;*.exe", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_SORT)

$totalNrOfFiles = $uFile[0]

ProgressOn("Progress", "Working...")

For $i = 1 To $totalNrOfFiles

    ; updating progress bar...
    $percentage = Round(($i / $totalNrOfFiles) * 100)
    ProgressSet($percentage, $percentage & "% done...", "Working on: " & $uFile[$i])

    Sleep(1000) ; for demo purposes :)

    If StringRegExp($uFile[$i], ".*-KB[0-9]+-.*") Then
        ; Do install thingies
    Else
        ; Do nothing
        ; This else block can be omitted if you want
    EndIf
Next

ProgressOff()

Exit

@4: You can use FileOpen() to open two different files in append mode. You can then FileWrite() to one handle in case of success, and to the other in case of an error. See the help files for examples. Not sure what you consider success and what you consider error, hard to give a good example.

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...