ddady
(Getting the hang of it)
2014-10-20 09:30 AM
Setting Default Printer from partial name

Hi all. It's been a while since i've been here.
My problem is as follows:

We are testing Citrix system in our company. We had a lot of problems with the printers. Finally we mange to find a solution to our printing problems but we stumbled into another one. Once the user log in to the Citrix, a new Citrix Generic printer is created. The printer name is as follows:"Citrix Universal Generic (From TC2) in session 3". I need to make that printer a default. The problem is that the name of the printer changes from user to user depends on it's workstation name and session number. The only thing that's not changing is the beginning of the printer name "Citrix Universal Generic".
Is there a way in Kix like in Windows systems that i can put something like Asterisk which will replace the changing parts of the printer name?

Thanks in advance \:\)


Mart
(KiX Supporter)
2014-10-20 10:13 AM
Re: Setting Default Printer from partial name

As far as I know there is no way to use a wildcard for setting the default printer but there must be a different solution to get it going.

You could use the SessionType() UDF to see if it is an ICA (citrix) session,check %CLIENTNAME% to get the name of the user's workstation and use the GetSessionID() UDF from the code below (could not find it in the UDF section) to get the session ID.

With all of this you should be able to construct the printer name like show below. Be careful because the code below is untested.

 Code:
Break on

If SessionType() = "ICA"
	;User is in an ICA session.

	;Get session number
	$session = GetSessionID()
	;Get client name
	$client = ExpandEnvironmentVars(%clientname%)
	;Set printer name
	$printer = "Citrix Universal Generic (From " + $client + ") in session " + $session
	;Set default printer
	$rc = SetDefaultPrinter($printer)
	;Check for errors
	If $rc <> 0
		? "There was an error."
		? @ERROR
		? @SERROR
	Else
		? "All is ok"
		? @ERROR
		? @SERROR
	EndIf
	Sleep 5	
EndIf

;FUNCTION      GetSessionID()
; 
;ACTION        Returns the Terminal Server session id 
; 
;AUTHOR        pearly 
; 
;CONTRIBUTORS  Richard H. (I'm almost reluctant to claiming this function as my own.  Thank you!) 
; 
;VERSION       1.0 
; 
;DATE CREATED  2005/03/06 
; 
;DATE MODIFIED 2005/03/06 
; 
;KIXTART       KiXtart 4.20 
; 
;SYNTAX        GetSession() 
; 
;PARAMETERS    None. 
; 
;RETURNS       Integer. 
; 
;REMARKS       Uses %TEMP% environment variable. 
; 
;DEPENDENCIES  None. 
; 
;EXAMPLE       Dim $session 
;			   $session = GetSessionID() 
; 
;KIXTART BBS    
; 
Function GetSessionID()
	Dim $s_sessionid
	Dim $iIndex
	$GetSessionID = 0
	$s_sessionid = SubStr(%TEMP%, 1 + InStrRev(%TEMP%, "\"))
	While $s_sessionid <> ""
		$iIndex = InStr("0123456789ABCDEF", Left($s_sessionid, 1))
		If $iIndex
			$GetSessionID = $GetSessionID * 16 + (CInt($iIndex - 1))
			$s_sessionid = SubStr($s_sessionid, 2)
		Else
			$GetSessionID = 0
			$s_sessionid = ""
		EndIf
	Loop
EndFunction


ddady
(Getting the hang of it)
2014-10-20 10:44 AM
Re: Setting Default Printer from partial name

Thank you Mart for the fast reply.
I will check the code today and will return with results.


AllenAdministrator
(KiX Supporter)
2014-10-20 03:06 PM
Re: Setting Default Printer from partial name

If you get a list of printers you should be able to figure out the printer with an instr() and set it that way.

PrinterList() -
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=84385#Post84385

How to use UDFs -
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=81943#Post81943

The rest of the UDFs are here -
http://www.kixtart.org/forums/ubbthreads.php?ubb=postlist&Board=7&page=1


ddady
(Getting the hang of it)
2014-10-20 03:58 PM
Re: Setting Default Printer from partial name

Mart, i can't thank you enough.
The script works like a charm.
I only had to change the SessionType which is "CtxMtHost" and not "ICA". We are testing Citrix XenDesktop system.

Again, Thank you Mart and also to Allen :-)


AllenAdministrator
(KiX Supporter)
2014-10-20 04:05 PM
Re: Setting Default Printer from partial name

Since you are using Xendesktop, would you be willing to test out another UDF, ClientName().

As far as I know this has never been tested in that environment, and it would be great to add that as something that also works.

http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=207561#Post207561


Mart
(KiX Supporter)
2014-10-20 04:40 PM
Re: Setting Default Printer from partial name

Thanks. Happy to help where I can.

So CtxMtHost is what you get for XenDesktop? If so then I'll update the UDF to support that one to. Do you also have XenApp or is that the same? If you do, what does it return on XenApp?

I don't use Citrix anymore so I'm not up to speed on all the products and versions.


ddady
(Getting the hang of it)
2014-10-21 06:42 AM
Re: Setting Default Printer from partial name

 Originally Posted By: Allen
Since you are using Xendesktop, would you be willing to test out another UDF, ClientName().

As far as I know this has never been tested in that environment, and it would be great to add that as something that also works.

http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=207561#Post207561


Hi Allen,

I'll gladly test the ClientName() UDF and i will return with results.


ddady
(Getting the hang of it)
2014-10-21 07:25 AM
Re: Setting Default Printer from partial name

 Originally Posted By: Mart
Thanks. Happy to help where I can.

So CtxMtHost is what you get for XenDesktop? If so then I'll update the UDF to support that one to. Do you also have XenApp or is that the same? If you do, what does it return on XenApp?

I don't use Citrix anymore so I'm not up to speed on all the products and versions.


Hi Mart,
Yes, CtxMtHost is what i get when connecting to XenDesktop.
I'll just give a short brief how XenDesktop works. Actually, we have 2 Host Session servers which creates a load balancing. Users login in into Citrix with a Citrix Reciever. There are 2 options to work with XenDesktop (as far as i know):
1) RDS
2) VDI

We decided to work with RDS which is actually Citrix Remote Desktop (equivalent to Microsoft's Remote Desktop 'RDP'). The two Host Session servers are equivalent to Microsoft TS Servers.

The VDI option is very different. It creates a full virtual machine for each user. This way requires more resources and fits to users who have special requirements such as heavy duty software and etc.

As far as i know, XenApp is a server that holds apps but users not actually open a session on it in order to work with the apps. Users still connect to the Host Session servers and there (in their session)they have an area like "Apps Store" which they can choose apps to work with.

Right now i'm learning the system so i'm not a 100% familiar with it. I will be glad to add more info about it as i go.


ddady
(Getting the hang of it)
2014-10-21 08:08 AM
Re: Setting Default Printer from partial name

 Originally Posted By: Allen
Since you are using Xendesktop, would you be willing to test out another UDF, ClientName().

As far as I know this has never been tested in that environment, and it would be great to add that as something that also works.

http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=207561#Post207561


Hi Allen,

Just finished testing the function.
I replaced in Mart's script:
 Code:
$client = ExpandEnvironmentVars(%clientname%)

with
 Code:
$client = ClientName()
.

It works like a charm :-)


Mart
(KiX Supporter)
2014-10-21 09:33 AM
Re: Setting Default Printer from partial name

Hi ddady,

Can you give the code below a go and see if it provides correct results?
I added XenDesktop supoort but cannot test this myself.

 Code:
Break on

SessionType()

Sleep 5



;Function	SessionType()
;
;Contributors:
;		Inspired by 
;			http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=195460
;
;Action:
;		Gets the session type of the current user.
;
;Syntax:
;		SessionType()
;
;Version:
;		1.0 August 20th 2009.
;			Initial post.
;
;Parameters:
;		None.
;
;Returns:
;		String value holding the session type.
;		Currently supports Console, RDP and ICA.
;
;Dependencies
;		No known dependencies
;
;KiXtart:
;		Developed and tested KiXtart 4.60 and Windows Vista Business SP2.
;
;Example(s):
;		Break on
;		$rc = SessionType()
;		$rc
;		Sleep 5
;
;Code:
Function SessionType()
	
	Dim $sessionname
	
	$sessionname = ExpandEnvironmentVars(%sessionname%)
	
	Select
		Case Left($sessionname, 3) = "Con"
			$sessiontype = "Console session"
		Case Left($sessionname, 3) = "RDP"
			$sessiontype = "Remote desktop session"
		Case Left($sessionname, 3) = "ICA"
			$sessiontype = "Citrix ICA session"
		Case Left($sessionname, 9) = "CtxMtHost"
			$sessiontype = "Citrix XenDesktop session"
		Case 1
			$sessiontype = "Unknown session type"
	EndSelect
EndFunction


ddady
(Getting the hang of it)
2014-10-21 11:00 AM
Re: Setting Default Printer from partial name

I get "Citrix ICA session".

Ok, I understand why. I got confused between the Process Name and the Session Name. The Session Name goes like this: "ICA-CGP#43" (the number at the end changes from user to user). The Process Name is the "CtxMtHost".

That makes me wonder how come the script still works fine?!


Mart
(KiX Supporter)
2014-10-21 11:50 AM
Re: Setting Default Printer from partial name

LOL ok then I will not update the UDF in the UDF section as it is actually correct.

Why your script still works? I don’t know. Can you post what you have running?


ddady
(Getting the hang of it)
2014-10-21 12:16 PM
Re: Setting Default Printer from partial name

That's the script i'm running:

 Code:
Break on

If SessionType() = "CtxMtHost"
	;User is in an ICA session.

	;Get session number
	$session = GetSessionID()
	;Get client name
	$client = ExpandEnvironmentVars(%clientname%)
	;Set printer name
	$printer = "Citrix UNIVERSAL Printer (from " + $client +") in session " + $session
	;Set default printer
	$rc = SetDefaultPrinter($printer)
	;Check for errors
	If $rc <> 0
		? "There was an error."
		? @ERROR
		? @SERROR
	Else
		? "All is ok"
		? @ERROR
		? @SERROR
	EndIf
	Sleep 5	
	
EndIf

;FUNCTION      GetSessionID()
; 
;ACTION        Returns the Terminal Server session id 
; 
;AUTHOR        pearly 
; 
;CONTRIBUTORS  Richard H. (I'm almost reluctant to claiming this function as my own.  Thank you!) 
; 
;VERSION       1.0 
; 
;DATE CREATED  2005/03/06 
; 
;DATE MODIFIED 2005/03/06 
; 
;KIXTART       KiXtart 4.20 
; 
;SYNTAX        GetSession() 
; 
;PARAMETERS    None. 
; 
;RETURNS       Integer. 
; 
;REMARKS       Uses %TEMP% environment variable. 
; 
;DEPENDENCIES  None. 
; 
;EXAMPLE       Dim $session 
;			   $session = GetSessionID() 
; 
;KIXTART BBS    
; 
Function GetSessionID()
	Dim $s_sessionid
	Dim $iIndex
	$GetSessionID = 0
	$s_sessionid = SubStr(%TEMP%, 1 + InStrRev(%TEMP%, "\"))
	While $s_sessionid <> ""
		$iIndex = InStr("0123456789ABCDEF", Left($s_sessionid, 1))
		If $iIndex
			$GetSessionID = $GetSessionID * 16 + (CInt($iIndex - 1))
			$s_sessionid = SubStr($s_sessionid, 2)
		Else
			$GetSessionID = 0
			$s_sessionid = ""
		EndIf
	Loop
EndFunction


AllenAdministrator
(KiX Supporter)
2014-10-21 03:43 PM
Re: Setting Default Printer from partial name

Thanks for the feedback! \:\)

Mart
(KiX Supporter)
2014-10-21 03:52 PM
Re: Setting Default Printer from partial name

Hmmmm....not sure why this works if SessionType returns ICA. I see as good as no changes to my code. You did include the the SessionType UDF right?

ddady
(Getting the hang of it)
2014-10-22 06:42 AM
Re: Setting Default Printer from partial name

 Originally Posted By: Mart
Hmmmm....not sure why this works if SessionType returns ICA. I see as good as no changes to my code. You did include the the SessionType UDF right?


Actually, i didn't \:o
That makes it even more strange.


LonkeroAdministrator
(KiX Master Guru)
2014-10-23 12:49 AM
Re: Setting Default Printer from partial name

Wtf? Something is not right here...

Mart
(KiX Supporter)
2014-10-23 09:21 AM
Re: Setting Default Printer from partial name

It should throw an error on If SessionType() = "CtxMtHost" if the UDF is not included or called earlier in the script. Somehow it returns CtxMtHost and continues the script. Unfortunately I cannot test anything Citrix related anymore. I also do not see anything in the code that may cause this.

ddady
(Getting the hang of it)
2014-10-23 09:55 AM
Re: Setting Default Printer from partial name

I'll try running it on debug mode and see if i can find something.

ddady
(Getting the hang of it)
2014-10-27 11:34 AM
Re: Setting Default Printer from partial name

Sorry for the delay.

I ran the script on debug mode.
It seems that the function works even though that i didn't write it in the script and the function recognize "CtxMtHost" as a session type cause it keeps runing to the next command which is "$session = GetSessionID()"

Is there a command i can add to the script that will display the value in the "SessionType()" function?

I tried adding this:
 Code:
Dim $a = SessionType()
? $a


and i get an error.
ERROR : unexpected command!
Script: \\172.30.168.229\netlogon\IL-CAR\citrixdefaultprinter.kix
Line : 5


LonkeroAdministrator
(KiX Master Guru)
2014-10-27 01:03 PM
Re: Setting Default Printer from partial name

That does not make sense. You should have got that error already oon the if line. And even if kix treats it as string, "sessiontype" does not equal your string.

Mart
(KiX Supporter)
2014-10-27 01:44 PM
Re: Setting Default Printer from partial name

You can display what SessionType() returns by simply typing

 Code:
SessionType()
sleep 5


It should give an error when the UDF is not included in the script.


Glenn BarnasAdministrator
(KiX Supporter)
2014-10-27 06:43 PM
Re: Setting Default Printer from partial name

You can't DIM and ASSIGN on a single line - try
 Code:
DIM $A
$A = SessionType()
Glenn


ddady
(Getting the hang of it)
2014-10-28 08:20 AM
Re: Setting Default Printer from partial name

Thank you Mart, I'll try it.

Thank you Glenn :-) BTW, until today i'm using your wonderful "Outlook Signature" script. Works like a charm.


ddady
(Getting the hang of it)
2014-10-28 09:17 AM
Re: Setting Default Printer from partial name

OK, when i run
 Code:
SessionType()
sleep 5


I get an error on the "SessionType()" line which means it can't run the function which its what it suppose to do on the line before that cause the function code is not in the script.

When i try
 Code:
DIM $A
	$A = SessionType()
	? $A


I just get in the console "SessionType()" as if the value in $A is a string.


Mart
(KiX Supporter)
2014-10-28 12:15 PM
Re: Setting Default Printer from partial name

You must include the SessionType() UDF otherwise it will never work as it should. Please add the UDF and try again.

LonkeroAdministrator
(KiX Master Guru)
2014-10-28 12:41 PM
Re: Setting Default Printer from partial name

Yes yes. But why the miracle behavior with if???

I guess someone needs to try and duplicate it...


Mart
(KiX Supporter)
2014-10-28 02:38 PM
Re: Setting Default Printer from partial name

I just dug up an old Citrix server (Metaframe presentation server) and it works fine on this server. Metaframe presentation server is now XenApp.

Does anyone have a XenDesktop server to test on?


ddady
(Getting the hang of it)
2014-10-28 04:18 PM
Re: Setting Default Printer from partial name

Most likely i will have a XenApp by the end of next week maybe even earlier.

I will add the UDF and will try again.