Snickers
(Fresh Scripter)
2017-03-30 12:10 PM
Map Printer on TS based on client IP

Hello

I have looked at a script that maps a printer based on your client IP address. It works but I don't think it will work in my set up.

We have Windows Terminal servers and Linux thin clients with reserved IP addresses.

After the thin clients boots the MS RDP client program starts. Students log on with their normal account. Is it at the log on process that a kix script would need to map the right printer based on the IP address of the linux client ?


Mart
(KiX Supporter)
2017-03-30 01:59 PM
Re: Map Printer on TS based on client IP

That is what I would do. Map the correct printer(s) when they logon to the terminal server.

Snickers
(Fresh Scripter)
2017-03-30 02:42 PM
Re: Map Printer on TS based on client IP

Can you give an example script please?

ps, Chuck Norris would never have had a film/tv career if it hadn't been for his part in one of Bruce Lee's films.


Arend_
(MM club member)
2017-03-30 06:35 PM
Re: Map Printer on TS based on client IP

 Originally Posted By: Snickers
ps, Chuck Norris would never have had a film/tv career if it hadn't been for his part in one of Bruce Lee's films.


Chuck Norris allowed Bruce Lee to create a standard as Chuck's standard is too high for anyone ;\)


Snickers
(Fresh Scripter)
2017-03-31 11:27 AM
Re: Map Printer on TS based on client IP

can you help me with my script issue ?

Mart
(KiX Supporter)
2017-03-31 12:10 PM
Re: Map Printer on TS based on client IP

Below is an example. Obviously you know your environment better then we do so the script may need to be customized but the basics should give you an idea how to proceed.

Make sure you use the correct printer names for AddPrinterConnection and SetDefaultPrinter. The first takes the sharename of the printer and the second takes the name of the printer. I made it a habit to keep them the same so I never get issues with that.

 Code:
If Not @LOGONMODE
	Break on
EndIf

If SessionType() = "Remote desktop session"
	$rc = AddPrinterConnection("\\server\printer")
	;If you want the printer to become the default printer uncomment the line below
	;$rc = Setdefaultprinter("\\server\printer")
EndIf


;========BELOW IS A USER DEFINED FUNCTION AKA UDF========
;========THE CODE BELOW IS READY FOR USE========
;
;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, $session
	
	$sessionname = ExpandEnvironmentVars(%sessionname%)
	$session = Left($sessionname, 3)
	Select
	Case $Session = "Con"
	$sessiontype = "Console session"
	Case $session = "RDP"
	$sessiontype = "Remote desktop session"
	Case $session = "ICA"
	$sessiontype = "Citrix ICA session"
	Case 1
	$sessiontype = "Unknown session type"
	EndSelect
EndFunction


Off topic: Chcuk Noris actually can divide by zero.
Off topic2: Clint Eastwood: Chuck Norris? Never heard of her!


Snickers
(Fresh Scripter)
2017-03-31 12:42 PM
Re: Map Printer on TS based on client IP

I think I understand what this script does.

It identifies what type of session has been created by the client and there are three possible types. Presumably you would map a different printer based on what kind of session is set up.

In our environment we only use one type of connection out of the possible three there are in the script - RDP.

So the script needs to map the right printer based on the client IP rather than the session type.

I want to map the printer that is in the same classroom as the linux-based thin clients.


Snickers
(Fresh Scripter)
2017-03-31 01:09 PM
Re: Map Printer on TS based on client IP

Can this be used when linux-based thin clients are used to RDP to a Windows 2008R2 Terminal server?
 Code:
If Left(@WKSTA, 6) = "be_105"
	;Delete the printer.
	$rc = DelPrinterConnection("printer_for_room_BE")
	;Connect the printer.
	$rc = AddPrinterConnection("\\printerserver\printer_for_room_BE")
	;Set the printer as default.
	$rc = SetDefaultPrinter("printer_for_room_BE")
Else
	;Do nothing.
EndIf

Taken from:

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


The clients do have unique hostnames.


Mart
(KiX Supporter)
2017-03-31 02:59 PM
Re: Map Printer on TS based on client IP

I do not have any Linux to test with but below are some comments.

 Code:
If Left(@WKSTA, 6) = "be_105"
....


Will return the first 6 characters of the machine the script is running on. The TS in your case. If you need the client computername (the workstation the users is behind) you could use ExpandEnvironmentVars(%clientname%)instead of @WKSTA.

The else is not needed if there is no code required there. Deleting a printer connection, creating it and making it default is one way of getting it done. Depending on personal preference it is also an option to check if the printer is there and set it or not depending on the results. I use the function PriMapState for that. This function is available in the UDF library.


Mart
(KiX Supporter)
2017-03-31 03:00 PM
Re: Map Printer on TS based on client IP

I'm not sure how and if it is posisble to get the client IP when starting up a TS session.

Snickers
(Fresh Scripter)
2017-04-03 08:30 AM
Re: Map Printer on TS based on client IP

I just tested this code on a linux thin client:

 Code:
;--------Start IF_ELSE.kix--------
If Left(@WKSTA, 1) = "e"
;Delete the printer.
$rc = DelPrinterConnection("PR2-A-E9-17")
;Connect the printer.
$rc = AddPrinterConnection("\\cur-prs\PR2-A-E9-17")
;Set the printer as default.
$rc = SetDefaultPrinter("\\cur-prs\PR2-A-E9-17")
Else
;Do nothing.
EndIf
;-------- end IF_ELSE.kix--------


It works. The printer is only mapped if the user logs on from a computer where the hostname begins with an "e".


Snickers
(Fresh Scripter)
2017-04-03 04:56 PM
Re: Map Printer on TS based on client IP

Just seen that the printer only installs if you log on with an admin account. Is there a command for making the script run with an elevated status ?

AllenAdministrator
(KiX Supporter)
2017-04-03 06:15 PM
Re: Map Printer on TS based on client IP

If you are having the same problem I've seen lately, it could be the printer drivers themselves causing this problem. Microsoft changed something, especially in Windows 10 that causes printer drivers to prompt for admin perms. Unfortunately, some manufacturers are not replacing the drivers and there is no work around for what has worked for years.

Snickers
(Fresh Scripter)
2017-04-04 08:46 AM
Re: Map Printer on TS based on client IP

the clients are linux thin clients. I will look into "runnas" and see if that can solve my problem.

ShaneEP
(MM club member)
2017-04-04 07:48 PM
Re: Map Printer on TS based on client IP

I have not used TS in a long time. Does TS still have install mode vs execute mode? If I recall correctly, we used to install all printer drivers as an admin while in install mode, then they would work for users that logged in and mapped to them. I may be getting my wires crossed though. And it was also before UAC prompt crap.