[Batch] Automated Integrate SP1 in Windows 7 Ultimate N-1

Discussion in 'Windows 7' started by Wisdown, Feb 27, 2015.

  1. Wisdown

    Wisdown MDL Novice

    Feb 11, 2015
    10
    1
    0
    #1 Wisdown, Feb 27, 2015
    Last edited by a moderator: Apr 20, 2017
    Hey Guys,

    I'm coding an script to try make an automated integration of Windows 7 SP1 using DISM in Windows N-1 image.

    On my tests, the code writed is already working, but, since I'm not familiar using VB Script, I'm wanna know If is possible you guys guide me to code it better.

    My How To source is this:

    Edited
    (Can't post links because low post count)

    My initial setup was using .bat files, but, since I don't know how to find and replace strings / lines in texts files using .bat files, I have splited the process in 3 steps.

    First Step
    Extract the SP1:

    Code:
    @ECHO OFF
    
    REM Check If TMP folder exists and create it if necessary
        IF NOT EXIST C:\Deploy\SP (
            MD C:\Deploy\SP
            MD C:\Deploy\SP\x64
            MD C:\Deploy\SP\x64\TMP
            MD C:\Deploy\SP\x86
            MD C:\Deploy\SP\x86\TMP
            )
            
    REM Extract the Windows 7 Service Pack 1 64-bit to C:\Deploy\SP\x64\TMP
    C:\Deploy\Updates\x64\"Windows 7 Service Pack 1 for x64-based Systems (KB976932)"\AMD64-all-windows6.1-kb976932-x64_74865ef2562006e51d7f9333b4a8d45b7a749dab.exe /X:C:\Deploy\SP\x64\TMP
    
    REM Extract the Windows 7 Service Pack 1 32-bit to C:\Deploy\SP\x86\TMP
    C:\Deploy\Updates\x86\"Windows 7 Service Pack 1 (KB976932)"\X86-all-windows6.1-kb976932-x86_c3516bc5c9e69fee6d9ac4f981f5b95977a8a2fa.exe /X:C:\Deploy\SP\x86\TMP
    
    REM Move the main file from TMP folder to next step of extraction
    MOVE /Y C:\Deploy\SP\x64\TMP\windows6.1-KB976932-X64.cab C:\Deploy\SP\x64\windows6.1-KB976932-X64.cab
    MOVE /Y C:\Deploy\SP\x86\TMP\windows6.1-KB976932-X86.cab C:\Deploy\SP\x86\windows6.1-KB976932-X86.cab
    
    REM Extract .CAB Files
    expand C:\Deploy\SP\x64\windows6.1-KB976932-X64.cab -F:* C:\Deploy\SP\x64\
    expand C:\Deploy\SP\x86\windows6.1-KB976932-X86.cab -F:* C:\Deploy\SP\x86\
    
    REM Remove unecessary files after previous extraction
    DEL /F C:\Deploy\SP\x64\windows6.1-KB976932-X64.cab
    DEL /F C:\Deploy\SP\x86\windows6.1-KB976932-X86.cab
    
    REM Extract extras files: KB976933-LangsCab0.cab KB976933-LangsCab1.cab KB976933-LangsCab2.cab KB976933-LangsCab3.cab KB976933-LangsCab4.cab KB976933-LangsCab5.cab KB976933-LangsCab6.cab
    REM To enable offline integration by DISM
    
    expand C:\Deploy\SP\x64\KB976933-LangsCab0.cab -F:* C:\Deploy\SP\x64\
    expand C:\Deploy\SP\x64\KB976933-LangsCab1.cab -F:* C:\Deploy\SP\x64\
    expand C:\Deploy\SP\x64\KB976933-LangsCab2.cab -F:* C:\Deploy\SP\x64\
    expand C:\Deploy\SP\x64\KB976933-LangsCab3.cab -F:* C:\Deploy\SP\x64\
    expand C:\Deploy\SP\x64\KB976933-LangsCab4.cab -F:* C:\Deploy\SP\x64\
    expand C:\Deploy\SP\x64\KB976933-LangsCab5.cab -F:* C:\Deploy\SP\x64\
    expand C:\Deploy\SP\x64\KB976933-LangsCab6.cab -F:* C:\Deploy\SP\x64\
    
    expand C:\Deploy\SP\x64\NestedMPPContent.cab -F:* C:\Deploy\SP\x64\
    
    expand C:\Deploy\SP\x86\KB976933-LangsCab0.cab -F:* C:\Deploy\SP\x86\
    expand C:\Deploy\SP\x86\KB976933-LangsCab1.cab -F:* C:\Deploy\SP\x86\
    expand C:\Deploy\SP\x86\KB976933-LangsCab2.cab -F:* C:\Deploy\SP\x86\
    expand C:\Deploy\SP\x86\KB976933-LangsCab3.cab -F:* C:\Deploy\SP\x86\
    expand C:\Deploy\SP\x86\KB976933-LangsCab4.cab -F:* C:\Deploy\SP\x86\
    expand C:\Deploy\SP\x86\KB976933-LangsCab5.cab -F:* C:\Deploy\SP\x86\
    expand C:\Deploy\SP\x86\KB976933-LangsCab6.cab -F:* C:\Deploy\SP\x86\
    
    expand C:\Deploy\SP\x86\NestedMPPContent.cab -F:* C:\Deploy\SP\x86\
    
    REM Remove unecessary files after previous extraction
    
    DEL /F C:\Deploy\SP\x64\KB976933-LangsCab0.cab
    DEL /F C:\Deploy\SP\x64\KB976933-LangsCab1.cab
    DEL /F C:\Deploy\SP\x64\KB976933-LangsCab2.cab
    DEL /F C:\Deploy\SP\x64\KB976933-LangsCab3.cab
    DEL /F C:\Deploy\SP\x64\KB976933-LangsCab4.cab
    DEL /F C:\Deploy\SP\x64\KB976933-LangsCab5.cab
    DEL /F C:\Deploy\SP\x64\KB976933-LangsCab6.cab
    
    DEL /F C:\Deploy\SP\x64\NestedMPPContent.cab
    
    DEL /F C:\Deploy\SP\x86\KB976933-LangsCab0.cab
    DEL /F C:\Deploy\SP\x86\KB976933-LangsCab1.cab
    DEL /F C:\Deploy\SP\x86\KB976933-LangsCab2.cab
    DEL /F C:\Deploy\SP\x86\KB976933-LangsCab3.cab
    DEL /F C:\Deploy\SP\x86\KB976933-LangsCab4.cab
    DEL /F C:\Deploy\SP\x86\KB976933-LangsCab5.cab
    DEL /F C:\Deploy\SP\x86\KB976933-LangsCab6.cab
    
    DEL /F C:\Deploy\SP\x86\NestedMPPContent.cab
    
    CALL C:\Deploy\Scripts\Windows\02-03-SP1_Replace_Text.vbs
    Second Step
    Since I don't know how to find and replace stuff in text files is use an VBS for do it:

    Code:
    ' Set Windows 7 SP1 to be integrated with DISM
    
    Const ForReading = 1
    Const ForWriting = 2
    
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("C:\Deploy\SP\x64\Windows7SP1-KB976933~31bf3856ad364e35~amd64~~6.1.1.17514.mum",ForReading)
    
    strText = objFile.ReadAll
    objFile.Close
    ' Replace The Line to allow integrate Windows 7 SP1 Offline
    strNewText = Replace(strText, "allowedOffline=""false""", "allowedOffline=""true""")
    
    Set objFile = objFSO.OpenTextFile("C:\Deploy\SP\x64\Windows7SP1-KB976933~31bf3856ad364e35~amd64~~6.1.1.17514.mum",ForWriting)
    objFile.WriteLine strNewText
    
    objFile.Close
    Set objFile = nothing
    
    
    
    Set objFile = objFSO.OpenTextFile("C:\Deploy\SP\x64\update.mum",ForReading)
    
    strText = objFile.ReadAll
    objFile.Close
    ' Replace The Line to allow integrate Windows 7 SP1 Offline
    strNewText = Replace(strText, "allowedOffline=""false""", "allowedOffline=""true""")
    
    Set objFile = objFSO.OpenTextFile("C:\Deploy\SP\x64\update.mum",ForWriting)
    objFile.WriteLine strNewText
    
    objFile.Close
    Set objFile = nothing
    
    
    
    ' Open the Old File for Reading
    Set objFile = objFSO.OpenTextFile("C:\Deploy\SP\x64\update.ses",ForReading)
    
    OldFile = "C:\Deploy\SP\x64\update.ses"
    NewFile = "C:\Deploy\SP\x64\TMP\update.ses"
    
    'Set the new Temporary File
    IF objFSO.FileExists(NewFile) Then
        objFSO.DeleteFile NewFile
    End IF
    
    Set objFileT = objFSO.CreateTextFile("C:\Deploy\SP\x64\TMP\update.ses",True,TristateTrue)
    
    Do Until objFile.AtEndOfStream
        linenum = objFile.Line
        strLine = objFile.ReadLine
        ' Replace The Line to allow integrate Windows 7 SP1 Offline
        If linenum = 18 Then
            strLine = "            <package id=""Windows7SP1-KB976933~31bf3856ad364e35~amd64~~6.1.1.17514"" targetState=""Installed""/>"
        End If
    
        objFileT.WriteLine strLine
        
        'Wscript.Echo strLine
    Loop
    
    objFile.Close
    objFileT.Close
    
    objFSO.copyFile NewFile, OldFile, TRUE
    
    Set objFile = nothing
    Set objFileT = nothing
    Set OldFile = nothing
    Set NewFile = nothing
    
    
    
    Set objFile = objFSO.OpenTextFile("C:\Deploy\SP\x86\Windows7SP1-KB976933~31bf3856ad364e35~x86~~6.1.1.17514.mum",ForReading)
    
    strText = objFile.ReadAll
    objFile.Close
    ' Replace The Line to allow integrate Windows 7 SP1 Offline
    strNewText = Replace(strText, "allowedOffline=""false""", "allowedOffline=""true""")
    
    Set objFile = objFSO.OpenTextFile("C:\Deploy\SP\x86\Windows7SP1-KB976933~31bf3856ad364e35~x86~~6.1.1.17514.mum",ForWriting)
    objFile.WriteLine strNewText
    
    objFile.Close
    Set objFile = nothing
    
    
    
    Set objFile = objFSO.OpenTextFile("C:\Deploy\SP\x86\update.mum",ForReading)
    
    strText = objFile.ReadAll
    objFile.Close
    ' Replace The Line to allow integrate Windows 7 SP1 Offline
    strNewText = Replace(strText, "allowedOffline=""false""", "allowedOffline=""true""")
    
    Set objFile = objFSO.OpenTextFile("C:\Deploy\SP\x86\update.mum",ForWriting)
    objFile.WriteLine strNewText
    
    objFile.Close
    Set objFile = nothing
    
    
    
    ' Open the Old File for Reading
    Set objFile = objFSO.OpenTextFile("C:\Deploy\SP\x86\update.ses",ForReading)
    
    OldFile = "C:\Deploy\SP\x86\update.ses"
    NewFile = "C:\Deploy\SP\x86\TMP\update.ses"
    
    'Set the new Temporary File
    IF objFSO.FileExists(NewFile) Then
        objFSO.DeleteFile NewFile
    End IF
    
    Set objFileT = objFSO.CreateTextFile("C:\Deploy\SP\x86\TMP\update.ses",True,TristateTrue)
    
    Do Until objFile.AtEndOfStream
        linenum = objFile.Line
        strLine = objFile.ReadLine
        ' Replace The Line to allow integrate Windows 7 SP1 Offline
        If linenum = 18 Then
            strLine = "            <package id=""Windows7SP1-KB976933~31bf3856ad364e35~amd64~~6.1.1.17514"" targetState=""Installed""/>"
        End If
    
        objFileT.WriteLine strLine
        
        'Wscript.Echo strLine
    Loop
    
    objFile.Close
    objFileT.Close
    
    objFSO.copyFile NewFile, OldFile, TRUE
    
    Set objFile = nothing
    Set objFileT = nothing
    Set OldFile = nothing
    Set NewFile = nothing
    
    
    
    Set shell = CreateObject("Wscript.shell")
    shell.run "C:\Deploy\Scripts\Windows\03-03-SP1_Apply_SP1.bat"
    set shell = nothing
    
    Wscript.Quit
    Setp 03
    Working on it yet, is call dism and apply the SP1:

    Code:
    @ECHO OFF
    
    REM Clear TMP DIR
    RD /S /Q C:\Deploy\SP\x64\TMP
    RD /S /Q C:\Deploy\SP\x86\TMP
    My main concerns is about the Step 2, for example, I'm replacing "linenum = 18" instead real "linenum = 34" because for an unknow reason (din't understand why) the script don't read blank lines.

    Thanks in Advice.