Page 1 of 1 1
Topic Options
#197267 - 2010-01-04 09:03 PM Adding Printers separating groups
Valentim Offline
Fresh Scripter

Registered: 2010-01-04
Posts: 46
Loc: Brazil, PE, Recife
Hello,

I'm having some trouble creating a script to add network printers separately by group
Example:

 Code:
Group 01>> Printer \\Server03\HP-Group01

If InGroup("Group01") = 0
  $ print = "\\Server03\HP-Group01"
  If AddPrinterConnection($print) = 0
    SetDefaultPrinter($print)
  EndIf
Endif

Group 02>> Printer \\Server03\HP-Group01

If InGroup("Group02") = 0
  $ print = "\\Server03\HP-Group02"
  If AddPrinterConnection($print) = 0
    SetDefaultPrinter($print)
  EndIf
Endif

Group 03>> Printer \\Server03\HP-Group01

If InGroup("Group03") = 0
  $ print = "\\Server03\HP-Group03"
  If AddPrinterConnection($print) = 0
    SetDefaultPrinter($print)
  EndIf
Endif


I did something simple but I'm trying to improve the script, anyone have any tips on how to improve this structure? I'm trying to create the script using the ini files
_________________________

Top
#197272 - 2010-01-04 11:55 PM Re: Adding Printers separating groups [Re: Valentim]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4665
Loc: The Netherlands
Hi Roberto,

Welcome to the board.

You have some small typos in your script. If the InGroup function returns 0 the user is NOT in the group but in your setup they do get the printer. Second thing is that there is a space between the $ and print when setting the printer name. See below for the "fixed" code.

 Code:
If InGroup("Group01")
	$print = "\\Server03\HP-Group01"
	If AddPrinterConnection($print) = 0
		SetDefaultPrinter($print)
	EndIf
EndIf


If you want to use an ini file then a setup like below should do the trick.

Ini file:
 Quote:

[group1]
printers=printer1,printer2
default=printer2
[group2]
printers=printer1,printer3
default=printer3


Example code using the ini file above:
 Code:
Break on

$inifile = "Path and name of ini file goes here"

If InGroup("group1")
	$printers = ReadProfileString($inifile, "Group1", "Printers")
	$printers = Split($printers, ",")
	$default = ReadProfileString($inifile, "Group1", "Default")
	For Each $printer in $printers
		$rc = AddPrinterConnection($printer)
		If $printer = $default
			$rc = SetDefaultPrinter($printer)
		EndIf
	Next
EndIf


Edited by Mart (2010-01-04 11:55 PM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#197273 - 2010-01-05 01:18 PM Re: Adding Printers separating groups [Re: Mart]
Valentim Offline
Fresh Scripter

Registered: 2010-01-04
Posts: 46
Loc: Brazil, PE, Recife
Thanks for help to use ini files ...

But the script would be very great because the structure of the company has more than 15 groups, will try to develop a function that reads all groups of the ini file.

If you can help me or if this function exists somewhere please let me know
_________________________

Top
#197274 - 2010-01-05 01:48 PM Re: Adding Printers separating groups [Re: Valentim]
BradV Offline
Seasoned Scripter
****

Registered: 2006-08-16
Posts: 683
Loc: Maryland, USA
Hi Roberto,

Mart already gave it to you. Just create the INI with all of your group information. Each group gets its own section. If each group is mutually exclusive, you can select them use a select case statement. If not, just put them in contiguous if statements as Mart showed. If any of that is unclear, let us know.

Regards,

Brad

Top
#197275 - 2010-01-05 02:36 PM Re: Adding Printers separating groups [Re: BradV]
Valentim Offline
Fresh Scripter

Registered: 2010-01-04
Posts: 46
Loc: Brazil, PE, Recife
Hi BradV,

Yeah, He just gave me a code where I would have to create a "If InGroup (GroupX)" for each existing group.

Through this code he gave me this could create code that automatically gets all the groups, keys and values of the ini file

 Code:
;================================================================
;= Function    : ENumGroupIni
;= Author      : Roberto Valentim
;= Example(s)  : ENumGroupIni("C:\print.ini")
;================================================================
Function ENumGroupIni($file)
;================================================================
	DIM $GrpArray, $Grupo, $GrpChave, $Chave, $Valores, $Valor

	$GrpArray = split(readprofilestring($file, '', ''), chr(10))
	For Each $Grupo in $GrpArray
		If ($Grupo <> "")
			If InGroup($Grupo)
;				? "Grupo >> " + $Grupo
				$GrpChave = split(readprofilestring($file, $Grupo, ''), chr(10))
				For Each $Chave in $GrpChave
					If ($Chave = "Printers")
;						? "     Chave >> " + $Chave
						$Valores = split(readprofilestring($file, $Grupo, $Chave), ",")
						For Each $Valor in $Valores
							AddPrinterConnection("\\srv-jbgarq03\" + $Valor)
;							? "          Printers >> " + $Valor
						Next
					EndIf

					If ($Chave = "Default")
;						? "     Chave >> " + $Chave
						$Valores = split(readprofilestring($file, $Grupo, $Chave), ",")
						For Each $Valor in $Valores
							SetDefaultPrinter("\\srv-jbgarq03\" + $Valor) = 0
;							? "          Default >> " + $Valor
						Next
					EndIf
				Next
			EndIf
		EndIf
	Next
EndFunction


Mart, Thanks for the help and disclaims


Edited by Valentim (2010-01-05 02:37 PM)
_________________________

Top
#197276 - 2010-01-05 03:00 PM Re: Adding Printers separating groups [Re: Valentim]
Valentim Offline
Fresh Scripter

Registered: 2010-01-04
Posts: 46
Loc: Brazil, PE, Recife
Why the result is different as it uses

 Code:
If InGroup($Group)
  ? "Group >> " + $Group
EndIf

Result = No Printer

and

 Code:
If InGroup($Group) = 0
  ? "Group >> " + $Group
EndIf

Result = two printers

What should I use? What is the right way?
_________________________

Top
#197292 - 2010-01-06 12:43 PM Re: Adding Printers separating groups [Re: Valentim]
BradV Offline
Seasoned Scripter
****

Registered: 2006-08-16
Posts: 683
Loc: Maryland, USA
Hi Roberto,

I'm confused. What does this do?
 Code:
$GrpArray = split(readprofilestring($file, '', ''), chr(10))
It's not going to read anything. Are you trying not to code each group into the script itself? If so, create another ini that just lists your groups and read it from there.

Top
#197294 - 2010-01-06 01:03 PM Re: Adding Printers separating groups [Re: BradV]
Glenn Barnas Administrator Online   content
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4350
Loc: New Jersey
Brad - take a look at the EnumIni UDFs.. that syntax is the core method to return a list of sections in an INI file.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#197295 - 2010-01-06 01:06 PM Re: Adding Printers separating groups [Re: BradV]
Valentim Offline
Fresh Scripter

Registered: 2010-01-04
Posts: 46
Loc: Brazil, PE, Recife
Hello BradV,

This line will capture all the groups of the ini file, for example:

 Code:
[Group01]
Printer=Print-Group01,Print-Group02
Default=Print-Group01
[Group02]
Printer=Print-Group02
Default=Print-Group02
[Group03]
Printer=Print-Group03,Print-Group01
Default=Print-Group03

the variable $ GrpArray will be an array with the values "Group01" "Group02" and "Group03"

The line which has
 Code:
$GrpChave = split(readprofilestring($file, $Grupo, ''), chr(10))

the variable $GrpChave will also be an array with the values "Printer" and "Default"

the same thing will happen to the variable $Valores
 Code:
 $Valores  = split(readprofilestring($file, $Grupo, $Chave), ",")

The variable $Valores in the part of If $Valores ="Printer" and $Grupo ="Group01" will receive the following values Print-Group01 and Print-Group02

the part of If $Valores ="Default" and $Grupo ="Group01" will receive the following values Print-Group01

I hope you have understood what I tried to explain
_________________________

Top
#197303 - 2010-01-07 12:17 PM Re: Adding Printers separating groups [Re: Valentim]
Valentim Offline
Fresh Scripter

Registered: 2010-01-04
Posts: 46
Loc: Brazil, PE, Recife
I have a problem, most users are members of only one group, but when you run the script below the result is wrong.

User: rvalentim
Groups: Group TI, Group Systems, Computers

User: ssouza
Groups: Group Billing

Script 1 for users rvalentim and ssouza:
 Code:
If InGroup("Group Support")
	? "In Group Support"
Else
	? "Not In Group Support"
EndIf

If InGroup("Group TI")
	? "In Group TI"
Else
	? "Not In Group TI"
EndIf

If InGroup("Group Systems")
	? "In Group Systems"
Else
	? "Not In Group Systems"
EndIf

If InGroup("Group Billing")
	? "In Group Billing"
Else
	? "Not In Group Billing"
EndIf


Result 1 for users rvalentim and ssouza:
 Code:
Not In Group Support
Not In Group TI
Not In Group Systems
Not In Group Billing


Script 2 for users rvalentim and ssouza: (Script with "= 0")
 Code:
If InGroup("Group Support") = 0
	? "In Group Support"
Else
	? "Not In Group Support"
EndIf

If InGroup("Group TI") = 0
	? "In Group TI"
Else
	? "Not In Group TI"
EndIf

If InGroup("Group Systems") = 0
	? "In Group Systems"
Else
	? "Not In Group Systems"
EndIf

If InGroup("Group Billing") = 0
	? "In Group Billing"
Else
	? "Not In Group Billing"
EndIf


Result 2 for users rvalentim and ssouza:
 Code:
In Group Support
In Group TI
In Group Systems
In Group Billing


The correct result would be for user rvalentim:
 Code:
Not In Group Support
In Group TI
In Group Systems
Not In Group Billing


The correct result would be for user ssouza:
 Code:
Not In Group Support
Not In Group TI
Not In Group Systems
In Group Billing


What is wrong with my script? someone help me?
_________________________

Top
#197305 - 2010-01-07 12:24 PM Re: Adding Printers separating groups [Re: Valentim]
Glenn Barnas Administrator Online   content
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4350
Loc: New Jersey
If these were recent changes, Kix may be using cached data that is now stale. Run KIX32 /F and then try your script to see if the problem persists.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#197306 - 2010-01-07 01:03 PM Re: Adding Printers separating groups [Re: Glenn Barnas]
Valentim Offline
Fresh Scripter

Registered: 2010-01-04
Posts: 46
Loc: Brazil, PE, Recife
ran the script with the parameter "/f" and the result was the same

CMD >> "KIX32.EXE /f C:\KiXtart\script.kix"
_________________________

Top
#197307 - 2010-01-07 02:07 PM Re: Adding Printers separating groups [Re: Valentim]
Glenn Barnas Administrator Online   content
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4350
Loc: New Jersey
Your group names actually contain the word "group"?? (as in "Group Support" and not a group called "Support").
 Code:
If InGroup('name')
  'Is in the group' ?
Else
  'is NOT in the group' ?
EndIf
Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#197308 - 2010-01-07 02:08 PM Re: Adding Printers separating groups [Re: Valentim]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Try this little script to sanity check the groups:
 Code:
$=SetOption("Explicit","ON")
Break ON

Dim $sGroup,$iIndex

$iIndex=0

"--Starting enumeration for "+@USERID+@CRLF
$sGroup=EnumGroup($iIndex)
While $sGroup
	IIf(InGroup($sGroup),"  Passed","**FAILED")+" checking InGroup() for '"+$sGroup+"'"+@CRLF
	$iIndex=$iIndex+1 $sGroup=EnumGroup($iIndex)
Loop
"--Enumeration done"+@CRLF

Top
#197312 - 2010-01-07 03:00 PM Re: Adding Printers separating groups [Re: Richard H.]
Valentim Offline
Fresh Scripter

Registered: 2010-01-04
Posts: 46
Loc: Brazil, PE, Recife
 Originally Posted By: Glenn Barnas
Your group names actually contain the word "group"?? (as in "Group Support" and not a group called "Support").
 Code:
If InGroup('name')
  'Is in the group' ?
Else
  'is NOT in the group' ?
EndIf
Glenn

Yes, some groups have the word "group" at the beginning

 Originally Posted By: Richard H.
Try this little script to sanity check the groups:
 Code:
$=SetOption("Explicit","ON")
Break ON

Dim $sGroup,$iIndex

$iIndex=0

"--Starting enumeration for "+@USERID+@CRLF
$sGroup=EnumGroup($iIndex)
While $sGroup
	IIf(InGroup($sGroup),"  Passed","**FAILED")+" checking InGroup() for '"+$sGroup+"'"+@CRLF
	$iIndex=$iIndex+1 $sGroup=EnumGroup($iIndex)
Loop
"--Enumeration done"+@CRLF

Not showing all groups that the User has the active directory


Edited by Valentim (2010-01-07 03:02 PM)
_________________________

Top
#197319 - 2010-01-07 09:42 PM Re: Adding Printers separating groups [Re: Valentim]
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Hi Roberto,

Here are the results that I see with Richard's script..
 Quote:

--Starting enumeration for KDyer
Passed checking InGroup() for 'DOMAIN\Domain Users'
Passed checking InGroup() for 'Everyone'
Passed checking InGroup() for 'WORKSTATION\Users'
Passed checking InGroup() for 'WORKSTATION\Administrators'
Passed checking InGroup() for 'INTERACTIVE'
Passed checking InGroup() for 'Authenticated Users'
Passed checking InGroup() for 'LOCAL'
Passed checking InGroup() for 'DOMAIN\Domain Admins'
Passed checking InGroup() for 'DOMAIN\Tem Svcs Users'
Passed checking InGroup() for 'DOMAIN\Documentation_READ'
Passed checking InGroup() for 'DOMAIN\DEVELOPMENT'
Passed checking InGroup() for 'DOMAIN\Implementation'
Passed checking InGroup() for 'Administrators'
Passed checking InGroup() for 'Users'
--Enumeration done


So, I put together a script that may help you understand this a bit more..
 Code:
BREAK ON
CLS

$rc=InGroup("Group Support")
?"For Group Support "+$rc

$rc=InGroup("Group TI")
?"For Group TI "+$rc

$rc=InGroup("Group Systems")
?"For Group Systems "+$rc

$rc=InGroup("Group Billing")
?"For Group Billing "+$rc

$rc=InGroup("Domain Admins")
?"For Domain Admins "+$rc

$rc=InGroup(@DOMAIN+"\Domain Admins")
?"For "+@DOMAIN+"\Domain Admins "+$rc

?"Script is done..  Press a key"
Get $


Here are the results from the script I just put together:
 Quote:

For Group Support 0
For Group TI 0
For Group Systems 0
For Group Billing 0
For Domain Admins 1
For DOMAIN\Domain Admins 1
Script is done.. Press a key


HTH,

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#197322 - 2010-01-08 09:31 AM Re: Adding Printers separating groups [Re: Valentim]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
 Originally Posted By: Valentim
Not showing all groups that the User has the active directory


That's good. So what is special about the groups that are not appearing? What type of group are they?

Top
#197326 - 2010-01-08 11:52 AM Re: Adding Printers separating groups [Re: Richard H.]
Valentim Offline
Fresh Scripter

Registered: 2010-01-04
Posts: 46
Loc: Brazil, PE, Recife
I found out what the problem was already resolved and the network administrator has placed the group as "local" and not the Network group, which is why the groups were not being display in the script of Richard.

Thanks for everyone's help, the script is working perfectly now.

Ini File:
 Code:
[DOMAIN\Print DEMAN]
Printers=\\Server01\HP-2015-Deman
Default=\\Server01\HP-2015-Deman

[DOMAIN\Print SMAQF1]
Printers=\\Server01\HP-1320-SMAQF1
Default=\\Server01\HP-1320-SMAQF1

[DOMAIN\Print SMAQF2]
Printers=\\Server01\HP-2015-SMAQF2
Default=\\Server01\HP-2015-SMAQF2

[DOMAIN\Print UGB02]
Printers=\\Server01\HP-2015-UGB02
Default=\\Server01\HP-2015-UGB02


Script:
 Code:
Break ON
CLS

; Altera o Titulo da Janela do MS-DOS
SetTitle ("Nome da Empresa [KiXtart @Kix]")

;================================================================
;= Função        : DelPrinterConnections
;= Autor         : Lonkero
;= UDF Site      : http://www.kixtart.org/UDF/UDF_lister.php?what=post&code=83797
;= Contribuintes : Nenhum
;= Changes       : Roberto Valentim
;= Descrição     : Deleta todas as impressoas que foram adicionadas
;=                  apartir do servidor informado pelo parametro
;=                  $Server que devera ser informado.
;= Sintaxe       : DelPrinterConnections($Server)
;= Parâmetros    : $Server
;=                 Nome do Servidor
;= Retorna       : Nada
;= Dependências  : Nenhuma
;= Exemplo(s)    : DelPrinterConnections("Servidor01")
;================================================================
Function DelPrinterConnections($Server)
;================================================================
	DIM $c, $bk, $conn, $Index, $KeyName[5], $element
	$c = 0
	$bk = "HKEY_CURRENT_USER\Printers\Connections"
	For $Index = 0 to 5
		$KeyName[$Index] = EnumKey($bk, $Index)
	Next
	For Each $element in $KeyName
		$conn = enumkey($bk,$c)
		If (SubStr($element,3,12) = $Server) = 0
			$c= $c + 1
			$conn= delkey($bk + "\" + $conn)
			$conn= enumkey($bk,$c)
		EndIf
	Next
EndFunction

;================================================================
;= Função        : ENumGroupIni
;= Autor         : Kent Dyer
;= UDF Site      : http://www.kixtart.org/UDF/UDF_lister.php?what=post&code=84431
;= Contribuintes : Jens Meyer
;=                 Howard Bullock
;= Changes       : Roberto Valentim
;= Descrição     : Numera todos os grupos que estão no arquivo ini
;=                  que deverá ser informado pelo parametro $file
;=                  e o servidor no qual o grupo faz parte pelo
;=                  pelo parametro $Server.
;= Sintaxe       : ENumGroupIni($file)
;= Parâmetros    : $file
;=                 Arquivo para ser lido e usado
;= Retorna       : Nada
;= Dependências  : Nenhuma
;= Exemplo(s)    : ENumGroupIni("C:\print.ini")
;================================================================
Function ENumGroupIni($file)
;================================================================
	DIM $GrpArray, $Grupo, $GrpChave, $Chave, $Valores, $Valor

	$GrpArray = split(readprofilestring($file, "", ""), chr(10))
	For Each $Grupo in $GrpArray
		If ($Grupo <> "")
			If InGroup("$Grupo") = 0
				$InGroups = $InGroups + "$Grupo"
	      $GrpChave = split(readprofilestring($file, $Grupo, ""), chr(10))
				For Each $Chave in $GrpChave
					If ($Chave = "Printers")
						$Valores = split(readprofilestring($file, $Grupo, $Chave), ",")
						For Each $Valor in $Valores
							AddPrinterConnection("$Valor)
						Next
					EndIf

					If ($Chave = "Default")
						$Valores = split(readprofilestring($file, $Grupo, $Chave), ",")
						For Each $Valor in $Valores
							SetDefaultPrinter("$Valor)
						Next
					EndIf
				Next
			Else
			EndIf
		EndIf
	Next
EndFunction

DelPrinterConnections("Server01")

ENumGroupIni("print.ini")


Edited by Valentim (2010-01-08 11:55 AM)
_________________________

Top
Page 1 of 1 1


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

Who's Online
0 registered and 155 anonymous users online.
Newest Members
dimi575, Drecksnacken, Nlill, webtools321, multiman
17734 Registered Users

Generated in 0.043 seconds in which 0.014 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