Overblog Tous les blogs Top blogs Technologie & Science Tous les blogs Technologie & Science
Editer l'article Suivre ce blog Administration + Créer mon blog
MENU

script to make bootstrap.ini universal

Publié le par damcuvelier

On error resume Next
Dim WshShell, fso, ControlPath, Source
Dim scriptpath, ScriptName, ScriptFullName, DeployFldr, MDTDSshareName, BS
Dim UserDomain, UserID, UserPassword, cred, CentralDEPLOYROOT, DSCentral
dim LocalMDTSRV

Set WshShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

' ############################## VARIABLES ##############################
LocalMDTSRV = ""
scriptpath = WshShell.CurrentDirectory
ScriptName = WScript.ScriptName
ScriptFullName = WScript.ScriptFullName
BS = "X:\Deploy\Scripts\BootStrap.ini"
DeployFldr = fso.GetParentFolderName(scriptpath)

' ############################## MAIN ##############################

UserDomain = getval("UserDomain",BS) 
UserID = getval("UserID",BS) 
UserPassword = getval("UserPassword",BS) 
cred = UserDomain & "\" & UserID
CentralDEPLOYROOT = getval("DEPLOYROOT",BS)
DSCentral = split(CentralDEPLOYROOT,"\")(2)

OSDMETHOD = getMETHOD

if (OSDMETHOD = "MEDIA") then
' Ecrire à la place de la lignes contenant #MDTSRV# : "DEPLOYROOT = " & deployRoot:
replaceline()

' Puis effacer toutes les lignes contenants MDTSRV:
cleanfile()


else
' la suite du script readmdtsrv.vbS :


UserDomain = getval("UserDomain",BS) 
UserID = getval("UserID",BS) 
UserPassword = getval("UserPassword",BS) 
cred = UserDomain & "\" & UserID
Wshshell.run "net use U: " & CentralDEPLOYROOT & " /user:" & cred & " " & UserPassword,0,true
Wshshell.run "net use",0,false
WScript.Sleep 5000
NEWBS = updtBS(BS)
WScript.Sleep 5000

end if

' ############################## FUNCTIONS ##############################
Dim ControlPath, Source
ControlPath = fso.GetParentFolderName(scriptpath) & "\Control"

If fso.FolderExists(ControlPath) Then
Source=BootMedia
bootstrapmedia(BS)
else
Source=MDT

End If

function bootstrapmedia(BS)
Set objFile = objFSO.OpenTextFile(BS, ForWriting)
objFile.WriteLine "[Settings]"
objFile.WriteLine "Priority=Default"
objFile.WriteLine " "
objFile.WriteLine "[Default]"
objFile.Close
end function

function getval(variable,filevar)
    Set objFileToRead = fso.OpenTextFile(filevar,1)
    Dim strLine
        do while not objFileToRead.AtEndOfStream
        strLine = objFileToRead.ReadLine()
            If instr(lcase(strLine),lcase(variable)) > 0 Then
            getval = split(strLine,"=")(1)
            end if
        loop
    objFileToRead.Close
    Set objFileToRead = Nothing
end function

function getipadress()
    strQuery = "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE MACAddress > ''"

    Set objWMIService = GetObject( "winmgmts://./root/CIMV2" )
    Set colItems      = objWMIService.ExecQuery( strQuery, "WQL", 48 )

    For Each objItem In colItems
        If IsArray( objItem.IPAddress ) Then
            If UBound( objItem.IPAddress ) = 0 Then
                strIPs = objItem.IPAddress(0)
            Else
                strIPs = Join( objItem.IPAddress, "," )
            End If
        End If
    Next

    For Each strIP In split(strIPs,",")
    if instr(strIP,".") > 1 Then
    getipadress = strIP
    end if
    Next
end function

function NewMDTSRV()
    dim item, Ipscope, Result
    ' NewMDTSRV = "N/A"
    item = getipadress()
    Ipscope = split(item,".")(0) & "." & split(item,".")(1) & "." & split(item,".")(2)
    NewMDTSRV = getval(Ipscope, "U:\Tools\OSDConfig\MDTSRVs.txt")
end function

function updtBS(BS)
    ' Const ForReading = 1
    ' Const ForWriting = 2

    Dim objFSO,strBS,strSearch,strReplace,objFile,oldContent,newContent,strpw,strText,strNewText,f,i,strline
    dim TMPBS

    LocalMDTSRV = NewMDTSRV()
    ' if LocalMDTSRV = "N/A" Then
    ' WScript.Quit 0
    ' end if


    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set f = objFSO.OpenTextFile(BS)
    For i = 1 to 0
        f.ReadLine
    Next
    strLine = f.ReadLine
    f.Close

    Set openBS = objFSO.OpenTextFile(BS)
    strText = openBS.ReadAll
    openBS.Close

    ' STRNEWTEXT = REPLACE(STRTEXT, "%MDTSRV%", LOCALMDTSRV)
    STRNEWTEXT = REPLACE(STRTEXT, "#MDTSRV#", LOCALMDTSRV)
    Set objFile = objFSO.OpenTextFile(BS, ForWriting)
    objFile.WriteLine strNewText
    objFile.Close

    updtBS = LocalMDTSRV
end function

function checkprincipalMDT
Dim hostname
hostname = DSCentral
Set WshShell = WScript.CreateObject("WScript.Shell")
Ping = WshShell.Run("ping -n 1 " & hostname, 0, True)
    if Ping = 1 then
    checkprincipalMDT = "MEDIA"
       ' WScript.Echo "The principal MDT server '" & hostname & "' is unreachable. OSD impossible. Stop OSD"
       ' Wshshell.run "shutdown -s -t 00",0,true
    else
    checkprincipalMDT = "UNC"
    End if
end function

function getMETHOD
dim localRoot
Set drives = objFSO.Drives
For Each drive In drives
    If UCase(drive.DriveLetter) <> "X" Then
        letter = drive.DriveLetter & ":"
        If objFSO.FolderExists(letter & "\deploy") Then
        localRoot = letter & "\deploy"
        exit for
        end if
    End If
Next

wscript.echo "localRoot = " & localRoot

if localRoot <> "" Then
getMETHOD = "MEDIA"
else
getMETHOD = checkprincipalMDT
end if

end function


function replaceline()
strFilename = "X:\deploy\scripts\bootstrap.ini"
strFind = "#MDTSRV#"
strReplace = "DEPLOYROOT=" & deployRoot

Set objFile = objFSO.OpenTextFile(strFilename, ForReading)

strContent = ""
blnMatchFound = False

Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
    If InStr(strLine, strFind) > 0 Then
        strLine = strReplace
        blnMatchFound = True
    End If
    strContent = strContent & strLine & vbCrLf
Loop

objFile.Close

If blnMatchFound Then
    Set objFile = objFSO.OpenTextFile(strFilename, ForWriting)
    objFile.Write strContent
    objFile.Close
End If
end function


function cleanfile()
dim command, strInputFile, strOutputFile
' Chemin d'accès au fichier d'entrée et de sortie
strInputFile = "X:\deploy\scripts\bootstrap.ini"
strOutputFile = "X:\deploy\scripts\bootstrap_tmp.ini"

' Ouvrir le fichier d'entrée en lecture
Set objInputFile = objFSO.OpenTextFile(strInputFile, ForReading)

' Créer le fichier de sortie en écriture
Set objOutputFile = objFSO.CreateTextFile(strOutputFile, True)

' Parcourir toutes les lignes du fichier d'entrée
Do Until objInputFile.AtEndOfStream
    ' Lire la ligne courante
    strLine = objInputFile.ReadLine

    ' Vérifier si la ligne ne contient pas le mot "toto"
    If InStr(strLine, "MDTSRV") = 0 Then
        ' Si la ligne ne contient pas le mot "toto", l'écrire dans le fichier de sortie
        objOutputFile.WriteLine strLine
    End If
Loop

' Fermer les fichiers
objInputFile.Close
objOutputFile.Close

objFSO.CopyFile strOutputFile, strInputFile, True
objFSO.DeleteFile(strOutputFile)

end function

Commenter cet article