Jump to content

mod txt file with vbs .. noob needs help


Recommended Posts

hi guys,

i have a lill problem at work. i need to deploy a script to 1000 clients which puts the computername into the bios.

therefor i found a bios utility for the hp machines we use that writes the bios values to a txt file ... now i just have to search and replace the the right line in the file ....

as i have not much expirience with vb maybe some of you guys can help ... i managed to cutnpaste some script that does the trick .... but it has a lill flaw .. look :

thats a part of the bios txt i have :

.....
Cache Size (L1/L2)
20/128 KB
Memory Size
512 MB
System BIOS
786B2 v1.10
Chassis Serial Number
FRB34702RS
Asset Tracking Number
FRB34702RS
Removable Media Boot
*Enable
Disable
....

the field i need to change is the 'asset tracking number' ... my scripts read the bios values via wmi and make a search / change for each line in the file ....

to bad that on new machines the serial number and the asset tag are the same ... so it changes both values ...

maybe some1 can give me a hint in the right direction ?

heres the script i dirty cutnpasted ;)

strComputer = "."
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSMBIOS = objWMIService.ExecQuery _
   ("SELECT * FROM Win32_SystemEnclosure")
For Each objSMBIOS in colSMBIOS
 Set fso = CreateObject("Scripting.FileSystemObject")
 SourceFile="bios.cur"
 DestFile="bios.new"
 Set MyFile = fso.OpenTextFile(SourceFile, 1)
 Set NewFile = fso.OpenTextFile(DestFile, 2,True)
 do while MyFile.AtEndOfStream <> True
   Zeile = MyFile.ReadLine
   wrtstrg=Replace(Zeile, objSMBIOS.SMBIOSAssetTag, WshNetwork.ComputerName)
   NewFile.WriteLine wrtStrg
 loop
Next

thx a lot for your attention

mason

Link to comment
Share on other sites


strComputer = "."
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:" _
  & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSMBIOS = objWMIService.ExecQuery _
  ("SELECT * FROM Win32_SystemEnclosure")
For Each objSMBIOS in colSMBIOS
Set fso = CreateObject("Scripting.FileSystemObject")
SourceFile="bios.cur"
DestFile="bios.new"
Set MyFile = fso.OpenTextFile(SourceFile, 1)
Set NewFile = fso.OpenTextFile(DestFile, 2,True)

status = 0

do while MyFile.AtEndOfStream <> True
  Zeile = MyFile.ReadLine

  Select Case vbTrue
    Case InStr(UCase(Zeile), "ASSET TRACKING NUMBER") <> 0
      status = 1
    Case status = 1
      wrtstrg=Replace(Zeile, objSMBIOS.SMBIOSAssetTag, WshNetwork.ComputerName)
      status = 0
  End Select

  NewFile.WriteLine wrtStrg
loop
Next

When a line containing "asset tracking number" is found,

status changes from "0" to "1", thus indicating the NEXT

line has to be modified. After mod has taken place, status

gets reset, so no other line will be changed.

HTH

Andreas

Edited by ack-hh
Link to comment
Share on other sites

hey ack-hh ... thx a lot for your effort ... highly appreciated :)

but when i use your code ... it produces a new file just with the asset number in a row ...

ill attach the original file and the file your script generated ... maybe you can have another look? would be great .... thx bud!

bios.zip

Link to comment
Share on other sites

At quick glance it just looks like the "normal" condition was omitted. Maybe something like this... If it finds the "ASSET" tag it echos the line to output file, reads the next line and writes the replacement. If the tag is not found it just echos the current line.

do while MyFile.AtEndOfStream <> True
  Zeile = MyFile.ReadLine
  if InStr(UCase(Zeile), "ASSET TRACKING NUMBER") <> 0 then
     NewFile.WriteLine zeile
     Zeile = MyFile.ReadLine
     NewFile.WriteLine Replace(Zeile, objSMBIOS.SMBIOSAssetTag, WshNetwork.ComputerName)
  else
     NewFile.WriteLine zeile
  end if
loop

and really, you don't even have to worry about the replace() function since you are just swapping one line for another. It could just be

do while MyFile.AtEndOfStream <> True
  Zeile = MyFile.ReadLine
  if InStr(UCase(Zeile), "ASSET TRACKING NUMBER") <> 0 then
     NewFile.WriteLine zeile
     Zeile = MyFile.ReadLine
     NewFile.WriteLine WshNetwork.ComputerName
  else
     NewFile.WriteLine zeile
  end if
loop

Edited by dman
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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...