Video Screencast Help

VBScript that imports all MAC addresses from the eXpress DB into the MAC filters for PXE

Created: 05 Sep 2006 • Updated: 05 Dec 2007 | 2 comments
Enrolado's picture
+2 2 Votes
Login to vote

The headline says it all. This is a sample script that can help you import all of the MAC addresses from your eXpress DB into the MAC filters for PXE.

'Function that overwrites a line to a file on a specific line
'************************************************************
Sub WriteToFile(strFilePath, strData, iLineNumber)
   Dim objFSO, objFile, arrLines
   Dim strAllFile, x
   Set objFSO=CreateObject("Scripting.FileSystemObject")
   strAllFile=""
   If objFSO.FileExists(strFilePath) Then
      Set objFile=objFSO.OpenTextFile(strFilePath)
      If Not(objFile.AtEndOfStream) Then
         strAllFile = objFile.ReadAll
      End If
      objFile.Close
   End If
   arrLines = Split(strAllFile, VBCrLf)
   Set objFile=objFSO.CreateTextFile(strFilePath)
   For x=0 To UBound(arrLines) 
      If ((iLineNumber-1)=x) Then
	 objFile.WriteLine(strData)
      Else objFile.WriteLine(arrLines(x))
      End If
   Next
   If iLineNumber>=UBound(arrLines) Then objFile.WriteLine(strData)
   objFile.Close
   Set objFile=Nothing
   Set objFSO=Nothing
End Sub 


'Stops PXE Manager Service and Waits 10 seconds
'**********************************************
strServiceName = "Altiris PXE Manager"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery("Select * from Win32_Service Where Name ='" & strServiceName & "'")
For Each objService in colListOfServices
    objService.StopService()
Next
WScript.Sleep("10000")


'Getting the list of MAC Addresses from the DB
'*********************************************
Dim SQLString, MACStr
SQLString = "select mac_addr from nics" 'This is the actual SQL query that will be run
ColumnName = "mac_addr" 'This is the comlumn name to look for
Const DB_CONNECT_STRING = "Altiris eXpress Database" 'Name of the ODBC connection
MACStr = "MacFilterAddresses="

Set MyDB = CreateObject("ADODB.Connection")
MyDB.Open(DB_CONNECT_STRING)

set queryResults = MyDB.Execute(SQLString)

If Not (queryResults.EOF And queryResults.EOF) Then
   While Not queryResults.EOF 'Loop through all Query Results
      GetMACAddr = queryResults.Fields(ColumnName)
      queryResults.MoveNext
      MACStr = MACStr & """" & GetMACAddr & """" & ","
   Wend
End if
MACStr = Left(MACStr, (Len(MACStr)-1))
MyDB.Close


'Getting the file path to the PXE Manager.ini file
'*************************************************
Dim WshShell, fileName
Set WshShell = WScript.CreateObject("WScript.Shell")
fileName = WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Altiris\Altiris eXpress\DataStorePath")
fileName = fileName & "\PXE\PXEManager.ini"


'Getting the file line where MAC filtering starts
'************************************************
Dim fso, f, ts
Dim CurrentLine, LineNum 
CurrentLine = ""
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(fileName)
Set ts = f.OpenAsTextStream(1, -2)
While CurrentLine <> "[PXEServer\Shared\MACFilter]" 
	CurrentLine = ts.ReadLine()
WEnd
LineNum = ts.Line
ts.Close


'Writing out changes to the PXEManager.ini file
'**********************************************
Call WriteToFile(FileName, "MacAddrFilterEnabled=True", LineNum)
Call WriteToFile(FileName, "MacAddrFilterType=0", LineNum+2)
Call WriteToFile(FileName, MACStr, LineNum+3)


'Starts PXE Manager service back up
'**********************************
strServiceName = "Altiris PXE Manager"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ='" & strServiceName & "'")
For Each objService in colListOfServices
    objService.StartService()
Next

Comments 2 CommentsJump to latest comment

Ives Ledegen's picture

This will come in handy.
Looks great. I will use it certainly.

Grtz,

Ives

0
Login to vote
yaodongxian's picture

I was in the Altiris Boot Camp last week, and as I remember this is the script we used in the training, well done! Just curious, are you the author of this script? Thanks!

Alt-Iris

0
Login to vote