# Thread: need VBS script to...

1. ## need VBS script to...

I have a VBS script that adds registry entries for each LAN computer at logon. In the script, one numeric value needs to be unique for each machine (can be unique for each user but that is not as important). I think the field can be up to 4 characters long.

I was hoping to add a random number generator or a number partially generated based on MAC address. Suggestions?

2. Will the script detect whether or not the reg entry has been set already, or is it assigning it a new number at every logon?

I have a whole bunch of ideas, but they're all overcomplicated. And I bet you don't want to involve a database.

3. lol, i'm trying for K.I.S.S.

No, the sript doesn't check for entries. The entries are set per user.

Actually I have a text file on a network share just for keeping track of this number but I didn't know how to increment it via VBS.

\\proxy2\paging$\install.ini [Client Setup] ClientNumber=8 If the VBS script can increment this number automatically that would be great! 4. You should be able to read the number via Open or something similar (FileSystemObject seems like overkill here). You have to write to the file somehow already to keep track of the number so that shouldn't be a problem. I haven't got my VBS hat on (not that much experience with it to be honest), but once you read in the number via Open, ReadLine, and CInt, you can just incriment with i = i + 1 or something then write it back. Set a boolean so it returns to 1 after the number hits 9999. Have I got what you're asking or am I on the wrong track going backwards? 5. You've got it right, I just don't know much VBS. But I have a book and I'm digging thru it right now using your terms. I may figure it out within the next 4 hours. 6. I found a section using FSO, is it really that much more overhead? 7. FSO is easier to use once you get it running, but there's a lot more variablenessness involved. If you use the standard Open command, you don't even have to set a variable. Open \\proxy2\paging$\install.ini as #1

is what it should be with Open.

Then just make a loop that does

Code:
sTest = ""
While Left(sTest, 14) <> "ClientNumber="
Wend
That'll get you sTest equal to the entire line, so then you need to strip the number off the end and convert it to an integer which is pretty standard.

Code:
iCNum = CInt(Right(sTest, 1))

Then to adjust by +1 with the testing boolean...

If iCNum = 9999 Then
iCNum = 1
Else
iCNum = iCNum + 1
End If
As I suddenly can't remember how to get it to write back to the correct line... How does it write it now? It doesn't write the entire file all at the same time, does it?

8. Well I copied and pasted...
open "\\proxy2\paging$\install.ini" as #1 sTest = "" While Left(sTest, 14) <> "ClientNumber=" sTest = ReadLine #1 Wend iCNum = CInt(Right(sTest, 1)) If iCNum = 9999 Then iCNum = 1 Else iCNum = iCNum + 1 End If into a file but received Expected end of statement at 1,37. (I wasn't joking, I really don't know VBS at all! ) 9. Also tried... open "\\proxy2\paging$\install.ini"

sTest = ""
While Left(sTest, 14) <> "ClientNumber="
Wend

iCNum = CInt(Right(sTest, 1))

If iCNum = 9999 Then
iCNum = 1
Else
iCNum = iCNum + 1
End If
but got (5, 22) Microsoft VBScript compilation error: Expected end of statement

10. I wonder if VBS doesn't support Open the same way that VB does. Where's Vassago when you need him eh? Let me dig a bit and see what I can find.

11. Okay, VBS doesn't actually support Open/Read. There has to be a way for it to read files, but looking at the function list, I'm not sure how it does it...

But it DOES support FileSystemObject.

How are you writing this thing? Just by hand, or are you using Visual Studio?

12. by hand (a lot of trial and error)

13. Well, you can use the FSO, but I don't know if it's something that VBS just knows or what. In VB you have to add the runtime dll to the project or it errors.

You can find a ton of info on how to use the FSO on the net.

It ends up being something like
Code:
Dim fso, oFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFile = fso.OpenTextFile("\\path\to\file.ini", 8, True)
Now you can use ReadLine to set variables and WriteLine("text") to write text to the file. Now, depending on how you open it, it either appends or replaces the original file. To append, the value in OpenTextFile should be 8, to overwrite, it should be 1.

What you could do (the only way >I< know how to do it) is to read the entire ini file into an array, then seach the array for the line you need to work on, change it in the array, then write the entire file back out from the array. Time consuming, but I don't remember there being a Find/Replace command for this.

14. Here's what I got so far which is incomplete and untested...
Const ForReading = 1, ForWriting = 2, For Appending = 8

DIM FSO, TS
Set FSO = CreateObject("Scripting. FileSystemObject")

Set TS = FSO.openAs TextStream("\\proxy2\paging$\install.ini", ForWriting, False) n = 0 n = ts.ReadLine n = n + 1 Set ts = Nothing Set ts = fso.CreateTextFile("\\proxy2\paging$\install.ini") 'implicitly Overwrite = True
ts.Write n
Set ts = Nothing
Set fso = Nothing

TS.close

15. Wow, I didn't know you could stack a variable like that (n = n + 1). I've always used a loop to cycle through the textstream object and saved it to an array. Might have something to do with the way VBS works as all variables are variants rather than specific datatypes.

16. Whir? vass? anyone? I'm still stuck on this and could use some more help.

17. gimme a min looking at it

In the meantime, I use this as a pretty good reference page for FSO
http://www.sloppycode.net/fso/?p=d

Nope whir Open doesn't work in VBS, and you can't put "Print" either ... There are a lot of little things like that you gotta watch out for in vbs

Question:
how is this script going to be called?
If its from login script you're going to have a problem with multiple people logging in at the same time and contention for this file.

also what kind of clients are you using?
if its from a login script you can use WMI to get the MAC but I'd be leary if they were Win9x clients

18. W2K & XP clients.

My intention was via a VBS file put in the startup folder for all users. Distributed via a MSI/ GPO.

AFAIK this value must be a number.

Misc: this is to fix a problem with InfoRad paging software installation. By design, this program must be installed once for every user (not just once per machine!). Each install normally increments this file as needed and places the correct value in the registry.

To speed up install and make it automatic for new and replacement computers, I want to assign the application via GPO (I created my own MSI file) and have a VBS file automatically fill in user settings if they are not already there (under HKEY Current USER).

19. Does the user have access to write to this part of the registry?

Doesn't that little guy ever get sick of pepsi!?

Create a unique number via a random number using the machine name (unique in the entire domain) as the seed, which should guarantee the random number to be different for each machine... that sound valid?
As long as you're not crossing domains you should be ok.. and if I remember right you're in AD so the likelihood of that is fairly low.

Oh btw, if you ever want to get the mac here is a (albeit a bit cheesy way lol) to get the mac into a text file on the local machine

http://www.winnetmag.com/Files/5139/Listing_01.txt

20. I can/will assign users rights to appropriate parts of the registry via GPO.

Always drinking Pepsi: No, at least he never complains.

Random number using the machine name is fine if only up to 4 digits are used. I actually don't know the limit, but 4 sounds reasonable.