Valentim
(Fresh Scripter)
2010-01-04 09:03 PM
Adding Printers separating groups

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


Mart
(KiX Supporter)
2010-01-04 11:55 PM
Re: Adding Printers separating groups

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


Valentim
(Fresh Scripter)
2010-01-05 01:18 PM
Re: Adding Printers separating groups

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


BradV
(Seasoned Scripter)
2010-01-05 01:48 PM
Re: Adding Printers separating groups

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


Valentim
(Fresh Scripter)
2010-01-05 02:36 PM
Re: Adding Printers separating groups

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


Valentim
(Fresh Scripter)
2010-01-05 03:00 PM
Re: Adding Printers separating groups

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?


BradV
(Seasoned Scripter)
2010-01-06 12:43 PM
Re: Adding Printers separating groups

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.


Glenn BarnasAdministrator
(KiX Supporter)
2010-01-06 01:03 PM
Re: Adding Printers separating groups

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


Valentim
(Fresh Scripter)
2010-01-06 01:06 PM
Re: Adding Printers separating groups

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


Valentim
(Fresh Scripter)
2010-01-07 12:17 PM
Re: Adding Printers separating groups

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?


Glenn BarnasAdministrator
(KiX Supporter)
2010-01-07 12:24 PM
Re: Adding Printers separating groups

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


Valentim
(Fresh Scripter)
2010-01-07 01:03 PM
Re: Adding Printers separating groups

ran the script with the parameter "/f" and the result was the same

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


Glenn BarnasAdministrator
(KiX Supporter)
2010-01-07 02:07 PM
Re: Adding Printers separating groups

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


Richard H.Administrator
(KiX Supporter)
2010-01-07 02:08 PM
Re: Adding Printers separating groups

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


Valentim
(Fresh Scripter)
2010-01-07 03:00 PM
Re: Adding Printers separating groups

 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


Kdyer
(KiX Supporter)
2010-01-07 09:42 PM
Re: Adding Printers separating groups

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


Richard H.Administrator
(KiX Supporter)
2010-01-08 09:31 AM
Re: Adding Printers separating groups

 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?


Valentim
(Fresh Scripter)
2010-01-08 11:52 AM
Re: Adding Printers separating groups

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")