arjanv
(Fresh Scripter)
2011-05-26 10:56 AM
how to read a custom environment variable (XDClientName)

old situation:
printers are installed with a kix loginscript based on client computername.
 Code:
$computerprt = left (@wksta, 6)
select 
;Locatie 1
	case $computerprt = "LA-MAN"
		AddPrinterConnection ("\\printserver\printer1")
		SetDefaultPrinter("\\printserver\printer1")
;Locatie 2
	case $computerprt = "CA-DOC"
		AddPrinterConnection ("\\printserver\printer2")
		SetDefaultPrinter("\\printserver\printer2")
endselect


this works and i'm very happy with it.

But we are migrating some clients to XenDesktop 4(locked down), all these virtual clients have the same first 6 characters, only the number is different.
So my kix script isn't working anymore.

I found a little tool on the citrix website: http://support.citrix.com/article/CTX124963
this retrieves the original machines client name and puts them in the windows environment variable.

Now my question is how to let kix read this variable and use this to install the printers?(example above)

I hope my you can understand my question.(it's a bit messy)

thanks


BradV
(Seasoned Scripter)
2011-05-26 11:40 AM
Re: how to read a custom environment variable (XDClientName)

Why not just add another select at the begging? Such as:

 Code:
$computerprt = left (@wksta, 6)
select
;Xen systems
case left(@wksta, 6) = "xendes"
AddPrinterConnection ("\\printserver\printerxen")
SetDefaultPrinter("\\printserver\printerxen")
;Locatie 1
case $computerprt = "LA-MAN"
AddPrinterConnection ("\\printserver\printer1")
SetDefaultPrinter("\\printserver\printer1")
;Locatie 2
case $computerprt = "CA-DOC"
AddPrinterConnection ("\\printserver\printer2")
SetDefaultPrinter("\\printserver\printer2")
endselect


arjanv
(Fresh Scripter)
2011-05-26 12:22 PM
Re: how to read a custom environment variable (XDClientName)

no that doesn't work.

some more info:
we have six locations and each location has it's own computernames
Example:
location 1 has computernames that begin with LA-MAN-XXX
Location 2 has computernames that begin with CA-MAN-XXX
so i can deploy printers to specific computers.

whitin citrix all computers (for all locations) have the same name(except the number)


BradV
(Seasoned Scripter)
2011-05-26 12:33 PM
Re: how to read a custom environment variable (XDClientName)

Sorry, I miss-understood. I thought all of your xen systems were at one location.

 Code:
$Y = "And this is how you access environment variables: %USERNAME%..."
So,
 Code:
$computerprt = "%XDClientName%"


arjanv
(Fresh Scripter)
2011-05-26 01:12 PM
Re: how to read a custom environment variable (XDClientName)

Thank you.

don't know what this mean: $Y = "And this is how you access environment variables: %USERNAME%..."

so i use this?
 Code:
$computerprt = "%XDClientName%"
$computerprt = Left(@wksta, 6)
Select
		;Xen systems
	Case Left(@wksta, 6) = "xendes"
		AddPrinterConnection("\\printserver\printerxen")
		SetDefaultPrinter("\\printserver\printerxen")
		;Locatie 1
	Case $computerprt = "LA-MAN"
		AddPrinterConnection("\\printserver\printer1")
		SetDefaultPrinter("\\printserver\printer1")
		;Locatie 2
	Case $computerprt = "CA-DOC"
		AddPrinterConnection("\\printserver\printer2")
		SetDefaultPrinter("\\printserver\printer2")
EndSelect


BradV
(Seasoned Scripter)
2011-05-26 02:13 PM
Re: how to read a custom environment variable (XDClientName)

Sorry, I was just trying to subtle about saying, "Read the manual." \:\)

That is a direct quote from the manual about how to read an environment variable.

I think you will have to first determine if the system is a virtual one or not. So,

 Code:
if xen = true
   $computerprt = "%XDClientName%"
else
   $computerprt = left (@wksta, 6)
endif


The way you have it, the variable will be overwritten even if it is a xen computer.


arjanv
(Fresh Scripter)
2011-05-26 02:28 PM
Re: how to read a custom environment variable (XDClientName)

ok thank you so much \:\)

i know i'm not good at it ;\)

where is the "xen" from if xen = true?

when i put all the code together is this correct?
 Code:
If xen = true
	$computerprt = "%XDClientName%"
Else
	$computerprt = Left(@wksta, 6)
EndIf

Select
		;Location 1
	Case $computerprt = "LA-MAN"
		AddPrinterConnection("\\printserver\printer1")
		SetDefaultPrinter("\\printserver\printer1")
		
		;Locatie 2
	Case $computerprt = "CA-DOC"
		AddPrinterConnection("\\printserver\printer2")
		SetDefaultPrinter("\\printserver\printer2")
EndSelect


Mart
(KiX Supporter)
2011-05-26 02:40 PM
Re: how to read a custom environment variable (XDClientName)

I think it should be $xen. It should be filled by some code in your script before trying to use it.

I use the SessionType() UDF for this. Check: UDF Library » SessionType() - Gets the session type for the current user
This UDF will check if you are on RDP, ICA (Citrix) or a console session.


Glenn BarnasAdministrator
(KiX Supporter)
2011-05-26 02:46 PM
Re: how to read a custom environment variable (XDClientName)

The first question is - are you running "xd4ClientName.exe" before you run the Kix login script? This might be one of those situations where you need to call the login script as a Batch file - the bat file would contain something like
 Code:
@Echo Off
Rem - set the XDCLIENTNAME environment var
XD4ClientName.exe
Rem - Run the Kix login script
kix32.exe kixtart.kix
This will insure that the environment var is defined and present when Kix runs. If you run the XD4ClientName.exe from within Kix, it will set the value in the child shell, which will then die when the command ends.

Running the command on a non-Xen system causes a 4-second delay before the app dies and does NOT set the variable. Thus, if this small delay is acceptable, you could modify your Kix script like this using the above bat file:
 Code:
; Get the computer name - either remote client or local PC as appropriate
$ComputerPrt = IIf(%XDClientName%,%XDCLientName%,@WKSTA)
; Trim to firtst six chars
$ComputerPrt = Left($ComputerPrt, 6)
The rest of your code remains the same. The process I have illustrated is done in 2 steps because IIF is picky about what you place in the args. It's also easier to follow the logic. Basically, if the environment var isn't empty, use that value for the computer name, otherwise get the name from the @WKSTA macro. Trim it to the first 6 chars and process as usual.

If the delay isn't acceptable (and it's better practice anyway), modify the batch file that launches the login script to detect if this is a Xen session and run the XD4ClientName.exe command only when on a Xen server. No changes to the Kix script are necessary since the environment var won't be set either way. I'm not in the office so I can't test the Xen server detection part.. someone else here may be able to help before I can test it further.

Glenn


arjanv
(Fresh Scripter)
2011-05-26 03:31 PM
Re: how to read a custom environment variable (XDClientName)

wow! that's a lot of information. my brain are getting overheated.

my appologies i'm so stupid.

As i'm said i have multiple locations. all location has it's own computernames.
All computernames has 10 charaters:
xx-yyy-zzz
xx = location example LA and CA
yyy = department example FAC (factory) and OFF (office)
zzz = the number of the computer example 001

On all locations some computers are locked-down and they logged in with a citrix virtualdesktop. All these virtual desktops has the computername: VA-CTX-001 (which the number varies)

Example: on location LA there are 2 users in an office and 1 printer.
One user has a regular desktop and logs on this desktop and the other user logs is with citrix. both user must get the same printer.

In the existing situation the logon script is defined in the ActiveDirectory. This logon script is a batch file which start the kix script:
 Code:
 
@Echo Off
net use P: \\server\App$
kix32.exe Printers.kix


and my originally kix script:
 Code:
$computerprt = left (@wksta, 6)

select 

	case $computerprt = "LA-FAC"
		AddPrinterConnection ("\\printserver\printer1")
		SetDefaultPrinter("\\printserver\printer1")

	case $computerprt = "LA-OFF"
		AddPrinterConnection ("\\printserver\printer2")
		SetDefaultPrinter("\\printserver\printer2")

	case $computerprt = "CA-REC"
		AddPrinterConnection ("\\printserver\printer3")
		SetDefaultPrinter("\\printserver\printer3")

	case $computerprt = "CA-ROO"
		AddPrinterConnection ("\\printserver\printer4")
		AddPrinterConnection ("\\printserver\printer1")
		SetDefaultPrinter("\\printserver\printer4")

endselect
 


Can someone make it complete? I would buy you a beer or so, but i'm from the netherlands, so i also can do a paypal deposit if needed ;\)


Glenn BarnasAdministrator
(KiX Supporter)
2011-05-26 03:59 PM
Re: how to read a custom environment variable (XDClientName)

Simple..

1. Copy the XD4ClientName.exe to your NetLogon folder
2. Add the XD4ClientName.exe command to your existing batch file, immediately after the existing "Net Use" command.
3. Replace the current line "$computerprt = left (@wksta, 6)" in your kix script with the following 2 lines:
 Code:
$ComputerPrt = IIf(%XDClientName%,%XDCLientName%,@WKSTA)
$ComputerPrt = Left($ComputerPrt, 6)
4. Test!

As I said, this will result in an approximate 4 second delay on non Xen systems. If you can identify the Xen computer from within the BATCH file, you could do something like this in your batch file instead..
 Code:
If %COMPUTERNAME%==xenserver   XD4ClientName.exe
so it only runs when connecting to a Xen server. Try the 4 steps above first to see how it works, then figure out how to identify the Xen server(s) in the batch file and run the XD4ClientName command only when on a Xen system.

Glenn




ShaneEP
(MM club member)
2011-05-26 07:24 PM
Re: how to read a custom environment variable (XDClientName)

Maybe Im looking at this wrong, but should this...

$ComputerPr = IIf(%XDClientName%,%XDCLientName,@WKSTA)

be this...

$ComputerPr = IIf(%XDClientName%,%XDCLientName%,@WKSTA)

?


arjanv
(Fresh Scripter)
2011-05-27 09:17 AM
Re: how to read a custom environment variable (XDClientName)

i'm confused now.

$ComputerPr = IIf(%XDClientName%,%XDCLientName,@WKSTA)
$ComputerPr = IIf(%XDClientName%,%XDCLientName%,@WKSTA)
$ComputerPrt = IIf(%XDClientName%,%XDCLientName,@WKSTA)
$ComputerPrt = IIf(%XDClientName%,%XDCLientName%,@WKSTA)

which?


Mart
(KiX Supporter)
2011-05-27 09:22 AM
Re: how to read a custom environment variable (XDClientName)

Use the one Shane suggested. Environment variables always start and end with a percentage character. Glenn made a small typo.

arjanv
(Fresh Scripter)
2011-05-27 09:47 AM
Re: how to read a custom environment variable (XDClientName)

First.Thank you all for your answers and great help \:\)

it's:
 Code:
$computerprt = IIf(%XDClientName%,%XDClientName%,@WKSTA)
$computerprt = Left($computerprt, 6)
 


i've test it, but it doesn't work. no printers.
the XDClientName.exe works,because the variable is written.
when i launch \\server\netlogon\kix32.exe logonscript.kix from start - run
then the printers are loaded.

how to solve this?

any idea?


Mart
(KiX Supporter)
2011-05-27 10:27 AM
Re: how to read a custom environment variable (XDClientName)

The code below is an example. It only has two locations but this can obviously be expanded.

It checks if the user is in an ICA session, if yes it should run XD4ClientName.exe and translate %XDClientName% variable into a kix style variable. It check the first two characters of the client name and can run whatever is needed when a case statements is true. It requires the SessionType UDF I linked to earlier.

 Code:
Break on

$sessiontype = SessionType()

If $sessiontype = "ICA"
	;Run XD4ClientName.exe here.
	$clientname = ExpandEnvironmentVars(%XDClientName%)
	Select
		Case Left($clientname, 2) = "LA"
			;Do LA stuff
		Case Left($clientname, 2) = "CA"
			;Do CA stuff
	EndSelect
EndIf


arjanv
(Fresh Scripter)
2011-05-27 11:24 AM
Re: how to read a custom environment variable (XDClientName)

now i'm lost \:\(

BradV
(Seasoned Scripter)
2011-05-27 11:47 AM
Re: how to read a custom environment variable (XDClientName)

Where are you lost? My previous post told you that you can't set a variable on one line and then re-set it on the next and expect it to have two different values. Go back to what Glenn said and use Mart's suggestion to fill in the "xen = true" part. That was just pseudo code.

So, have a batch file launch it. Run the XD4ClientName application in the batch file and then call your kix script. In your kix script, use the SessionType function as Mart suggested to do the "xen = true " test.

So, start with a batch file:
 Code:
@Echo Off
Rem - set the XDCLIENTNAME environment var
XD4ClientName.exe
Rem - Run the Kix login script
kix32.exe kixtart.kix


In your kixtart.kix file:

 Code:
$sessiontype = SessionType()

If $sessiontype = "ICA"
   $computerprt = left ("%XDClientName%", 6)
else
   $computerprt = left (@wksta, 6)
endif

select 
   case $computerprt = "LA-FAC"
      AddPrinterConnection ("\\printserver\printer1")
      SetDefaultPrinter("\\printserver\printer1")
   case $computerprt = "LA-OFF"
      AddPrinterConnection ("\\printserver\printer2")
      SetDefaultPrinter("\\printserver\printer2")
   case $computerprt = "CA-REC"
      AddPrinterConnection ("\\printserver\printer3")
      SetDefaultPrinter("\\printserver\printer3")
   case $computerprt = "CA-ROO"
      AddPrinterConnection ("\\printserver\printer4")
      AddPrinterConnection ("\\printserver\printer1")
      SetDefaultPrinter("\\printserver\printer4")
endselect


Is that any clearer?


arjanv
(Fresh Scripter)
2011-05-27 12:15 PM
Re: how to read a custom environment variable (XDClientName)

ok, i'm gonna try it right away.

but do i need to load this also: http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=195471

and i was wondering where is the "ICA" comes from? from this line: If $sessiontype = "ICA"


arjanv
(Fresh Scripter)
2011-05-27 01:16 PM
Re: how to read a custom environment variable (XDClientName)

i've tried this.
 Code:
  
@Echo Off
Rem - set the XDCLIENTNAME environment var
XD4ClientName.exe
Rem - Run the Kix login script
kix32.exe kixtart.kix


 Code:
$sessiontype = SessionType()

If $sessiontype = "ICA"
   $computerprt = left ("%XDClientName%", 6)
else
   $computerprt = left (@wksta, 6)
endif

select 
   case $computerprt = "LA-FAC"
      AddPrinterConnection ("\\printserver\printer1")
      SetDefaultPrinter("\\printserver\printer1")
   case $computerprt = "LA-OFF"
      AddPrinterConnection ("\\printserver\printer2")
      SetDefaultPrinter("\\printserver\printer2")
   case $computerprt = "CA-REC"
      AddPrinterConnection ("\\printserver\printer3")
      SetDefaultPrinter("\\printserver\printer3")
   case $computerprt = "CA-ROO"
      AddPrinterConnection ("\\printserver\printer4")
      AddPrinterConnection ("\\printserver\printer1")
      SetDefaultPrinter("\\printserver\printer4")
endselect


also tried this:

 Code:
Break on

$sessiontype = SessionType()

If $sessiontype = "ICA"
	Run XD4ClientName.exe
	$clientname = ExpandEnvironmentVars(%XDClientName%)

	Select
		Case Left($clientname, 6) = "LA-DOC"
		AddPrinterConnection ("\\VA-PRN-001\LYC - ALG - op1020")
		AddPrinterConnection ("\\VA-PRN-001\LYC - LHB - HP8000")
		AddPrinterConnection ("\\VA-PRN-001\LYC - ROO - HP7700")
		SetDefaultPrinter("\\VA-PRN-001\LYC - ALG - op1020")

	EndSelect
EndIf

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


none work.


Glenn BarnasAdministrator
(KiX Supporter)
2011-05-27 01:26 PM
Re: how to read a custom environment variable (XDClientName)

I've corrected the typo's in my example. Sorry - I must have lost a char when I cut/paste parts of the original script.

The problem with Mart's example is that it performs the Citrix detection and runs the command to set the environment var from Kix. This won't work because the value is set in the child environment of the Run/Shell command, where it will disappear when the comand ends. The environment var must be set BEFORE Kix runs. There's no benefit of detecting physical or XEX session state within kix for your requirement.

Further - my "IIF" example will work and will not require any additional logic.

When you are on a regular PC and run the login script, the %XDClientName% will be empty. This causes the IIF logic to evaluate to "FALSE" and return the @WKSTA value. The command run from the bat file (or not, if you add the detection process there) will insure that the environment variable is not set.

However, when you login to a Xen session, the batch file runs the command which successfully sets the environment var. When the Kix script runs, the IIF evaluates to TRUE and returns the environment variable.

Either way, the $computerprt will contain the name of the PHYSICAL computer that the user is sitting at. Take the first six chars and continue with your code to determine the location and map the printers.

Now, for diagnostics..
In your Batch file, BEFORE you call the Kix script, add the line
 Code:
Set > %TEMP%\%USERNAME%_env.txt
This will capture the user's login environment settings to a file. Go to that folder after you log in and open it. Verify that the XDClientName variable is defined, and if so, is defined the way you expect. If it isn't, you need to troubleshoot the XD4ClientName.exe command.

Add the line
 Code:
'XD Client Name: ' %XDClientName% @CRLF
to your Kix script to verify that it sees the proper var value. Add this just before the IIF statement in my example. Add another debug message after the IIF block to view the contents of the $computerprt var. The end result should be:
 Code:
'XD Client Name: ' %XDClientName% @CRLF
$ComputerPrt = IIf(%XDClientName%,%XDCLientName%,@WKSTA)
$ComputerPrt = Left($ComputerPrt, 6)
'ComputerPrt: $ComputerPrt @CRLF
If this doesn't work, post the contents of the %TEMP%\%USERNAME%_env.txt file and a screenshot of the debug messages from the code above.

Glenn


Glenn BarnasAdministrator
(KiX Supporter)
2011-05-27 01:36 PM
Re: how to read a custom environment variable (XDClientName)

One more thing.. Since Xen is VDI and not the same as a Citrix RDP session, the use of session type detection AND forcing it to be ICA format may not be valid. I'll be able to get into the lab this afternoon and confirm what the sessiontype returns on our Xen system, but for what you want to do, it just complicates things - remove it for now.

Glenn


Mart
(KiX Supporter)
2011-05-27 02:30 PM
Re: how to read a custom environment variable (XDClientName)

 Originally Posted By: arjanv
ok, i'm gonna try it right away.

but do i need to load this also: http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=195471

and i was wondering where is the "ICA" comes from? from this line: If $sessiontype = "ICA"


If you use the code Brad posted then you need to include the SessionType UDF. It will not work without the UDF. The ICA part comes from the UDF. It checks the session the user is in and returns RDP (remote desktop), ICA (Citrix) or CON (console).


Glenn BarnasAdministrator
(KiX Supporter)
2011-05-27 02:36 PM
Re: how to read a custom environment variable (XDClientName)

Mart,

Have you confirmed that it will return "ICA" from a Xen Desktop connection under all conditions?

This isn't a traditional Citrix server, and I don't connect to my Xen server using an ICA client, so this may be messing withthings a bit.. That's why I sugggest avoiding the session-type determination entirely.

If the env var is set, the session is a Xen system and the var contains the client computer name, otherwise it is blank. This keeps the process simple, since all that's needed is to identify the client computer name to determine the user's physical location.

Glenn


Mart
(KiX Supporter)
2011-05-27 03:10 PM
Re: how to read a custom environment variable (XDClientName)

Sorry, I do not have any Xen servers but I assumed that it works. Might not be 100% correct. I do not have any Xen servers so I'm unable to test.

Is anybody using Xen servers and able to do a small test?


Glenn BarnasAdministrator
(KiX Supporter)
2011-05-27 03:52 PM
Re: how to read a custom environment variable (XDClientName)

You must be suffering from springtimeitis.. I noted that I have a Xen server, which is why I stated that "I don't connect using an ICA client..." \:D

I know it's been a long, cold winter, but stop staring out the window at the pretty women and pay attention (at least momentarily) to the task at hand.

I'll get back to my office this afternoon and verify what is actually reported from the Xen server. I've been on the road for a few days and haven't had time. I'm concerned that - unlike a traditional Citrix server - there are multiple connection methods for Xen and they might not all report the same condition in SessionType.

Glenn

PS - feel free to resume daydreaming \:\) I'm out in an hour and looking forward to a 3.5 day weekend and my own daydreaming!


Mart
(KiX Supporter)
2011-05-27 04:56 PM
Re: how to read a custom environment variable (XDClientName)

 Originally Posted By: Glenn Barnas
You must be suffering from springtimeitis.. I noted that I have a Xen server, which is why I stated that "I don't connect using an ICA client..." \:D

I know it's been a long, cold winter, but stop staring out the window at the pretty women and pay attention (at least momentarily) to the task at hand.
....


SIR YES SIR!

LOL yeah. The weather is ok, it has been a rough week and it is almost weekend so I might be a little distracted.
Let me know what you find on your Xen server. If it is possible I'll update the UDF to report RDP, CON, ICA or XEN (or whatever comes out).

kr,
Marty


ShaneEP
(MM club member)
2011-05-27 06:48 PM
Re: how to read a custom environment variable (XDClientName)

On a side note...Did a bit of testing here...The XDClientName.exe throws an error when run on the PC side since it is trying to use a dll that is only present on citrix servers. So may have to verify that its a server in the BAT file before running.

ShaneEP
(MM club member)
2011-05-27 09:47 PM
Re: how to read a custom environment variable (XDClientName)

Another idea...You may be able to avoid using the XDClientName.exe altogether if someone can figure out how to connect to the wfAPI library via kix? If that's possible?

http://community.citrix.com/display/xa/How+to+get+Client+IP+and+hostname+using+Citrix+WFAPI+SDK


Glenn BarnasAdministrator
(KiX Supporter)
2011-05-27 11:34 PM
Re: how to read a custom environment variable (XDClientName)

Shane,

Yeah, I noted that.. that's why it takes 3-4 seconds before returning with an error. Still, it did not populate the environemnt var. Not the cleanest solution, but would get through initial testing.

I'll look at your other recommendation - I might have some time in the coming days to try some things on our test Xen server.

Glenn


ShaneEP
(MM club member)
2011-05-27 11:47 PM
Re: how to read a custom environment variable (XDClientName)

Yea the 3-4 seconds isn't bad. But on my xp machine it actually halted with a popup error asking to send the error to microsoft lol. Sorry I couldn't do more testing. No servers to test on.

Glenn BarnasAdministrator
(KiX Supporter)
2011-05-28 02:48 AM
Re: how to read a custom environment variable (XDClientName)

Hmm.. I ran it from the command line - got an error after a few seconds, but no popup. If you run it from Explorer with double-click, that might be the case, but that doesn't represent the environment of a bat file.

Glenn


ShaneEP
(MM club member)
2011-05-28 03:06 AM
Re: how to read a custom environment variable (XDClientName)

It was actually via a bat file (was trying to replicate the setup). It also does same thing on my windows 7 laptop. Get the 'XDClientName has stopped working' box with options to check online for solution or to close the program. After I close that box, the expected error shows in the cmd window. But regardless...just my observations. There has got to be a better way to get this info from the server via kix alone. Im not familiar with xen, but would the old wtsmanager work in that environment?

http://kixhelp.com/wr/files/kixtart/wtsmanager.zip


ShaneEP
(MM club member)
2011-05-28 05:57 PM
Re: how to read a custom environment variable (XDClientName)

Got to thinking about this more...I cant get the xdclientname.exe to run on my system so i dont know the exact syntax of what it displays, but according to the docs it does display the client name on the console...SO...Why not use WshPipe to get it? And of course there is still the fact of only running on a server...so still need to figure out how to distinguish servers from desktops. But something like this should work in theory?...

 Code:
If $xen
   $computerprt = Join(WshPipe(@ScriptDir+"\XD4ClientName.exe",1))
Else
   $computerprt = Left(@WkSta,6)
Endif

Select 
   Case $computerprt = "LA-FAC"
      AddPrinterConnection ("\\printserver\printer1")
      SetDefaultPrinter("\\printserver\printer1")
   Case $computerprt = "LA-OFF"
      AddPrinterConnection ("\\printserver\printer2")
      SetDefaultPrinter("\\printserver\printer2")
   Case $computerprt = "CA-REC"
      AddPrinterConnection ("\\printserver\printer3")
      SetDefaultPrinter("\\printserver\printer3")
   Case $computerprt = "CA-ROO"
      AddPrinterConnection ("\\printserver\printer4")
      AddPrinterConnection ("\\printserver\printer1")
      SetDefaultPrinter("\\printserver\printer4")
   Case 1
endselect

Function WshPipe($ShellCMD, OPTIONAL $NoEcho)
	Dim $oExec, $Output
	$oExec = CreateObject("WScript.Shell").Exec($ShellCMD)
	If Not VarType($oExec)=9 $WshPipe="WScript.Shell Exec Unsupported" Exit 10 EndIf
	$Output = $oExec.StdOut.ReadAll + $oExec.StdErr.ReadAll
	If Not $NoEcho $Output Endif
	$WshPipe=Split(Join(Split($Output,CHR(13)),''),CHR(10))
	Exit($oExec.ExitCode)
EndFunction


arjanv
(Fresh Scripter)
2011-05-30 09:25 AM
Re: how to read a custom environment variable (XDClientName)

@Glenn

I've test it a couple of times and it works. but indeed i get the popup error asking to send the error to microsoft lol.

any way to fix this?


Mart
(KiX Supporter)
2011-05-30 09:30 AM
Re: how to read a custom environment variable (XDClientName)

 Originally Posted By: arjanv
@Glenn

I've test it a couple of times and it works. but indeed i get the popup error asking to send the error to microsoft lol.

any way to fix this?


Not with Kix.
You'll have to ask the developer what is going on and if it can be fixed.


arjanv
(Fresh Scripter)
2011-05-30 04:22 PM
Re: how to read a custom environment variable (XDClientName)

is it an idea to add a wait/pause in the bat file before xdclientname is loading?

Mart
(KiX Supporter)
2011-05-30 04:39 PM
Re: how to read a custom environment variable (XDClientName)

You can always try. I guess it will not fix the error but some testing doesn't hurt.

ShaneEP
(MM club member)
2011-05-30 06:16 PM
Re: how to read a custom environment variable (XDClientName)

i think the WshPipe method may work fine eliminating the need for a bat altogether, all thats needed is a way to keep it only running on the servers.

arjanv
(Fresh Scripter)
2011-06-06 10:12 AM
Re: how to read a custom environment variable (XDClientName)

i've tried this, but doesn't work at all.

 Code:
If $xen
   $computerprt = Join(WshPipe(@ScriptDir+"\XD4ClientName.exe",1))
Else
   $computerprt = Left(@WkSta,6)
Endif

Select 
   Case $computerprt = "LA-FAC"
      AddPrinterConnection ("\\printserver\printer1")
      SetDefaultPrinter("\\printserver\printer1")
   Case $computerprt = "LA-OFF"
      AddPrinterConnection ("\\printserver\printer2")
      SetDefaultPrinter("\\printserver\printer2")
   Case $computerprt = "CA-REC"
      AddPrinterConnection ("\\printserver\printer3")
      SetDefaultPrinter("\\printserver\printer3")
   Case $computerprt = "CA-ROO"
      AddPrinterConnection ("\\printserver\printer4")
      AddPrinterConnection ("\\printserver\printer1")
      SetDefaultPrinter("\\printserver\printer4")
   Case 1
endselect

Function WshPipe($ShellCMD, OPTIONAL $NoEcho)
	Dim $oExec, $Output
	$oExec = CreateObject("WScript.Shell").Exec($ShellCMD)
	If Not VarType($oExec)=9 $WshPipe="WScript.Shell Exec Unsupported" Exit 10 EndIf
	$Output = $oExec.StdOut.ReadAll + $oExec.StdErr.ReadAll
	If Not $NoEcho $Output Endif
	$WshPipe=Split(Join(Split($Output,CHR(13)),''),CHR(10))
	Exit($oExec.ExitCode)
EndFunction
 


anyone a solution, i'm desperate:(


Mart
(KiX Supporter)
2011-06-06 11:40 AM
Re: how to read a custom environment variable (XDClientName)

What is in the $xen variable? I do not see it being filed before it is referenced. If it is not filled the Else section will always be true.

arjanv
(Fresh Scripter)
2011-06-06 12:01 PM
Re: how to read a custom environment variable (XDClientName)

what do you mean?
i've used the code as it is.


Mart
(KiX Supporter)
2011-06-06 01:32 PM
Re: how to read a custom environment variable (XDClientName)

Your script checks if $xen holds anything. If it is empty this check will fail and the Else statement will be executed. You need to put something in it to get a successful check. If you just do an If $xen check and $xen is null (as in empty) the check will never be true and the following lines will not be executed. It will skip to the Else statement if there is an Else statement.
I do not have a xen server to test with but it should be possible to see that the client in on a xen server and executed whatever is needed for xen sessions.

If you start a xen session and check all environment variables from the command line there might be one or more that can be used to determine if it is a xen session or not. Another option can be to check if one or more specific xen server files exist or if the hostname is your xen server. Once this is done you can get the client name and connect the printers they need and do all other stuff that needs to be done for the people.


arjanv
(Fresh Scripter)
2011-06-06 01:53 PM
Re: how to read a custom environment variable (XDClientName)

?

the session name is on both "console". If it was different then we didn't need the xdclientname right?


Mart
(KiX Supporter)
2011-06-06 03:17 PM
Re: how to read a custom environment variable (XDClientName)

Like I said I do not have a xen server for testing so I did not know that.

A method to identify the xen server should be implemented and the results of this check should be put in the $xen variable. Without this the script Shane posted and you tested is not going to work.


arjanv
(Fresh Scripter)
2011-06-06 03:44 PM
Re: how to read a custom environment variable (XDClientName)

hi,

did you find something hoe to solve this?

thanks


ShaneEP
(MM club member)
2011-06-06 05:35 PM
Re: how to read a custom environment variable (XDClientName)

I'm amazed that someone with so little knowledge of scripting, has been put in charge of script maintenance. Nevertheless, to get it to work without that error popping up there has to be someway to differentiate server from client. Are all of your servers named with a similar convention? Are they all on a separate IP range? Are they all connected over ICA so we can use sessiontype()? It is hard for us to be of more assistance when you are the only one with the Xen servers to test on.

arjanv
(Fresh Scripter)
2011-06-06 06:16 PM
Re: how to read a custom environment variable (XDClientName)

huh! wtf?! none of your sugestions work. you call youself "citrixman" we've got here 3 pages without a working solution, but it seems you know the answer. but won't tell it?
If it was that easy i wouldn't ask for it nor we have 3 pages needed.........1 reply was enough. but apperently it isn't that simple.

The work of Glenn is super.


ShaneEP
(MM club member)
2011-06-06 06:58 PM
Re: how to read a custom environment variable (XDClientName)

Well then use Glenn's work. He's a kix master for sure and nobody said his work isn't super. If you want that error message to pop every time it runs on a p.c. then go for it dude. All of my solutions are perfectly acceptable with just a bit of testing and correct implementation. The real problem is that you don't even know how to understand a simple If Endif statement in Kix...Resulting in 3 pages of you being confused by possible legitimate solutions.

"Can someone make it complete?", "i'm confused now.", "how to solve this?", "any idea?", "now i'm lost", "anyone a solution, i'm desperate:(", "what do you mean?".

And yes I created this account about 9 years ago (about how long i've been using kix as well) when I was a full time citrix server administrator. We are just volunteers on here trying to help people that need it. You come on here expecting to not have to do anything but buy somebody an imaginary beer and be spoon fed a complete solution to your problem. And if we knew your environment better, you may possibly have gotten one (were that nice). But we don't...So in conclusion...Go read some documentation! I have nothing more to say in this thread. But with all sincerity...Good luck!


Mart
(KiX Supporter)
2011-06-06 09:00 PM
Re: how to read a custom environment variable (XDClientName)

I'm sorry to say it but you’re right Shane.

Several solutions were offered but offering a 100% working solution is impossible without knowing the setup. Trying an offered solution which is a suggestion and is never offered as complete and working bullet proof code is great but some understanding of kixtart is required. Just copy and paste and then saying that it doesn't work is not the way to go.

All the members that replied have been more than fair and offered several solutions but are unable to give a fully working script because we do not know the full setup. I’ve been using kixtart since 1999 and did not find anything that could not be done with kixtart and possibly the help of one or two external DLL’s or applications. I'm sure there must be a way to check if the person is on a xen server and get the name of the client the person is using but not like this. Like said we are all volunteers and do this besides our regular job. Attacking someone on the nickname they use on this board or on the fact that they did not supply fully working code without knowing the complete setup is an insult and is just weak and unprofessional.

I’m willing to work (and I don’t think I’m the only one) on a solution but I need cooperation and not just “It doesn’t work what now?” otherwise I think my efforts are useless.


arjanv
(Fresh Scripter)
2011-06-06 10:27 PM
Re: how to read a custom environment variable (XDClientName)

about info? maybe you've missed post 1 and 16. (no offence)

i'm not attacking anyone.

i don't know hot to figure out how to sepparate the physical client from the virtual one, neither do you....it seems.

theer is no different session type.
the physical client is locked down with the citrix lockdown tool, so the physical client are presenting the virtual desktop logon screen.

it's possivle with the citrix session printers but this doesn't work always smooth. adn it's not very clear and messy. so i like kix way better.

yes maybe i'm stupid........i admit, but that doesn't mean i can't ask for specific help.

this is a decent forum, so don't say crap like..........just google. that is what a lot of people yells at forums....that's the easyest way.

right:)


Mart
(KiX Supporter)
2011-06-06 11:15 PM
Re: how to read a custom environment variable (XDClientName)

I did read each and every post in this thread but some info that can be helpful is not in any of them. You are right I do not know xen but I’m just trying to help to get things up and running for you. Nobody told you to Google and nobody called you stupid. Maybe you are just not that experienced in kix scripting. That’s how we all started. Sure you can ask for specific help. Someone may have an answer.

Ok. Let’s get going.
Facts:
  • Clients are locked down and immediately show the virtual desktop logon screen.
  • There is more than one xen server.
  • First six characters of the client name are unique to a specific location.
  • Printers should be set based on the client’s location.

Questions:
  • Do your xen servers have a specific naming layout that can be used to identify them (xenserver01, 02, etc...) or something like it? Maybe they are in a specific IP range or is there anything else specific to the xen servers that can be used to identify them in kix?
  • Is the clientname environment variable available in the virtual desktop session? If so then it might be useful to identify the client and set printers based on the client name. You can use the xd4clientname application but if it is available as an environment var then this is much easier.

Below is an example of what I'm after. It assumes that the name of the servers can be used to identify them but this can be changed if needed. It is pseudo code just to give you an idea. It is not a working script ;\)
 Code:
If InStr(@WKSTA, "XENSERVER")
	Shell @LDRIVE + "\XD4Clientname.exe"	
	$clientname = ExpandEnvironmentVars(%XDClientName%)
	Select
		Case Left($clientname, 6) = "LA-MAN"
			;add printer code for LA-MAN clients here.
		Case Left($clientname, 6) = "CA-DOC"
			;add printer code for CA-DOC clients here.
	EndSelect
Else
	?"Not on a xen server"
EndIf


Glenn BarnasAdministrator
(KiX Supporter)
2011-06-07 12:06 AM
Re: how to read a custom environment variable (XDClientName)

Mart,

Just to clarify, the XD4ClientName.exe can't be run from within Kix.. it sets an environment var that exists only within the subshell. It really must be called before running Kix to work. Calling it from WSHPipe might work, but it really would work best if WSHPipe called a bat file that called the EXE and then echo'd the environment var, so that WSHPipe only captured the variable data.

That said... one of the quickest ways to ostracize oneself is to expect complete answers to problems. Mart, Shane, and I have all posted examples based on what we know (in general about Citrix/Xen and specifically about what has been described. We all have an expectation that when someone asks for assistance, they will take the responses and try a few things, attempt some of the examples, and report back specific conditions.

The example that I posted will work, but requires you figure out how to define the batch file such that the XD4ClientName.exe runs only when logging onto a Xen server. Mart's Kix code won't work because the way the exe works, but his IDEAS ARE SPOT ON! His questions are valid! You need to try a few things on your own, in your specific environment. Something as simple as
 Code:
IF ^%COMPUTERNAME%^==^someXENserver^ xd4ClientName.exe
- with one IF for each Xen server, then the command will run only if logging into a Xen server. Try it, let us know if it worked. If it doesn't, report the error messages, not just that it didn't work.

Bottom line, it's your problem in your environment, you'll need to get your hands dirty - more so that the rest of us, simply because you're there.

We're all here because we're willing to help. Most times, that help is in the form of sharing ideas. There's the occasional "silver platter" code, and if you figure this out, I'd be happy to post the result in the code vault to help the next person struggling with this.

Glenn


arjanv
(Fresh Scripter)
2011-06-07 01:18 PM
Re: how to read a custom environment variable (XDClientName)

 Originally Posted By: Mart

Questions:
  • Do your xen servers have a specific naming layout that can be used to identify them (xenserver01, 02, etc...) or something like it? Maybe they are in a specific IP range or is there anything else specific to the xen servers that can be used to identify them in kix?
  • Is the clientname environment variable available in the virtual desktop session? If so then it might be useful to identify the client and set printers based on the client name. You can use the xd4clientname application but if it is available as an environment var then this is much easier.



we have a Provisioning server named(computername): va-ctx-prs1
and a presentation server named(computername): va-ctx-ddc1
virtual clients have the computername: va-ctx-001 ~ 200

without running xdclient the clientname environment variable is not available, when running xdclient(with error popup) the xdclient variable is present.

thanks


Mart
(KiX Supporter)
2011-06-08 09:32 AM
Re: how to read a custom environment variable (XDClientName)

A bit late but I was unable to reply yesterday afternoon.

The code below should do the trick. It assumes that the xd4cleintname application has already run from your batch file so the xdcleintname environment variable is filled with the client name.
It checks if the server name contains VA-CTX-DDC and sets the $xen variable to Yes or No. As far as I understand this is the only server clients connect to. It leaves out the 1 at the end to provide some flexibility when adding another server. If $xen is yes it gets the client name from the xdclientmane environment variable and checks the first 6 characters. If they match whatever you specify (the aaaaaa, bbbbbb, cccccc strings) it executes the code placed below the check. If $xen is anything else but Yes it can execute some other code if needed. This is an example to show the concept. The client name check needs to be modified to match the client names you use.

 Code:
If InStr(@WKSTA, "va-ctx-ddc")
	$xen = "Yes"
Else
	$xen = "No"
EndIf

If $xen = "Yes"
	$clientname = ExpandEnvironmentVars(%XDClientName%)
	Select
		Case Left($clientname, 6) = "aaaaaa"
			;Add code for aaaaaa systems here.
		Case Left($clientname, 6) = "bbbbbb"
			;Add code for bbbbbb systems here.
		Case Left($clientname, 6) = "cccccc"
			;Add code for cccccc systems here.
	EndSelect
Else
	;Place code for non xen sessions here if needed.
	;You can remove the Else section if nothing is needed for non xen sessions.
EndIf


NTDOCAdministrator
(KiX Master)
2011-06-08 09:45 AM
Re: how to read a custom environment variable (XDClientName)

 Originally Posted By: arjanv
huh! wtf?! none of your sugestions work. you call youself "citrixman"


Well I must say that your response is way out of line and I'm not going to be as nice as the other members here.

Please refrain from any more such out bursts or I will ban your account. People are here to help others and have a good time, not take abusive replies from others.

Just because this is the Internet does not give one the right to respond like that. You certainly would not do that in a room with men you'd never met before without taking the chance of a physical confrontation.

So let's please just drop any attitude and move on.

Thank you.


arjanv
(Fresh Scripter)
2011-06-08 09:49 AM
Re: how to read a custom environment variable (XDClientName)

I'll try this tomorrow, but i doubt if this will work, because users don't logon to a xen server. all users logged on to the same domaincontroller, or do i missing something here.

somehow i need xdclient to run when the (virtual)client computername begins with the VA-CTX and the load the kix script.

ps. does a loginscript loads earlier than a gpo logonscript? or doesn't that matter?

maybe a crappy solution, but also could try to load the script on the virtual client inself from example the "startup" folder.


arjanv
(Fresh Scripter)
2011-06-08 09:56 AM
Re: how to read a custom environment variable (XDClientName)

Thanks, looks promissing, i'll going to test this also tomorrow.

as an reply on NTDOC. my apologise, i'm deeply sorry, at first i found the citrixmans response also not nice.


Mart
(KiX Supporter)
2011-06-08 11:41 AM
Re: how to read a custom environment variable (XDClientName)

Great, let us know the outcome please.

In the batch file you could use %computername% to see if it is a xen server or not just like @wksta in kix it returns the name of the system. You can then start the xd4clientname app if it is a xen server or skip it if it is a regular system.

AD settings or the GPO version are both logon scripts so imho they should run at the same stage during the logon process.


Arend_
(MM club member)
2011-06-08 10:11 PM
Re: how to read a custom environment variable (XDClientName)

Maybe thinking too simple here but:
 Code:
If Left(@WKSTA,6) = "VA-CTX" AND CInt(Right(@WKSTA,3)) < 1000
  ;Do Stuff
EndIf


ShaneEP
(MM club member)
2011-06-08 11:52 PM
Re: how to read a custom environment variable (XDClientName)

I know I said I wasn't going to try to help anymore...but I can't help myself. (and I was really wanting to get it up to 4 pages ;\) )

Try this code... Again I can't test it completely because I don't have the servers necessary, but I know the .bat testing works, so as long as all of your presentation servers are named with the "xx-ctx-ddcx" convention, and %computername% is available on your servers, and your servers are 64 bit, it should work.
(if your servers are not 64 bit, change out XD4ClientName.exe to XDClientName.exe)

BAT FILE
 Code:
@Echo Off
net use P: \\server\App$

REM Sets tempcomp to chars 8-10 of computername
set tempcomp=%computername:~7,3%

REM If tempcomp is ddc then assumes server and runs
REM the /i is used to make it non case sensitive
if /i "%tempcomp%"=="ddc" (XD4ClientName.exe)

REM Runs the kix script
kix32.exe kixtart.kix

KIX SCRIPT
 Code:
$ComputerPrt = IIf(%XDClientName%,%XDCLientName%,@WKSTA)
$ComputerPrt = Left($ComputerPrt, 6)

select 
	case $computerprt = "LA-FAC"
		AddPrinterConnection ("\\printserver\printer1")
		SetDefaultPrinter("\\printserver\printer1")
	case $computerprt = "LA-OFF"
		AddPrinterConnection ("\\printserver\printer2")
		SetDefaultPrinter("\\printserver\printer2")
	case $computerprt = "CA-REC"
		AddPrinterConnection ("\\printserver\printer3")
		SetDefaultPrinter("\\printserver\printer3")
	case $computerprt = "CA-ROO"
		AddPrinterConnection ("\\printserver\printer4")
		AddPrinterConnection ("\\printserver\printer1")
		SetDefaultPrinter("\\printserver\printer4")
endselect


arjanv
(Fresh Scripter)
2011-06-09 02:38 PM
Re: how to read a custom environment variable (XDClientName)

thanks all!!

I don't have the time today to test all the options above.

First i've tested it manually. just logged on without script and then started xdclient.exe and then from start run: \\server\netlogon\kix32.exe kixscript.kix
this works.......so that's a start.

the examples from above posts i doubt if this can work because i don't think the client will see anything about the presentation servers. so it can't retreive the xen server name...or can it?

i'm going to try to test some more tomorrow.


also i was thinking: when i put a file on the c drive of the virtual machine with for example the name "citrix.txt" can we do a if exist citrix.txt and then load xdclient.exe

or let kix read the virtual computername e.g. va-ctx-*** and the load xdclient.exe (if computername is va-ctx then load xdclient.exe and loads the rest of the script.)

i was thinking out load.


Mart
(KiX Supporter)
2011-06-09 03:16 PM
Re: how to read a custom environment variable (XDClientName)

I think kix is able to get the name of the server using @wksta but there is now ay to be sure without testing.
Placing a file on the c drive can also be an option.


arjanv
(Fresh Scripter)
2011-06-09 04:02 PM
Re: how to read a custom environment variable (XDClientName)

just has a couple of minutes spare time.

i've test this, and the xdclient run without error(at least not noticable), and the environment variable is present.
 Code:
$ComputerPrt = left (@wksta, 6)
select
case $ComputerPrt = "VA-CTX"
run "\\server\netlogon\XDClientName.exe"
endselect
 


how to load the rest of the script? do i need a other kix script from the above? or can this run whithin the same script. How to combine these two?

 Quote:

$ComputerPrt = IIf(%XDClientName%,%XDCLientName%,@WKSTA)
$ComputerPrt = Left($ComputerPrt, 6)


select
case $ComputerPrt = "LA-ROO"
AddPrinterConnection ("\\VA-PRN-001\printer1")
SetDefaultPrinter("\\VA-PRN-001\printer1")

case $ComputerPrt = "LA-DOC"
AddPrinterConnection ("\\VA-PRN-001\printer1")
AddPrinterConnection ("\\VA-PRN-001\printer2")
SetDefaultPrinter("\\VA-PRN-001\printer1")

endselect


thanks


Mart
(KiX Supporter)
2011-06-09 04:32 PM
Re: how to read a custom environment variable (XDClientName)

Great that at least a part of it is working. The script below should do the trick for adding the printers.
The $rc = in front of the AddPrinterConnection and SetdefaultPrinter lines will catch the return codes of these function so they do not show on the screen.

 Code:
$ComputerPrt = Left(@wksta, 6)
If $ComputerPrt = "VA-CTX"
	Run "\\server\netlogon\XDClientName.exe"
	$client ExpandEnvironmentVars(%xdclientname%)
	Select
		Case Left($client, 6) "LA-ROO"
			$rc = AddPrinterConnection("\\VA-PRN-001\printer1")
			$rc = SetDefaultPrinter("\\VA-PRN-001\printer1")
		Case Left($client, 6) "LA-DOC"
			$rc = AddPrinterConnection("\\VA-PRN-001\printer1")
			$rc = AddPrinterConnection("\\VA-PRN-001\printer2")
			$rc = SetDefaultPrinter("\\VA-PRN-001\printer1")
	EndSelect
Else
	;Not on a xen server.
	;Do stuff for non xen sessions here.
EndIf


arjanv
(Fresh Scripter)
2011-06-10 11:55 AM
Re: how to read a custom environment variable (XDClientName)

Hey Mart,

I saw your post yesterday, and i was convinced that this should work.

but i've just tested it, and no printers are added, but the xdclient still works.


ps. this earlier script works, but that gives the xdclient error popup, but in that script the printers are added.
 Quote:

$ComputerPrt = IIf(%XDClientName%,%XDCLientName%,@WKSTA)
$ComputerPrt = Left($ComputerPrt, 6)

select
case $ComputerPrt = "LA-DOCK"
AddPrinterConnection ("\\VA-PRN-001\printer1")
SetDefaultPrinter("\\VA-PRN-001\printer1")



thank you so much for your effort.


Mart
(KiX Supporter)
2011-06-10 12:33 PM
Re: how to read a custom environment variable (XDClientName)

There are many ways to debug. You can use the /d parameter when starting the script but what I usually do is display some key variables on the screen by adding ?$varname just after they are set and/or modified. Adding some text might also help. If you add something like ?"Yep system name is xxxx" in the case statements you will see what case resolves to true (if any). Adding a Case 1 statement as the last Case will enable you to show or do something when all others fail. If all other case statements fail Case 1 will always be executed. Adding a sleep at the end will enable you to actually see the stuff on the screen.

A small example:
 Code:
Break on

$test = 1
? $test

$test = $test + 5
? $test

Select
	Case $test = 1
		? "Test equals 1"
		;Do stuff for 1.
	Case $test = 7
		? "Test equals 7"
		;Do stuff for 7
	Case 1
		? "All cases failed. Test equals: " + $test
		;Do stuff for everything but 1 and 7.
EndSelect

Sleep 10


arjanv
(Fresh Scripter)
2011-06-10 01:16 PM
Re: how to read a custom environment variable (XDClientName)

new problem. When running this.
$ComputerPrt = Left(@wksta, 6)
If $ComputerPrt = "VA-CTX"
Run "\\server\netlogon\XDClientName.exe"


the xdclient variable is present, but i've looked all the time at the system properties under advanced - systemvariables. there is it present, but when do a "set" from commandprompt the variable isn't there. when i then run the xdclient manually then it's also available when do the "set" command, and that's why no printers where added.
can this cause this xdclient runs too early for windows perhaps.

it's getting funnier every time;)

I found also a registry setting, maybe it's possible to use this.
within the virual client the physical computername is visible under:
HKLM\software\citrix\PortICA\uxpersistence\LA-DOC-001


arjanv
(Fresh Scripter)
2011-06-10 01:16 PM
Re: how to read a custom environment variable (XDClientName)

sorry i was aware of that.

Mart
(KiX Supporter)
2011-06-10 02:43 PM
Re: how to read a custom environment variable (XDClientName)

 Originally Posted By: arjanv
new problem. When running this.
$ComputerPrt = Left(@wksta, 6)
If $ComputerPrt = "VA-CTX"
Run "\\server\netlogon\XDClientName.exe"


the xdclient variable is present, but i've looked all the time at the system properties under advanced - systemvariables. there is it present, but when do a "set" from commandprompt the variable isn't there. when i then run the xdclient manually then it's also available when do the "set" command, and that's why no printers where added.
can this cause this xdclient runs too early for windows perhaps.
....


I got a small explanation from Glenn about the xdclientname app and it seems it does not run that well from kix. If you start it from a batch file and directly after this start the kix script it should be better.

 Quote:

....
it's getting funnier every time;)
....


Yes, that’s how it goes sometimes. At some time you will start pulling out your hair, wanting to throw the computer out the window and beat up the first person asking you "Is it done yet?" for no reason but just because you can. These things might be great to blow off some steam but they do not help to fix the issue

 Quote:

....
I found also a registry setting, maybe it's possible to use this.
within the virual client the physical computername is visible under:
HKLM\software\citrix\PortICA\uxpersistence\LA-DOC-001
....


Great! You could use ReadValue to get this registry value and start connecting printers based on the result. A small example below. It assumes that the registry key is HKLM\software\citrix\PortICA, the value is uxpersistence and its contents is the name of the physical client.
 Code:
$ComputerPrt = Left(@WKSTA, 6)
If $ComputerPrt = "VA-CTX"
	$client = ReadValue("HKLM\software\citrix\PortICA", "uxpersistence")
	Select
		Case Left($client, 6) = "LA-DOC"
			;Connect LA-DOC printers.
		Case Left($client, 6) = "LA-ROO"
			;Connect LA-ROO printers.
		Case Left($client, 6) = "aaaaaa"
			;Connect aaaaaa printers.
	EndSelect
Else
	;Do stuff for non xen sessions.
EndIf


ShaneEP
(MM club member)
2011-06-10 05:32 PM
Re: how to read a custom environment variable (XDClientName)

 Quote:
I got a small explanation from Glenn about the xdclientname app and it seems it does not run that well from kix. If you start it from a batch file and directly after this start the kix script it should be better.


This is why I suggested checking for server in the Bat file in my post above (top of page 4).


Glenn BarnasAdministrator
(KiX Supporter)
2011-06-10 07:26 PM
Re: how to read a custom environment variable (XDClientName)

err - yup!

The command to set the environment variable must be run BEFORE the kix script, AND MUST be run in the same environment where it was set. That is..

You must have a batch file that runs the EXE and then runs the Kix script. The bat file runs the EXE that sets the environment var, then runs the kix script. This way, the variable is present in the environment used by Kix. You can see this by running a SET >%TEMP%\before.txt before you run the EXE, and then a SET >%TEMP%\after.txt after the EXE. The only difference between the files should be that the ClientName is defined in After.txt.

You CAN NOT run the EXE from within kix because it creates a child environment, sets the var into the child environment, and then that environment dies.

The key, as we've been saying, is to find a method that runs from the batch file that determines if the session is run against a Xen client.

Glenn


ShaneEP
(MM club member)
2011-06-10 10:20 PM
Re: how to read a custom environment variable (XDClientName)

Would changing your BAT file to this get rid of the popup error?
 Code:
@Echo Off
REM net use P: \\server\App$

REM If va-ctx is in computername then runs
if /i "%computername:~0,6%"=="va-ctx" (XD4ClientName.exe)

REM Runs the kix script
kix32.exe kixtart.kix

And then just keep this in your kix script...
 Code:
$ComputerPrt = IIf(%XDClientName%,%XDCLientName%,@WKSTA)
$ComputerPrt = Left($ComputerPrt, 6)


arjanv
(Fresh Scripter)
2011-06-21 10:08 AM
Re: how to read a custom environment variable (XDClientName)

 Originally Posted By: Mart
 Originally Posted By: arjanv
new problem. When running this.
$ComputerPrt = Left(@wksta, 6)
If $ComputerPrt = "VA-CTX"
Run "\\server\netlogon\XDClientName.exe"


the xdclient variable is present, but i've looked all the time at the system properties under advanced - systemvariables. there is it present, but when do a "set" from commandprompt the variable isn't there. when i then run the xdclient manually then it's also available when do the "set" command, and that's why no printers where added.
can this cause this xdclient runs too early for windows perhaps.
....


I got a small explanation from Glenn about the xdclientname app and it seems it does not run that well from kix. If you start it from a batch file and directly after this start the kix script it should be better.

 Quote:

....
it's getting funnier every time;)
....


Yes, that’s how it goes sometimes. At some time you will start pulling out your hair, wanting to throw the computer out the window and beat up the first person asking you "Is it done yet?" for no reason but just because you can. These things might be great to blow off some steam but they do not help to fix the issue

 Quote:

....
I found also a registry setting, maybe it's possible to use this.
within the virual client the physical computername is visible under:
HKLM\software\citrix\PortICA\uxpersistence\LA-DOC-001
....


Great! You could use ReadValue to get this registry value and start connecting printers based on the result. A small example below. It assumes that the registry key is HKLM\software\citrix\PortICA, the value is uxpersistence and its contents is the name of the physical client.
 Code:
$ComputerPrt = Left(@WKSTA, 6)
If $ComputerPrt = "VA-CTX"
	$client = ReadValue("HKLM\software\citrix\PortICA", "uxpersistence")
	Select
		Case Left($client, 6) = "LA-DOC"
			;Connect LA-DOC printers.
		Case Left($client, 6) = "LA-ROO"
			;Connect LA-ROO printers.
		Case Left($client, 6) = "aaaaaa"
			;Connect aaaaaa printers.
	EndSelect
Else
	;Do stuff for non xen sessions.
EndIf


i've made a screenshot from regedit:


arjanv
(Fresh Scripter)
2011-06-21 10:09 AM
Re: how to read a custom environment variable (XDClientName)

 Originally Posted By: Glenn Barnas
err - yup!

The command to set the environment variable must be run BEFORE the kix script, AND MUST be run in the same environment where it was set. That is..

You must have a batch file that runs the EXE and then runs the Kix script. The bat file runs the EXE that sets the environment var, then runs the kix script. This way, the variable is present in the environment used by Kix. You can see this by running a SET >%TEMP%\before.txt before you run the EXE, and then a SET >%TEMP%\after.txt after the EXE. The only difference between the files should be that the ClientName is defined in After.txt.

You CAN NOT run the EXE from within kix because it creates a child environment, sets the var into the child environment, and then that environment dies.

The key, as we've been saying, is to find a method that runs from the batch file that determines if the session is run against a Xen client.

Glenn


when running the exe from the bat login file both bofore and after doesn't have the xdclient variable.


Mart
(KiX Supporter)
2011-06-21 04:07 PM
Re: how to read a custom environment variable (XDClientName)

It might be a bit tricky but enumerating the registry key and checking the name might work. This only work if there is just one sub key in HKLM\software\citrix\PortICA\uxpersistence\.

 Code:
$index = 0
$keyname = EnumKey("HKLM\software\citrix\PortICA\uxpersistence\", $index)
While @ERROR = 0
	Select
		Case InStr($keyname, "LA-DOC")
			;set printers
		Case InStr( $keyname, "LA-ROO")
			;set printers
		Case InStr($keyname, "aaaaaa")
			;set printers
	EndSelect
	$Index = $Index + 1
Loop


ShaneEP
(MM club member)
2011-06-21 07:34 PM
Re: how to read a custom environment variable (XDClientName)

While...Next?

Mart
(KiX Supporter)
2011-06-22 08:58 AM
Re: how to read a custom environment variable (XDClientName)

 Originally Posted By: ShaneEP
While...Next?


oops my bad. Had a sh#tty day day of very poor quality yesterday and did a quick and dirty code as an example. Fixed thanks.


arjanv
(Fresh Scripter)
2011-06-22 09:42 AM
Re: how to read a custom environment variable (XDClientName)

hmm. i just copy and past your script and this seems to work:)

but it gives error : NEXT without FOR!

i did not test it but does this also work on non citrix clients?
or goes that code after the NEXT?


Mart
(KiX Supporter)
2011-06-22 09:52 AM
Re: how to read a custom environment variable (XDClientName)

See my post just above yours. The Next should be replaced by Loop.

You need to put the code inside the code that checks if the person is on a xen server or not.


arjanv
(Fresh Scripter)
2011-06-22 12:03 PM
Re: how to read a custom environment variable (XDClientName)

 Originally Posted By: Mart
See my post just above yours. The Next should be replaced by Loop.

You need to put the code inside the code that checks if the person is on a xen server or not.


that must be inside the loop?

;for non xen clients
$computerprt = left (@wksta, 6)
Select
case $computerprt = "LA-DOC"
;Connect LA-DOC printers.
EndSelect


Mart
(KiX Supporter)
2011-06-22 05:01 PM
Re: how to read a custom environment variable (XDClientName)

Yes, it should be inside the loop.

Something like this (untested):
 Code:
$ComputerPrt = Left(@wksta, 6)
If $ComputerPrt = "VA-CTX"
	$index = 0
	$keyname = EnumKey("HKLM\software\citrix\PortICA\uxpersistence\", $index)
	While @ERROR = 0
		Select
			Case InStr($keyname, "LA-DOC")
				$client = Left($keyname, 6)
			Case InStr($keyname, "LA-ROO")
				$client = Left($keyname, 6)
			Case InStr($keyname, "aaaaaa")
				$client = Left($keyname, 6)
		EndSelect
		$Index = $Index + 1
		$keyname = EnumKey("HKLM\software\citrix\PortICA\uxpersistence\", $index)
	Loop
	Select
		Case $client = "LA-ROO"
			$rc = AddPrinterConnection("\\VA-PRN-001\printer1")
			$rc = SetDefaultPrinter("\\VA-PRN-001\printer1")
		Case $client = "LA-DOC"
			$rc = AddPrinterConnection("\\VA-PRN-001\printer1")
			$rc = AddPrinterConnection("\\VA-PRN-001\printer2")
			$rc = SetDefaultPrinter("\\VA-PRN-001\printer1")
	EndSelect
Else
	;Not on a xen server.
	;Do stuff for non xen sessions here.
EndIf


Some things can be combined but in the example above I kept all steps separated to make it more readable.


arjanv
(Fresh Scripter)
2011-07-05 11:25 AM
Re: how to read a custom environment variable (XDClientName)

hi, this works good.
but the big test will be when all users are back from vacation (over 6 weeks)

also like to integrate BGinfo in this script and delete old printer connections. anyone knows a best way to do this?

thanks, thanks, thanks


Mart
(KiX Supporter)
2011-07-06 09:19 AM
Re: how to read a custom environment variable (XDClientName)

There are several way to delete old printers.
What we do for example is enumerate all installed network printers and store it in a temp file, install all needed printers and store the names of the needed printers in the same temp file, at the end of the script read both values from the temp file, compare the "old" printer to the "new" printers and delete whatever is in the list of "old" printers and not in the list of "new" printers. See below for an example.

The example below requires some UDF's.
ArrayEnumKey() - Creates an array of names of the subkeys contained in a registry key
PriMapState() - Checks for existent printer connection
ArrayComp() - Compares two 1 Dimensional Arrays

 Code:
Break on

$printerlog = "c:\somefolder\printers.ini"


;Region Get all installed network printers.
;Set the key that should be enumerated.
$key = "HKCU\Printers\Connections"
;Enumerate network printers key.
$oldprinters = ArrayEnumKey($key)
;Do stuff for each value
For Each $value in $oldprinters
	;Split the value on the comma.
	$value = Split($value, ",")
	;Put the value back together so that is represents the correct path to the printer.
	$value1 = "\\" + $value[2] + "\" + $value[3]
	;Add the created value to a variable to be written to the printer log file.
	$oldprinter = $oldprinter + "~" + $value1
Next
;Write all old printers to the printer log file.
$rc = WriteProfileString($printerlog, "Printers", "OldPrinters", $oldprinter)
;endregion

;Region Add printers
;Map printer printer01
;Set printer name.
$printer = "\\Server01\printer01"
;Check group membership.
If InGroup("SomeGroup01")
	;Check if the printer is already installed.
	If Not PriMapState($printer)
		;Printer is not installed. Add printer.		
		$rc = AddPrinterConnection($printer)
	EndIf
	;Read all so far installed printers from the printer log file.
	$connected = ReadProfileString($printerlog, "Printers", "NewPrinters")
	;Add the printer name to a variable to be written to the printer log file.
	$connected = $connected + "~" + $printer
	;Write the new printers value to the printers log file.
	$rc = WriteProfileString($printerlog, "Printers", "NewPrinters", $connected)
EndIf

;Set printer name.
;Map printer printer02
$printer = "\\Server01\printer02"
;Check group membership.
If InGroup("SomeGroup02")
	;Check if the printer is already installed.
	If Not PriMapState($printer)
		;Printer is not installed. Add printer.		
		$rc = AddPrinterConnection($printer)
	EndIf
	;Read all so far installed printers from the printer log file.
	$connected = ReadProfileString($printerlog, "Printers", "NewPrinters")
	;Add the printer name to a variable to be written to the printer log file.
	$connected = $connected + "~" + $printer
	;Write the new printers value to the printers log file.
	$rc = WriteProfileString($printerlog, "Printers", "NewPrinters", $connected)
EndIf
;endregion

;Region Delete unneeded printers
;Read all old network printers from the printer log file.
$oldprinters = ReadProfileString($printerlog, "Printers", "OldPrinters")
;Check if there are any old printers in the list.
If $oldprinters <> ""
	;Split the old printers on the ~ character
	$oldprinters = Split($oldprinters, "~")
EndIf
;Check each element of the array.
For $i = 0 to UBound($oldprinters)
	;Remove extra ~ characters.
	$oldprinters[$i] = Join(Split($oldprinters[$i], "~"), "")
Next

;Read all new printers from the printer log file.
$newprinters = ReadProfileString($printerlog, "Printers", "NewPrinters")
;Check if there are any new printers in the list.
If $newprinters <> ""
	;Split the new printers on the ~ character
	$newprinters = Split($newprinters, "~")
EndIf
;Check each element of the array.
For $i = 0 to UBound($newprinters)
	;Remove extra ~ characters.
	$newprinters[$i] = Join(Split($newprinters[$i], "~"), "")
Next

;Compare the list of old and new printers.
$tobedeletedprinters = ArrayComp($newprinters, $oldprinters)
;Check if there are any printers that should be deleted.
If UBound($tobedeletedprinters) > -1
	For Each $tobedeletedprinter in $tobedeletedprinters
		;Delete each printer that should be deleted.
		$rc = DelPrinterConnection($tobedeletedprinter)
	Next
EndIf
;endregion


Adding BGInfo is easy. BGInfo has lots of command line parameters and can be scripted. You should create a config file first by starting BGInfo, adding the stuff you want to display and save the config file. If you start BGInfo.exe /? from a command windows you get an overview of all command line parameters.