|
Option Explicit
'Class instance variable declarations
Public sDATADriveLetterVar, NewDVDLetterVar, oDVDLetterVar, oDVDVolumeVar, oTSEnv
Dim iRetVal, iPartitionCount, sSystemDrive
Dim WshShell, oEnvironment, oFSO, here
Set WshShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oTSEnv = CreateObject("Microsoft.SMS.TSEnvironment")
Set oEnvironment = WshShell.Environment("PROCESS")
here=left(WScript.ScriptFullName,len(WScript.ScriptFullName)-len(WScript.ScriptName)-1)
sSystemDrive = ucase(mid(oEnvironment("WINDIR"),1, 2))
OSDNewDVDLetterVar = oTSEnv("OSDNewDVDLetterVar")
OSDDATADriveLetter = oTSEnv("OSDDATADriveLetter")
' Read Task Sequence Variable for new DVD Letter and for DATA Drive Letter
if isnull(OSDNewDVDLetterVar)=true or OSDNewDVDLetterVar="" then
NewDVDLetterVar = "E"
else
NewDVDLetterVar = OSDNewDVDLetterVar
end if
if isnull(OSDDATADriveLetter)=true or OSDDATADriveLetter="" then
sDATADriveLetterVar = "D"
else
NewDVDLetterVar = OSDDATADriveLetter
end if
' Detect DVD Volume Letter
DetectDVD()
'if the DVD letter is equal to the new data drive letter we want:
if oDVDLetterVar = sDATADriveLetterVar then
'Assign a new letter "NewDVDLetterVar" to DVD Volume
AssignDVDletter()
end if
' Defrag System Drive
iRetVal = DefragDrive(sSystemDrive)
' Shrink SystemDrive to create DATA Drive with "sDATADriveLetterVar"
iRetVal = ShrinkDrive(sSystemDrive,sDATADriveLetterVar)
' DefragDrive SystemDrive
Function DefragDrive(sDrive)
Dim iRetVal,sCMDString
iRetVal = "Success"
If oFSO.FileExists(oEnvironment("SystemRoot") & "\system32\defrag.exe") then
sCmdString = "cmd /c " & oEnvironment("SystemRoot") & "\system32\defrag.exe " & sDrive
ElseIf oFSO.FileExists(oEnvironment("SystemRoot") & "\sysnative\defrag.exe") then
sCmdString = "cmd /c " & oEnvironment("SystemRoot") & "\sysnative\defrag.exe " & sDrive
Else
sCmdString = "cmd /c DEFRAG.EXE " & sDrive
End if
DefragDrive = iRetVal
End Function
' Shrink SystemDrive to Create Data Drive
Function ShrinkDrive(sDrive,sDATADrive)
Dim iRetVal, oDiskpartFile,sDiskPartFile
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
iRetVal = "Success"
sDiskPartFile = here & "\DATAShrinkDiskPart.txt"
Set oDiskPartFile = oFSO.CreateTextFile(sDiskPartFile, True, False)
oDiskPartFile.WriteLine "Select Vol " & sDrive
oDiskPartFile.WriteLine "Shrink DESIRED = 40000 MINIMUM = 10000"
oDiskpartFile.WriteLine "Create Partition Primary"
oDiskPartFile.WriteLine "Format fs=ntfs LABEL=" & chr(34) & "DATADrive" & chr(34) & " quick"
oDiskpartFile.WriteLIne "Assign Letter=" & sDATADrive
oDiskPartFile.Close
' Execute diskpart.exe
iRetVal = WshShell.Run("cmd /c DISKPART.EXE /s " & chr(34) & sDiskPartFile & chr(34), 0, true)
ShrinkDrive = iRetVal
End Function
Sub DetectDVD()
Dim sDiskPartDVDFile, oDiskPartDVDFile
Dim iRetValDVD,sCmd
Dim oCheck, Line
sDiskPartDVDFile = here & "\dectectDVDPart.txt"
Set oDiskPartDVDFile = oFSO.CreateTextFile(sDiskPartDVDFile, True, False)
oDiskPartDVDFile.WriteLine "list volume"
oDiskPartDVDFile.Close
sCmd = "%comspec% /c diskpart /s " & chr(34) & sDiskPartDVDFile & chr(34)
set iRetValDVD = WshShell.Exec(sCmd)
oCheck = iRetValDVD.StdOut.ReadAll
For Each Line In Split(oCheck,vbCrLf)
if trim(Line)="" _
or ubound(split(ucase(Line),ucase("volume"))) < 1 then
else
If instr(Line,"DVD")>0 then
oDVDVolumeVar=Trim(left(Line,len("volume D "))
oDVDLetterVar=Trim(mid(Line,1,len("Volume 2 C")))
end if
end if
Next
End Sub
Sub AssignDVDletter()
Dim sDiskPartDVDletFile, oDiskPartDVDletFile
Dim iRetDVD, sCmdValDVD
sDiskPartDVDletFile = here & "\DVDPart.txt"
Set oDiskPartDVDletFile = oFSO.CreateTextFile(sDiskPartDVDletFile, True, False)
oDiskPartDVDletFile.WriteLine "select " & oDVDVolumeVar
oDiskPartDVDletFile.WriteLine "Assign letter = " & NewDVDLetterVar
oDiskPartDVDletFile.Close
sCmdValDVD = "%comspec% /c diskpart /s " & chr(34) & sDiskPartDVDletFile & chr(34)
set iRetDVD = WshShell.Exec(sCmdValDVD)
End Sub
'If an usb or firewire drive could be connected when the script is running, add a function like this:
oDiskPartDVDletFile.WriteLine "select volume " & NewDVDLetterVar
oDiskPartDVDletFile.WriteLine "Remove letter = " & NewDVDLetterVar
|
Commenter cet article