Page 1 of 2 12>
Topic Options
#145151 - 2005-08-10 12:51 AM VB conversion
cyri Offline
Getting the hang of it

Registered: 2005-06-27
Posts: 95
I was wondering if someone might be able to help me convert this vbscript I wrote into kix. The vbscript conversion udf I found in the library didn't really seem to even do much to the script at all.

It's a pretty simple script designed to grab the computer and MAC address and write to a text file which would look something like this.

%ComputerName% = %MAC%

It also checks for duplication and writes to a seperate log file if there are duplicates. Any help would be greatly appreciated.

Code:
'GetNics.vbs
'Author: Travis
'Date: 6/29/2005


'Force Declaration of Variables
Option Explicit

'Declare Variables
Dim NIC1, Nic, objNet,objHost,fso,ts, modMAC, strMAC, strRead

'If something fails, move on
On Error Resume Next

'Get the Computer's network name
Set objNet=CreateObject("wscript.Network")
objHost=objNet.ComputerName

'Get a connection to the WMI NetAdapteConfig object
Const strQuery = "Select * From Win32_NetworkAdapterConfiguration where IPEnabled = True"
Set NIC1 = GetObject("winmgmts:").ExecQuery(strQuery)

'Set Variables and Constants
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject")

'Run next portion of code for each NIC queried from WMI
For Each Nic in NIC1

'Set MAC variable for each NIC and open text file for reading
strMAC = NIC.MACAddress
modMAC = mid(strMAC,1,2) + mid(strMAC,4,2) + mid(strMAC,7,2) +mid(strMAC,10,2) +mid(strMAC,13,2) + mid(strMAC,16,2)
Set ts = fso.OpenTextFile ("\\sutil\images$\rename\test.txt", ForReading, True)
strRead = ts.readall

'Check for duplicate entries in text file
'Write to conflicts.txt if conflicts found
'Otherwise write MAC = ComputerName to test.txt

If Instr(strRead,modMAC) = 0 THEN
Set ts = fso.OpenTextFile ("\\sutil\images$\rename\test.txt", ForAppending, True)
ts.WriteLine modMAC & " = " & objHost
ts.Close
ElseIf Instr(strRead,modMAC & " = " & objHost) = 1 THEN
ts.Close
Wscript.QUIT
Else
Set ts = fso.OpenTextFile ("\\sutil\images$\rename\conflicts.txt", ForReading, True)
strRead = ts.readall
If Instr(strRead,modMAC & " = " & objHost) = 1 THEN
ts.Close
Wscript.QUIT
Else
ts.Close
Set ts = fso.OpenTextFile ("\\sutil\images$\rename\conflicts.txt", ForAppending, True)
ts.WriteLine modMAC & " = " & objHost
ts.Close
End If
End If
Next


Top
#145152 - 2005-08-10 01:18 AM Re: VB conversion
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
hmm...
you could wanna consider ini-files as your format already looks similar.

anyways, this should do:
Code:

$ = open(1,"\\sutil\images$\rename\test.txt")
$ = open(2,"\\sutil\images$\rename\test.tmp",5)
$ = open(3,"\\sutil\images$\rename\conflicts.txt",5)

$line=readline(1)
while @error = 0 and not $match
if instr($line,@wksta) or instr($line,@mac)
if not instr($line,@mac + " = " + @wksta)
$ = writeline(3,@mac + " = " + @wksta + @crlf)
endif
$match=1
endif
$=writeline(2,$line+@crlf)
$line=readline(1)
loop
if not $match
$=writeline(2,@mac + " = " + @wksta + @crlf)
endif
$=close(1)
$=close(2)
$=close(3)
del "\\sutil\images$\rename\test.txt"
move "\\sutil\images$\rename\test.tmp" "\\sutil\images$\rename\test.txt"

_________________________
!

download KiXnet

Top
#145153 - 2005-08-10 01:23 AM Re: VB conversion
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
but, like said, in ini format, it would be as simple as:
Code:

$macMatch = readprofilestring("myiniFile.ini","MACs",@mac)
if len($macMatch) and $macMatch<>@wksta
$=writeprofilestring("myiniFile.ini","Conflicts",@mac,@wksta)
else
$=writeprofilestring("myiniFile.ini","MACs",@mac,@wksta)
endif



I think you see the difference
_________________________
!

download KiXnet

Top
#145154 - 2005-08-10 02:20 AM Re: VB conversion
cyri Offline
Getting the hang of it

Registered: 2005-06-27
Posts: 95
Thanks for the code. I'm probably going to go with your first option and give that a try. I failed to mention in my first email that I am dealing with certain constraints. The program I am using will only read from text files. It will be parsing the text file created from this script.

Couple of questions though. I've never seen the @mac macro, is that new? And what format does that put the mac in?

Like this xx.xx.xx.xx.xx.xx or like xx-xx-xx-xx-xx-xx or perhaps another way?

The program I'm using to parse the text file needs to have the mac address formatted a certain way. Like this xxxxxxxxxxxx
No dashes, spaces, hyphens. Just one long string.

Another thing I can see is going to be a problem is the fact that only one computer would be able to write to the text file at a time. It was my hope that I could run this as part of the login script, but that doesn't seem feasible now that I think about it more. Not sure about that one.

Also, wish I could use the ini form of that code. It's so easy that way!


Edited by cyri (2005-08-10 02:43 AM)

Top
#145155 - 2005-08-10 02:45 AM Re: VB conversion
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
and the ini form would not be that much locking the file.

depends how the program is reading the text file, after all, the difference is about none as only thing needed is one extra line at the top specifying the sectionname.
try it out and see does your program manage with that.
_________________________
!

download KiXnet

Top
#145156 - 2005-08-10 02:52 AM Re: VB conversion
cyri Offline
Getting the hang of it

Registered: 2005-06-27
Posts: 95
perhaps i could have the script pause and try again if the file is locked? would i be able to use the @error with the writeline and/or open commands?

the login script would take a bit longer to finish perhaps, but i can't see it taking too long. unless a whole bunch of users login at the same time.

Top
#145157 - 2005-08-10 02:53 AM Re: VB conversion
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
ja, you could.
so, you can't test the ini way?
_________________________
!

download KiXnet

Top
#145158 - 2005-08-10 09:39 AM Re: VB conversion
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Quote:

I've never seen the @mac macro, is that new?



Must be. So new I've never seen it either! Try @ADDRESS

Quote:

And what format does that put the mac in?



Quickest way to answer that one is to try it and see
To save you the effort, it is returned without delimiters.

Once caveat though is that there is no guarantee which MAC address you will get returned, which is an issue where you have multiple adapters including additional NICs, dial-up, wireless and so-on.

If you need to get *all* the adaptors on a system you will need to use another method. On my automated server build I parse the output from "ipconfig /all" becuase it needs to work in a pre-boot environment, but normally you should be able to get away with WMI queries.

Top
#145159 - 2005-08-10 01:00 PM Re: VB conversion
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
damn those post edits.

indeed, there is @mac in my imaginationary kixtart
_________________________
!

download KiXnet

Top
#145160 - 2005-08-10 05:28 PM Re: VB conversion
cyri Offline
Getting the hang of it

Registered: 2005-06-27
Posts: 95
Like you were saying Richard, you never know which mac that will return. I need to return all macs that have IP Enabled. So I'm still going to need my WMI query in there. But at least I should be able to use Lonkero's way of writing to the file.

Which also means I'm still going to need to trim the MAC value returned from the WMI query like I do in my original vb script. The MAC comes back like this from the WMI query XX:XX:XX:XX:XX:XX.

Top
#145161 - 2005-08-10 05:55 PM Re: VB conversion
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Stripping characters is pretty simple:
Code:
$sMac="12:34:56:78:90:AB"
$sMac=Join(Split($sMac,":"),"")


Top
#145162 - 2005-08-10 06:02 PM Re: VB conversion
cyri Offline
Getting the hang of it

Registered: 2005-06-27
Posts: 95
ini way is out unfortunatelly. i tested the program, but it only accepts text files. too bad

i'm attempting to put together the new kix script. i'll post it when i'm done.

Top
#145163 - 2005-08-10 06:12 PM Re: VB conversion
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
you know writeprofilestring('.\textfle.txt','information',@wksta,@address) will work to... but it will put [Information] as the first line in the txt file.

Edited by Bryce (2005-08-10 09:21 PM)

Top
#145164 - 2005-08-10 09:12 PM Re: VB conversion
cyri Offline
Getting the hang of it

Registered: 2005-06-27
Posts: 95
I didn't know that actually. Sometimes I wonder how I get by with my scripts. I'm sure most of you wonder that as well now. Here is some code I'm playing with right now that seems to be working. Still testing to make sure it will be able to handle any encounter thrown at it.

Code:
Dim $sComputer,$Servers,$objWMIService,$colShares,$objShare,$File,$Report
$sComputer= "."
$objWMIService = GetObject("winmgmts:" + "{impersonationLevel=impersonate}!\\" + $sComputer + "\root\cimv2")
$NIC1 = $objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled = True")
For Each $NIC In $NIC1
$sMAC = $NIC.MacAddress
$sMac=Join(Split($sMac,":"),"")


$macMatch = readprofilestring("\\sutil\images$\rename\test.txt","MACs",$sMac)
if len($macMatch) and $macMatch <> @wksta
$=writeprofilestring("\\sutil\images$\rename\conflicts.txt","Conflicts",$sMac,@wksta)
else
$=writeprofilestring("\\sutil\images$\rename\test.txt","MACs",$sMac,@wksta)
endif
Next


Top
#145165 - 2005-08-10 09:41 PM Re: VB conversion
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
Well are you running that as a Logon script or an Admin script? As a logon script I see a couple issues.

1. The user would need to be a local admin on his/her system to properly run it as you have it coded.
2. If it is a logon script there is a high potential for file access conflict since you use the same filename.

Top
#145166 - 2005-08-10 10:17 PM Re: VB conversion
cyri Offline
Getting the hang of it

Registered: 2005-06-27
Posts: 95
Well I was hoping for login script.

Quote:

1. The user would need to be a local admin on his/her system to properly run it as you have it coded




Is there a way to query WMI without being a local admin?

Quote:

2. If it is a logon script there is a high potential for file access conflict since you use the same filename.




I'm not real sure how to remedy this yet. Would a random sleep time or set sleep time work? I realize that would add time to the scripts processing, but eventually it would finish. Especially during peak login times (7am, 8am, 9am). The script runs in the background anyway so it really wouldn't be all that bad.

Top
#145167 - 2005-08-10 10:50 PM Re: VB conversion
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
cyri, looking at your latest script and what bryce said (which I tried to explain with poor success earlier) you are actually using ini file now.
the file extension of the ini just is txt.
_________________________
!

download KiXnet

Top
#145168 - 2005-08-10 11:18 PM Re: VB conversion
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
This "should" allow normal user to use during logon.

$objWMIService=GetObject("winmgmts:{impersonationLevel=impersonate}!"+$sComputer+ "\root\cimv2")

By having a UNC path it tells it to remotely connect and in my tests with a normal user account it fails.

Top
#145169 - 2005-08-10 11:23 PM Re: VB conversion
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
doc, did you mean:
$objWMIService=GetObject("winmgmts:{impersonationLevel=impersonate}!.\root\cimv2")
_________________________
!

download KiXnet

Top
#145170 - 2005-08-10 11:26 PM Re: VB conversion
cyri Offline
Getting the hang of it

Registered: 2005-06-27
Posts: 95
Yes that is correct Lonkero. I didn't realize you could use anything other than a .ini file type. Since you can, I think the ini method simplifies the script a lot. Sorry for the confusion.

And thanks NTDOC I'll give that a whirl.

Top
Page 1 of 2 12>


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 466 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.073 seconds in which 0.024 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org