Jump to content

Need script to modify sysprep.inf


Recommended Posts

I have been trying to figure out a way to do this 25 machine deployment and I've resorted to using an Install Product key for the installation, running some post installation stuff, then running SysPrep. I know you can pre-fill Machine name and Product Key in the sysprep.inf, but I need to have one of my post installations bat files run a script that will Retrieve the current machines UUID or even MAC ID, look that up in a database (in the Sysprep Directory of current machine) and then modify (add) the appropriate Machine name & Prodcut key entries in the sysprep.inf file.

After my post installation reboot, I then want it to run Sysprep and that should take care of it..

Unfortunately I don't know enough on scripting to do this. I'm more of a .cmd/.bat person

Link to comment
Share on other sites


I didn't understand completelly, but I get this conclusion:

UUID varies across same computer, unreliable.

ipconfig /all gave MAC Adress from machine NIC.

A database consisting of sysprep.inf files on subdirectories like XX-XX-XX-XX-XX-XX\sysprep.inf

One subdir per MAC address

If the media is writeable the database may consist of 25 [MAC HERE].inf files and rename the coincident to sysprep.inf

I'm unaware of sysprep flow but have good batch skills, for help you further let me know, step by step, what you did manually.

Link to comment
Share on other sites

Hold on... I found myself unsusable with this task (.net :(), however I am talking with one friend right now about this. It should be more universal utility:

GetVar.exe

Search.txt

mac, ip, pcname, username
-------------------------------------
000BDB8743BE,172.16.1.38,pc1,mzugec

Now you will be able to use getvar with following syntax:

GetVar -set mac=000BDB8743BE -get username, pcname

It will create two variables:

getvarUsername = mzugec

getvarPCName = pc1

Link to comment
Share on other sites

Actually, username "companyuser" stays the same on each machine.

What I want to do is before sysprep is ran, to have the appropriate sysprep.inf file in place. By appropriate, I mean that each machine has it's own unique MAC ID and UUID and each machine has a Product Key Sticker. So the database would be like:

000BDB8743BE,Machine-05,1234-XXXX-5678-XXXXX-91011

000BC56EFA01,Machine-09,5555-XXXX-66666-77777-XXXX (MAC ID, Machine name, Product key)

The script would query the machine's MAC ID or UUID (doesn't matter to me), look that up in the database and either modify entries in sysprep.inf or copy 1 of 25 different sysprep.inf files to the sysprep folder. I am not concerned with username as that I have hardcoded in winlogon.

Link to comment
Share on other sites

Well, I was able to come up with the second half myself using the following code:

@ECHO OFF

TITLE Sysprep Customizer

SET C1=GATEWAY-06

SET P1=12345-ABCDE-12345-ABCDE-12345

ECHO.

ECHO Editing Sysprep.inf

ECHO.

SET T1=Fedit -add -once -f Sysprep.inf -s UserData

%T1% ComputerName=%C1%

%T1% ProductKey=%P1%

ECHO [userData] section completed...

EXIT

I'm using the Fedit utility I found in BTS Driver Pack. Of course in this example, the variable C1 & P1 are hard coded. Ultimately %C1% should be a variable pulled from the database as well as %P1%. I don't know a command line way of pulling the current PC's MAC ID into a variable. IPCONFIG /ALL tells me the MAC ID as well as a bunch of other info, but I don't know how that can be usefull in the whole scheme of this..

Link to comment
Share on other sites

No I don't have to do it from DOS, as it would be running from within the Windows environment (during RunOnce).

I just discovered also, that I actually can make use of IPCONFIG /ALL by using: IPCONIFG /ALL >C:\Sysprep\netdata.txt which outputs the information to the text file netdata.txt which happens to contain the 'Physical Address' (MAC ID). Now, a nice little script Possible usage of this one should be able to extract just the MAC ID from that text file and store it in a variable. Then lookup that variable for a match in the "database".

BTW, the NIC's on all these machines are Marvell Yukon and all start with a MAC ID: 00 E0 B8 80 XX XX, so I may only be interested in looking at the last two bytes.

So possible database format: (Last two bytes of Mac ID, Machine Name, Product Key) 58-DB,Gateway-06,12345-67890-AABBC-DDEEF-00000

57-15,Gateway-05,00000-11111-AAAAA-12345-889B, and so on....

Link to comment
Share on other sites

Mac.bat

for /f "usebackq delims=," %%i IN (`getmac /fo csv /nh`) do (
For /f "usebackq delims=, tokens=1,2" %%a IN (`type mac.txt`) DO If %%i EQU %%a Set strComputername=%%b
)
Echo %strComputername%

Mac.txt

"00-0B-DB-87-43-BE",pc1
"00-0B-DB-87-43-BF",pc2
"00-0B-DB-87-43-BD",pc3

Link to comment
Share on other sites

Try this...

GetMAC.vbs

On Error Resume Next

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Const ForReading = 1

Dim fs
Dim sh
Dim nw
Dim strMAC, strComp, strPID

Set sh = CreateObject("WScript.Shell")
Set fs = CreateObject("Scripting.FileSystemObject")
Set nw = CreateObject("WScript.Network")


strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_NetworkAdapter " _
       & "Where NetConnectionID = " & _
       "'Local Area Connection'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem in colItems
   strMACAddress = objItem.MACAddress
Next

'Open our text file and get the machine details
Set objFile = fs.OpenTextFile("Machines.txt", ForReading, false)

Do While objFile.AtEndOfStream <> True
If inStr(objFile.ReadLine, strMACAddress) Then
 colDetails = Split(objFile.ReadLine, ",")
 writeini "sysprep.ini", "UserData", "ComputerName", colDetails(1)
 writeini "sysprep.ini", "UserData", "ProductKey", colDetails(2)
Else
 objFile.Skipline
End If
Loop

objFile.Close


' READINI ( file, section, item )
' file = path and name of ini file
' section = [Section] must not be in brackets
' item = the variable to read

Function ReadIni(file, section, item)

ReadIni = ""
file = Trim(file)
item = Trim(item)
Set ini = fs.OpenTextFile( file, 1, False)

Do While ini.AtEndOfStream = False
 line = ini.ReadLine
 line = Trim(line)
 If LCase(line) = "[" & LCase(section) & "]" Then
 line = ini.ReadLine
 line = Trim(line)
 Do While Left( line, 1) <> "["
   'If InStr( 1, line, item & "=", 1) = 1 Then
   equalpos = InStr(1, line, "=", 1 )
   If equalpos > 0 Then
   leftstring = Left(line, equalpos - 1 )
   leftstring = Trim(leftstring)
   If LCase(leftstring) = LCase(item) Then
     ReadIni = Mid( line, equalpos + 1 )
     ReadIni = Trim(ReadIni)
     Exit Do
   End If
   End If

   If ini.AtEndOfStream Then Exit Do
   line = ini.ReadLine
   line = Trim(line)
 Loop
 Exit Do
 End If
Loop
ini.Close

End Function

' WRITEINI ( file, section, item, myvalue )
' file = path and name of ini file
' section = [Section] must not be in brackets
' item = the variable to write;
' myvalue = the myvalue to assign to the item.
'
Sub WriteIni( file, section, item, myvalue )

in_section = False
section_exists = False
item_exists = ( ReadIni( file, section, item ) <> "" )
wrote = False
file = Trim(file)
itemtrimmed = Trim(item)
myvalue = Trim(myvalue)

temp_ini = sh.ExpandEnvironmentStrings("%TEMP%\") & fs.GetTempName

Set read_ini = fs.OpenTextFile( file, 1, True, TristateFalse )
Set write_ini = fs.CreateTextFile( temp_ini, False)

While read_ini.AtEndOfStream = False
 line = read_ini.ReadLine
 linetrimmed = Trim(line)
 If wrote = False Then
 If LCase(line) = "[" & LCase(section) & "]" Then
   section_exists = True
   in_section = True
 ElseIf InStr( line, "[" ) = 1 Then
   in_section = False
 End If
 End If

 If in_section Then
 If item_exists = False Then
   write_ini.WriteLine line
   write_ini.WriteLine item & "=" & myvalue
   wrote = True
   in_section = False
 Else
   equalpos = InStr(1, line, "=", 1 )
   If equalpos > 0 Then
   leftstring = Left(line, equalpos - 1 )
   leftstring = Trim(leftstring)
   If LCase(leftstring) = LCase(item) Then
     write_ini.WriteLine itemtrimmed & "=" & myvalue
     wrote = True
     in_section = False
   End If
   End If
   If Not wrote Then
   write_ini.WriteLine line
   End If
 End If
 Else
 write_ini.WriteLine line
 End If
Wend

If section_exists = False Then ' section doesn't exist
 write_ini.WriteLine
 write_ini.WriteLine "[" & section & "]"
 write_ini.WriteLine itemtrimmed & "=" & myvalue
End If

read_ini.Close
write_ini.Close
       If fs.FileExists(file) then
           fs.DeleteFile file, True
       End if
       fs.CopyFile temp_ini, file, true
fs.DeleteFile temp_ini, True

End Sub

The Machines.txt looks like this:

Machines.txt

00:0F:1F:B4:88:EF,Machine-09,5555-XXXX-66666-77777-XXXX
00:0F:1F:B4:88:E8,Machine-04,5555-XXXX-66666-77777-XXXX

Link to comment
Share on other sites

Mac.bat

for /f "usebackq delims=," %%i IN (`getmac /fo csv /nh`) do (
For /f "usebackq delims=, tokens=1,2" %%a IN (`type mac.txt`) DO If %%i EQU %%a Set strComputername=%%b
)
Echo %strComputername%

Mac.txt

"00-0B-DB-87-43-BE",pc1
"00-0B-DB-87-43-BF",pc2
"00-0B-DB-87-43-BD",pc3

Wow Thank you Martin, I didn't know it could be done with just a few lines! That's great, but how can I expand that to also include Product Key and store that as %ProdKey% ? Here is a snipit of my Mac.txt file:

"00-0B-DB-87-43-BE",Gateway-01,11111-XXYXX-66666-77777-XXXUX

"00-0B-DB-87-43-BF",Gateway-02,55555-XXTXX-66666-77777-XXXTX

"00-0B-DB-87-43-BD",Gateway-03,11223-56789-00111-555AB-TKETK

"00-11-2F-C9-D8-30",Gateway-04,ABC12-MSPRO-XXXXX-USR55-KPWR1

Link to comment
Share on other sites

mac.bat

@ECHO OFF
REM Skip Sub(s)
GOTO :Start

REM Found Sub
:Found
SET strComputerName=%1
SET strProductKey=%2
GOTO :EOF

:Start
FOR /f "usebackq delims=," %%i IN (`getmac /fo csv /nh`) DO (
 FOR /f "usebackq delims=, tokens=1,2,3" %%a IN (`type mac.txt`) DO IF %%i EQU %%a CALL :Found %%b %%c
)
ECHO %strComputername% %strSerial%

mac.txt

"00-0B-DB-87-XX-XX",Gateway-01,11111-XXYXX-66666-77777-XXXUX
"00-0B-DB-87-XX-XX",Gateway-02,55555-XXTXX-66666-77777-XXXTX
"00-0B-DB-87-XX-XX",Gateway-03,11223-56789-00111-555AB-TKETK
"00-11-2F-C9-XX-XX",Gateway-04,ABC12-MSPRO-XXXXX-USR55-KPWR1

Note to the n00b unaware of what is going on: Add your own MAC addresss anywhere into mac.txt for testing purposes

Edited by Nilfred
Link to comment
Share on other sites

OK, Got it to work! Thank you both!

Now quick question, does this script have any dependencies as I tried running it on my friends laptop (added his MAC ID & PC Name, ProdKey to database) and it complained about 'getmac' saying that it wasn't an internal or external command or something like that.. Also, the laptop was XP Home in case that matters...

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...