David_Figueroa
(Fresh Scripter)
2012-10-08 03:58 AM
Pull a registry value into an environment variable

I'm trying to read a registry value, and place it into an environment variable. (I have had absolutely zero luck even reading the value).

Once I get that, I'd like to try and find a way to fire a wm_changenotify message (probably through .net?). I'm a decent vbscripter, and a pretty good winbatch scripter.. I'm picking kix32 back up now..

Any help would be appreciate it..


ShaneEP
(MM club member)
2012-10-08 06:04 AM
Re: Pull a registry value into an environment variable

Which registry key? You would want to use the ReadValue() function to read the value, you can then use one of the Set commands to set the environment variable.

ShaneEP
(MM club member)
2012-10-08 06:09 AM
Re: Pull a registry value into an environment variable

Here is a short example...
 Code:
$value = ReadValue("HKCU\Volatile Environment","HOMEDRIVE")

Set "NewEnvVarName = "+$value


Glenn BarnasAdministrator
(KiX Supporter)
2012-10-08 02:19 PM
Re: Pull a registry value into an environment variable

Just be aware that you can't pass an environment var back to the program that called the Kix script..

You can set a variable in:
  • the LOCAL environment that will be visible to any child process started by SHELL or RUN (SETL Command);
  • the SYSTEM environment that will be visible to any new process started by any user (SETM Comand);
  • the USER environment that will be visible to any new process started by the current user. (SET command). This will send a notify command to the environment and be updated by applications that support it.
If you want to pass a value back, you'll need to output the value or write it to a file that can be read by the parent process. The variable you set will not be visible to the currently running Kix script either.

Glenn


David_Figueroa
(Fresh Scripter)
2012-10-08 03:42 PM
Re: Pull a registry value into an environment variable

I'm trying to pull the CLIENTNAME information out of HKLM\Software\Citrix\ICA\Session.
So, my commands I'm trying to use are:

DIM $ClientName
$CLIENTNAME = READVALUE("HKLM\Software\Citrix\ICA\Session", "ClientName")

As a test, I tried to just write it to another part of the registry...

WriteValue("HKCU\Software\Test", "KixTest", $CLIENTNAME, "REG_SZ")

The value gets created properly, but the actual data in the value isn't written. If I manually set $CLIENTNAME before the WriteValue, then it works properly.

The flip side of this, is I wanted to get a "permanent" environment variable set with this data for subsequent processes. I know it will not be visible in the current environment, so my hope was to find a way to write it to the registry (to create a system level environment variable - {easy}) a fire off a wm_changenotify system message in order to force windows to revaluate the environment {hard}.

My preference is to stick with Kix, because this is going to be a pre-flight execution script for Desktop Authority.

Thanks :-)


Glenn BarnasAdministrator
(KiX Supporter)
2012-10-08 04:18 PM
Re: Pull a registry value into an environment variable

Have you verified that $ClientName contains data after the registry read? (Display to screen)
Display the @SERROR value after the WriteValue - what do you get?

The SetM() command will create a machine (System) for all users.

Glenn


Les
(KiX Master)
2012-10-08 04:32 PM
Re: Pull a registry value into an environment variable

Are you sure that reg value exists? It does not on the version of Citrix I use. Also, teh enviro var %CLIENTNAME% is set by the client on my system.

David_Figueroa
(Fresh Scripter)
2012-10-08 05:42 PM
Re: Pull a registry value into an environment variable

This is for XenDesktop. By default, it does not set ClientName.. you have to use the SessionStateMonitor for that, and I need that information *before* SSM runs.

I *know* the key/value contains data - I have triple checked it with regedt32.exe. I'm not really sure how to display it to the screen.. I have tried calling it directly, using MessageBox (the box comes up with my text, but not the variable information for $CLIENTNAME). I've tried using the debug mode..

I'm hoping that setm will work as I hope.. but until I get some good data to put into it I can't be sure that I'm doing any of this correctly.

(again, thanks for the help.. :-)


AllenAdministrator
(KiX Supporter)
2012-10-08 05:43 PM
Re: Pull a registry value into an environment variable

Here is a function that never fully got tested. If it works for you and or your situation, might you follow up in the other thread.

Clientname() -
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=204608#Post204608


David_Figueroa
(Fresh Scripter)
2012-10-08 06:02 PM
Re: Pull a registry value into an environment variable

I looked at the function, and it won't work for XD. For XA it works (at least in Win2k8 or Win2k8R2) because the sessionID is part of %temp% (i.e. if my session ID is 23, then my temp variable really points to %userprofile%\appdata\local\temp\23).

In XD, this is not valid. :-\ The CLIENTNAME is absolutely in the registry for XD (It is for XA also, but it is also predicated on sessionID (hklm\software\citrix\ica\session\\clientname.

I'll login from another machine, and I can post some screenshots.

Again, I really appreciate the help.. I have been racking my brains for 3 days on this.


ShaneEP
(MM club member)
2012-10-08 06:03 PM
Re: Pull a registry value into an environment variable

Putting this line should display what you read from the registry.
 Code:
$null = messagebox($CLIENTNAME,"",0)


David_Figueroa
(Fresh Scripter)
2012-10-08 06:42 PM
Re: Pull a registry value into an environment variable

That is what I thought ShaneEP.. I tried it and got nothing, and I tried it again after this morning and got nothing.

AllenAdministrator
(KiX Supporter)
2012-10-08 06:53 PM
Re: Pull a registry value into an environment variable

Just a thought... is this a 64bit OS?

At the top of the script, try...
 Code:
$RC=setoption("WOW64AlternateRegView","On")


David_Figueroa
(Fresh Scripter)
2012-10-09 03:52 AM
Re: Pull a registry value into an environment variable

It is a 64bit that I'm developing and testing on. I'll try it and post the results.. if it is it, then I will feel like an idiot :-D

Thanks!


David_Figueroa
(Fresh Scripter)
2012-10-09 04:04 AM
Re: Pull a registry value into an environment variable

That was definitely part of it. I am now getting a registry value when I read.. It never even occurred to me to look for an option to do that. :-)

My issue during testing now is I can't seem to write that value to HKCU (this is just a test.. )

I can't thank everyone enough :-)


ShaneEP
(MM club member)
2012-10-09 03:07 PM
Re: Pull a registry value into an environment variable

I tried it on my 64 bit Win7 laptop. The following reads and writes the registry value once I added Allen's suggestion. Can you check the error code after you try the write?

 Code:
$nul = setoption("WOW64AlternateRegView","On")

$CLIENTNAME = READVALUE("HKLM\Software\Citrix\ICA\Session", "ClientName") 

$nul = WriteValue("HKCU\Software\Test", "KixTest", $CLIENTNAME, "REG_SZ")

$nul = MessageBox("Error Number: "+@Error+@CRLF+"Error Text: "+@SError,"status",0)


AllenAdministrator
(KiX Supporter)
2012-10-09 04:33 PM
Re: Pull a registry value into an environment variable

David, would you mind trying the clientname() udf again? I added your reg setting to it, and it would be good to know if it at least works for your environment.

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