#200507 - 2010-11-05 01:07 PM
problem reading af file for use in script
|
roundup
Fresh Scripter
Registered: 2010-10-29
Posts: 11
Loc: Denmark
|
Hi, are there someone who can please help me, i facial turning blue an green.
I have a Printer.ini file for my printermappings that will map printers if $bssid = $mac
I also have a function that locate the bssid of the accesspoint the client is connected to, and use this to compare the location of my printers so the client will get the printer mapped close to him.
Printer.inf 00:1B:3F:8E:CD:21;Location1;Share1;Group1 00:16:B9:52:AF:BF;Location2;Share3;Group3 00:17:08:22:0B:C3;Location3;Share2;Group6 00:1B:3F:8E:CD:21;Location4;Share4;Group4 00:1C:2E:42:94:57;Location5;Share5;Group2
In this example i have 2 identical match (mac) and want to pass the line argument to som variables for further use (printermapping) in my script. It only works as long the @error = 0 when not it reads the same line again and again?
00:1B:3F:8E:CD:21;Location1;Share1;Group1 00:1B:3F:8E:CD:21;Location4;Share4;Group4
My problem is that the script will not loop through the end of my Printer.inf to isolate the match found, here $bssid = $mac
Here is my script:
$PrinterMapping = @LDRIVE + "\Printer.ini"
$ = Open(1, $PrinterMapping)
$aCurLine = Split(ReadLine(1),';')
$LoopCount = 0
While @error = 0
$mac = Trim($aCurLine[0])
$location = Trim($aCurLine[1])
$share = Trim($aCurLine[2])
$group = Trim($aCurLine[3])
If "$bssid" = "$mac"
? "BSSID: " $mac
? "Printer Placering: " $location
? "Printer Deling: " $share
? "Printer Gruppe: " $group
? @Error
$aCurLine = Split(ReadLine(1),';')
Else
? "Match not found"
? $bssid
? $mac
? $LoopCount
ENDIF
$LoopCount = $LoopCount + 1
sleep 3
Loop
Kind regards roundup
Edited by Mart (2010-11-05 02:08 PM) Edit Reason: Please use code tags when posting code.
|
Top
|
|
|
|
#200512 - 2010-11-05 04:27 PM
Re: problem reading af file for use in script
[Re: Les]
|
Shanee
Fresh Scripter
Registered: 2006-10-13
Posts: 39
Loc: Tulsa, OK
|
Using the bssid function i posted the other day...and a standard ini file...this is quite simple.
$mac = GetBSSID(1,@WkSta)
$location = ReadProfileString (@ScriptDir+"\info.ini", $mac, "Location")
$share = ReadProfileString (@ScriptDir+"\info.ini", $mac, "Share")
$group = ReadProfileString (@ScriptDir+"\info.ini", $mac, "Group")
$null = MessageBox($mac+@CRLF+$location+@CRLF+$share+@CRLF+$group,"BSSID",0)
FUNCTION GetBSSID(optional $device, optional $machine)
$strComputer = IIF (VarType($machine)<2, @Wksta, $Machine)
$objSWbemServices = GetObject("winmgmts:\\"+ $strComputer + "\root\wmi")
$colInstances = $objSwbemServices.ExecQuery("SELECT * FROM MSNdis_80211_ReceivedSignalStrength WHERE Active=True")
if $device<1
$card_no=1
$cards = "Please specify one of these devices to obtain it's BSSID :"
for each $objInstance in $colInstances
$cards = $cards+@CRLF+"Card "+$card_no+" = "+$objInstance.InstanceName
$card_no=$card_no +1
next
$GetBSSID = $cards
Return
endIf
$card_no=$device
$x=1
for each $objInstance in $colInstances
if $x = $card_no
$wifiAdapter = $objInstance.InstanceName
$x = $x + 1
endif
next
$last_signal = 0
$bssid = ""
$colInstances = $objSwbemServices.ExecQuery('SELECT * FROM MSNdis_80211_BaseServiceSetIdentifier WHERE Active = True AND InstanceName ="'+$wifiAdapter+'"')
for each $objInstance in $colInstances
$macbyte = 0
for each $decval in $objInstance.Ndis80211MacAddress
if $decval<17
$bssid = $bssid + "0"
endif
$bssid = $bssid + DecToHex($decval)
if $macbyte < 5
$bssid = $bssid + ":"
$macbyte = $macbyte + 1
endif
next
next
$colInstances = $objSwbemServices.ExecQuery ("SELECT * FROM MSNdis_80211_ReceivedSignalStrength WHERE Active = True AND InstanceName ='" + $wifiAdapter + "'")
for each $objInstance in $colInstances
$sigraw = $objInstance.Ndis80211ReceivedSignalStrength
$signal = $sigraw + "dB"
next
$last_signal = $sigraw
$GetBSSID = $bssid
ENDFUNCTION
and i just created a sample info.INI file that looks like this...
[00:1B:3F:8E:CD:21]
Location=Location1
Share=Share1
Group=Group1
[00:16:B9:52:AF:BF]
Location=Location2
Share=Share3
Group=Group3
[00:17:08:22:0B:C3]
Location=Location3
Share=Share2
Group=Group6
[00:1B:3F:8E:CD:21]
Location=Location4
Share=Share4
Group=Group4
[00:1C:2E:42:94:57]
Location=Location5
Share=Share5
Group=Group2
[00:25:9C:AD:43:D6]
Location=Location99
Share=Share98
Group=Group97
|
Top
|
|
|
|
#200514 - 2010-11-05 11:40 PM
Re: problem reading af file for use in script
[Re: Shanee]
|
roundup
Fresh Scripter
Registered: 2010-10-29
Posts: 11
Loc: Denmark
|
Hi Shanee
Thanks again for your great help, as you probably already figured out I am only a beginner. I really appreciate your help and are very grateful if you can help me on track.
My question is now.
How do I transfer the variables from Printer.ini file for further use in kix?
In the script you've made for me that finds the BSSID the clients are connected to, must now run through Printer.ini file to find the printers to be mapped in the area of BSSID, the way is to locate the same BSSID in the Printer.ini file and determin which printers to be mapped on the client.
The following parameters are used:
BSSID to determine whether the client is near the printer to be allocated.
Group for investigate whether the user is a member or not and if true then map the printer, otherwise not, and it can be that a student do not have access to a printer which has the same BSSID for the area!
The printer share for mappings (AddPrinterConnection)
The printer name to displayed to the user during mapping.
If there is only one printer then it must be set as default, if there are more in the same area the the last one must be set as default or perhaps determine this in the Printer.ini file!?
ex Printer.ini
[00:1B:3F:8E:CD:21] Location = Preparation Name = HP LaserJet 5550 Share = Laserjet1 Group = Students Default = HP LaserJet 5550
[00:1B:3F:8E:CD:21] Location = Preparation Name = HP LaserJet M4730 Share = Laserjet1 Group = Teacher Default = HP LaserJet M4730
[00:16:B9:52:AF:BF] Location = Cellular Name = HP LaserJet 1320 Share = Laserjet7 Group = Teacher Default = HP LaserJet 5550
[00:16:B9:52:AF:BF] Location = Cellular Share = Laserjet8 Group = Group6
etc. ..
I hope there are someone who can help me, from the modest kix beginner.
Kind regards roundup
|
Top
|
|
|
|
#200517 - 2010-11-06 01:57 PM
Re: problem reading af file for use in script
[Re: Les]
|
roundup
Fresh Scripter
Registered: 2010-10-29
Posts: 11
Loc: Denmark
|
Hi
How do I resolve my problem if there are more printers to be assigned to the same location as the access point (BSSID) when you write that each [SECTION] in a. ini must be unique?
Can I, as described above in the article use a Printer.txt file instead containing the variables to be used for mapping my printers.
And here use Printer.txt to search from start to finish and find out what printers to assign clients if the BSSID in the Printer.txt file is identical to the access point (BSSID).
I have tried to start a script, first in the article, but it do not works it stops if the $bssid is not equal to $mac, and then hangs in the same line without continuing to the end of the file?
Surely it is possible to isolate these variables for use in KIX script or what?
Printer.txt BSSID;location;printer share;group membership;printer model info 00:1B:3F:8E:CD:21;Location1;Laserjet1;Teacher;HP LaserJet 5550 00:1B:3F:8E:CD:21;Location1;Laserjet2;Students;HP LaserJet 4500 00:17:08:22:0B:C3;Location2;Laserjet3;All;HP LaserJet 1320
etc....
What should I do to get it to work, when I can not use a a true INI format.
NB: If only one printer is mapped, should this be set to default, if there is more than on printer, the last should be set as default.
Sincerely, roundup
|
Top
|
|
|
|
#200518 - 2010-11-06 04:23 PM
Re: problem reading af file for use in script
[Re: roundup]
|
Shanee
Fresh Scripter
Registered: 2006-10-13
Posts: 39
Loc: Tulsa, OK
|
I see you problem. May I ask why you need a Share AND a Name since you only use the Share to use the AddPrinterConnection(). Also do all of these printers reside on the same server?
|
Top
|
|
|
|
#200519 - 2010-11-06 04:54 PM
Re: problem reading af file for use in script
[Re: Shanee]
|
Shanee
Fresh Scripter
Registered: 2006-10-13
Posts: 39
Loc: Tulsa, OK
|
You could just put multiple shares under one section of the ini. like so...
[00:25:9C:AD:43:D6]
Location = Location99
Group = Group97
Shares = Share98,Share99,Share100,Share101,Share102
Names = Printer 98,Printer 99,Printer 100,Printer 101,Printer 102
Then modify the script to split them into multiple items if a "," exists.
$mac = GetBSSID(1,@WkSta)
$location = ReadProfileString (@ScriptDir+"\info.ini", $mac, "Location")
$group = ReadProfileString (@ScriptDir+"\info.ini", $mac, "Group")
$names = ReadProfileString (@ScriptDir+"\info.ini", $mac, "Names")
$shares = ReadProfileString (@ScriptDir+"\info.ini", $mac, "Shares")
If InStr($shares,",")
$names = Split($names,",")
$shares = Split($shares,",")
If UBound($names) <> UBound($shares)
$null = MessageBox("There seems to be an uneven Name to Share ratio for this user. Please verify printer.ini file for : "+$mac,"Name/Share Mismatch",0)
Else
$sharenum = 0
For Each $share in $shares
$name = $names[$sharenum]
If InGroup($group)
If AddPrinterConnection("\\server\"+$share) = 0
If $sharenum = UBound($shares)
SetDefaultPrinter("\\server\"+$share)
EndIf
Else
$null = MessageBox("There seems to have been a problem connecting to : "+$share,"Printer Error",0)
EndIf
EndIf
$sharenum = $sharenum+1
Next
EndIf
Else
If InGroup($group)
If AddPrinterConnection("\\server\"+$shares) = 0
SetDefaultPrinter("\\server\"+$shares)
Else
$null = MessageBox("There seems to have been a problem connecting to : "+$shares,"Printer Error",0)
EndIf
EndIf
EndIf
Still not sure what you would use the NAME variable for. Maybe this is supposed to be the server name?
|
Top
|
|
|
|
#200520 - 2010-11-06 04:59 PM
Re: problem reading af file for use in script
[Re: Shanee]
|
Allen
KiX Supporter
Registered: 2003-04-19
Posts: 4545
Loc: USA
|
|
Top
|
|
|
|
#200524 - 2010-11-06 05:40 PM
Re: problem reading af file for use in script
[Re: Les]
|
Shanee
Fresh Scripter
Registered: 2006-10-13
Posts: 39
Loc: Tulsa, OK
|
Ahh both very good points...
So sample .ini file...
[00:25:9C:AD:43:D6]
Location = Location99
Group = Group97
Shares = Share 98:Share98,Share 99:Share99,Share 100:Share100,Share 101:Share101,Share 102:Share102
Sample code to use .ini file...(changed setdefault to use name, changed share and name to one variable with a delimiter, also brought out the ingroup check since they all seem to depend on it.)
$mac = GetBSSID(1,@WkSta)
$location = ReadProfileString (@ScriptDir+"\info.ini", $mac, "Location")
$group = ReadProfileString (@ScriptDir+"\info.ini", $mac, "Group")
$shares = ReadProfileString (@ScriptDir+"\info.ini", $mac, "Shares")
If InGroup($group)
If InStr($shares,",")
$shares = Split($shares,",")
$sharenum = 0
For Each $shareobj in $shares
$name = Split($shareobj,":")[0]
$share = Split($shareobj,":")[1]
If AddPrinterConnection("\\server\"+$share) = 0
If $sharenum = UBound($shares)
$null = SetDefaultPrinter("\\server\"+$name)
EndIf
Else
$null = MessageBox("There seems to have been a problem connecting to : "+$share,"Printer Error",0)
EndIf
$sharenum = $sharenum+1
Next
Else
$name = Split($shares,":")[0]
$share = Split($shares,":")[1]
If AddPrinterConnection("\\server\"+$share) = 0
$null = SetDefaultPrinter("\\server\"+$name)
Else
$null = MessageBox("There seems to have been a problem connecting to : "+$shares,"Printer Error",0)
EndIf
EndIf
EndIf
Edited by Shanee (2010-11-06 06:00 PM) Edit Reason: changed defaultprinter lines
|
Top
|
|
|
|
#200525 - 2010-11-06 05:49 PM
Re: problem reading af file for use in script
[Re: Shanee]
|
Allen
KiX Supporter
Registered: 2003-04-19
Posts: 4545
Loc: USA
|
Should be something like this, although I don't see where $server is ever defined.
Shanee's code (don't forget the = after $null )
$null=SetDefaultPrinter("\\" + $server + "\" + $name)
or with SetAsDefaultPrinter(requires UDF)
$null=SetAsDefaultPrinter("\\" + $server + "\" + $share)
|
Top
|
|
|
|
#200526 - 2010-11-06 06:00 PM
Re: problem reading af file for use in script
[Re: Allen]
|
Shanee
Fresh Scripter
Registered: 2006-10-13
Posts: 39
Loc: Tulsa, OK
|
Good catch, edited/corrected above posted code.
|
Top
|
|
|
|
#200527 - 2010-11-06 11:36 PM
Re: problem reading af file for use in script
[Re: Shanee]
|
roundup
Fresh Scripter
Registered: 2010-10-29
Posts: 11
Loc: Denmark
|
1000 thanks for all your inputs.
I will test this as the first thing Monday morning, and of course comment if it works.
I am very grateful for all the great effort from all of you, and the many valuable solutions you all have contributed.
And a special thanks goes to a great coder Shanee for all the support and coding, and last but not least the answer to my problem, and I know that our school will benefit from it.
Sincerely, roundup
|
Top
|
|
|
|
#200528 - 2010-11-06 11:43 PM
Re: problem reading af file for use in script
[Re: roundup]
|
Shanee
Fresh Scripter
Registered: 2006-10-13
Posts: 39
Loc: Tulsa, OK
|
No problem. Hope it works. I haven't messed with kix much in a number of years, so it's nice to work on these kinds of problems to re-familiarize myself as well haha.
-Shane
|
Top
|
|
|
|
#200534 - 2010-11-08 02:00 PM
Re: problem reading af file for use in script
[Re: Shanee]
|
roundup
Fresh Scripter
Registered: 2010-10-29
Posts: 11
Loc: Denmark
|
Hi Shanee
The script works perfectly, but now there is only one problem:
I have 2 printers in the same location, and the teachers group are member of the students group, and should therefor have both printers installed, no problem here, but the students should not have the printer Xerox1 installed so they dont get confused when printing to a printer they dont have print rights to, how do i resolve this.
I was wondering if it was possible to integrate in your script, to define and check for group membership like the section for Names and Shares, like this:
Group = students,teachers
printer.ini [00:16:B9:52:AF:BF] Location = Preparation Group = students Names = HP LaserJet 1320nw - Support,Xerox WorkCentre 7345 Shares = LaserJet1,Xerox1
Kind regards roundup
Working script:
$location = ReadProfileString ($LogonServer + "\NETLOGON\printer.ini", $mac, "Location")
$group = ReadProfileString ($LogonServer + "\NETLOGON\printer.ini", $mac, "Group")
$names = ReadProfileString ($LogonServer + "\NETLOGON\printer.ini", $mac, "Names")
$shares = ReadProfileString ($LogonServer + "\NETLOGON\printer.ini", $mac, "Shares")
If InStr($shares,",")
$names = Split($names,",")
$shares = Split($shares,",")
If UBound($names) <> UBound($shares)
$null = MessageBox("Der er uoverensstemmelse mellem Names og Shares. Kontroller printer.ini filen under sektionen : "+$mac,0)
Else
$sharenum = 0
For Each $share in $shares
$name = $names[$sharenum]
If InGroup($group)
If AddPrinterConnection("\\" + $printserver + "\" + $share) = 0
If $sharenum = UBound($shares)
$null = SetDefaultPrinter("\\" + $printserver + "\" + $name)
EndIf
Else
$null = MessageBox("2 Der er et problem med at etablerer forbindelse til : "+$share,"Printer fejl",0)
EndIf
EndIf
$sharenum = $sharenum+1
Next
EndIf
Else
If InGroup($group)
If AddPrinterConnection("\\" + $printserver + "\" + $shares) = 0
$null = SetDefaultPrinter("\\" + $printserver + "\" + $names)
Else
$null = MessageBox("2 Der er et problem med at etablerer forbindelse til : "+$shares,"Printer fejl",0)
EndIf
EndIf
EndIf
Edited by Mart (2010-11-08 02:27 PM) Edit Reason: Added code tags. Please use code tags when posting code.
|
Top
|
|
|
|
#200535 - 2010-11-08 06:37 PM
Re: problem reading af file for use in script
[Re: roundup]
|
Shanee
Fresh Scripter
Registered: 2006-10-13
Posts: 39
Loc: Tulsa, OK
|
Ok, the easiest way that I can think of (maybe someone else has a more efficient way), would be to create different INI sections for the different groups. So if in teacher group, get all printers in that area, but if in student group then only get one printer in that area. I changed the INI to be like this...Where the same MAC area has different sections depending on the group the person is in. The modified code below will now check the approptiate section depending on which group that are in.
[00:25:9C:AD:43:D6:teacher]
Location = Preparation
Shares = HP LaserJet 1320nw - Support:LaserJet1,Xerox WorkCentre 7345:Xerox1
[00:25:9C:AD:43:D6:student]
Location = Preparation
Shares = HP LaserJet 1320nw - Support:LaserJet1
Modified code...
$mac = GetBSSID(1,@WkSta)
If InGroup("Teacher")
$mac = $mac+":teacher"
Else
If InGroup("Student")
$mac = $mac+":student"
Else
$null = MessageBox("User is not in any of the printer groups.","Printer fejl",0)
Quit
EndIf
EndIf
$location = ReadProfileString ($LogonServer+"\NETLOGON\printer.ini", $mac, "Location")
$shares = ReadProfileString ($LogonServer+"\NETLOGON\printer.ini", $mac, "Shares")
If InStr($shares,",")
$shares = Split($shares,",")
$sharenum = 0
For Each $shareobj in $shares
$name = Split($shareobj,":")[0]
$share = Split($shareobj,":")[1]
If AddPrinterConnection("\\server\"+$share) = 0
If $sharenum = UBound($shares)
$null = SetDefaultPrinter("\\server\"+$name)
EndIf
Else
$null = MessageBox("2 Der er et problem med at etablerer forbindelse til : "+$share,"Printer fejl",0)
EndIf
$sharenum = $sharenum+1
Next
Else
$name = Split($shares,":")[0]
$share = Split($shares,":")[1]
If AddPrinterConnection("\\server\"+$share) = 0
$null = SetDefaultPrinter("\\server\"+$name)
Else
$null = MessageBox("2 Der er et problem med at etablerer forbindelse til : "+$share,"Printer fejl",0)
EndIf
EndIf
FUNCTION GetBSSID(optional $device, optional $machine)
$strComputer = IIF (VarType($machine)<2, @Wksta, $Machine)
$objSWbemServices = GetObject("winmgmts:\\"+ $strComputer + "\root\wmi")
$colInstances = $objSwbemServices.ExecQuery("SELECT * FROM MSNdis_80211_ReceivedSignalStrength WHERE Active=True")
if $device<1
$card_no=1
$cards = "Please specify one of these devices to obtain it's BSSID :"
for each $objInstance in $colInstances
$cards = $cards+@CRLF+"Card "+$card_no+" = "+$objInstance.InstanceName
$card_no=$card_no +1
next
$GetBSSID = $cards
Return
endIf
$card_no=$device
$x=1
for each $objInstance in $colInstances
if $x = $card_no
$wifiAdapter = $objInstance.InstanceName
$x = $x + 1
endif
next
$last_signal = 0
$bssid = ""
$colInstances = $objSwbemServices.ExecQuery('SELECT * FROM MSNdis_80211_BaseServiceSetIdentifier WHERE Active = True AND InstanceName ="'+$wifiAdapter+'"')
for each $objInstance in $colInstances
$macbyte = 0
for each $decval in $objInstance.Ndis80211MacAddress
if $decval<17
$bssid = $bssid + "0"
endif
$bssid = $bssid + DecToHex($decval)
if $macbyte < 5
$bssid = $bssid + ":"
$macbyte = $macbyte + 1
endif
next
next
$colInstances = $objSwbemServices.ExecQuery ("SELECT * FROM MSNdis_80211_ReceivedSignalStrength WHERE Active = True AND InstanceName ='" + $wifiAdapter + "'")
for each $objInstance in $colInstances
$sigraw = $objInstance.Ndis80211ReceivedSignalStrength
$signal = $sigraw + "dB"
next
$last_signal = $sigraw
$GetBSSID = $bssid
ENDFUNCTION
|
Top
|
|
|
|
#200545 - 2010-11-09 02:38 PM
Re: problem reading af file for use in script
[Re: Glenn Barnas]
|
roundup
Fresh Scripter
Registered: 2010-10-29
Posts: 11
Loc: Denmark
|
Hi Shanee
1000 thanks for all your hard work.
I have not yet had time to test your change with respect to group affiliation, but am sure it will work, therefore I close the thread here.
"I will return again when I have problems."
Sincerely, roundup
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
1 registered
(Allen)
and 466 anonymous users online.
|
|
|