Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - REG_EXPAND_SZ only seems to expand some variables

Reply
 
Old 03-17-2009   #1 (permalink)
vorpal


 
 

REG_EXPAND_SZ only seems to expand some variables

Reference:
http://msdn.microsoft.com/en-us/libr...55(VS.85).aspx

Test code:
const HKEY_LOCAL_MACHINE = &H80000002
Const HKCU=2147483649

strComputer = "."
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &
strComputer & "\root\default:StdRegProv")
strKeyPath = "software\khpp\jbm"
strValueName = "TestVal"
Return = objReg.GetExpandedStringValue(HKCU,_
strKeyPath,strValueName,strValue)
If (Return = 0) And (Err.Number = 0) Then
WScript.Echo "The Test Value is: " & strValue
Else
Wscript.Echo _
"GetExpandedStringValue failed. Error = " & Err.Number
End If

I lifted this test code directly from the above, and it works.
I pointed the GetExpandedStringValue to a different value as you can
see.

If I put "%windir% in TestVal, the output is "c:\windows"
If I put %LOGONSERVER% in TestVal, the output is "%LOGONSERVER%

Is there somewhere that documents which environment variables are
expanded?
Is there a way in vbScript to expand the others w/o resorting to
the ExpandEnvironmentStrings method?
I can live with ExpandEnvironmentStrings if I must, but I would like
to understand more about why I must.

Cheers,
Vorpal


:

My System SpecsSystem Spec
Old 03-17-2009   #2 (permalink)
Pegasus [MVP]


 
 

Re: REG_EXPAND_SZ only seems to expand some variables


"vorpal" <brumac@xxxxxx> wrote in message
news:2219260c-8fff-41ad-8910-6abea179946e@xxxxxx
Quote:

> Reference:
> http://msdn.microsoft.com/en-us/libr...55(VS.85).aspx
>
> Test code:
> const HKEY_LOCAL_MACHINE = &H80000002
> Const HKCU=2147483649
>
> strComputer = "."
> Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &
> strComputer & "\root\default:StdRegProv")
> strKeyPath = "software\khpp\jbm"
> strValueName = "TestVal"
> Return = objReg.GetExpandedStringValue(HKCU,_
> strKeyPath,strValueName,strValue)
> If (Return = 0) And (Err.Number = 0) Then
> WScript.Echo "The Test Value is: " & strValue
> Else
> Wscript.Echo _
> "GetExpandedStringValue failed. Error = " & Err.Number
> End If
>
> I lifted this test code directly from the above, and it works.
> I pointed the GetExpandedStringValue to a different value as you can
> see.
>
> If I put "%windir% in TestVal, the output is "c:\windows"
> If I put %LOGONSERVER% in TestVal, the output is "%LOGONSERVER%
>
> Is there somewhere that documents which environment variables are
> expanded?
> Is there a way in vbScript to expand the others w/o resorting to
> the ExpandEnvironmentStrings method?
> I can live with ExpandEnvironmentStrings if I must, but I would like
> to understand more about why I must.
>
> Cheers,
> Vorpal
You need to break your problem down into its invididual components in order
to resolve it. The first question is, of course: Do expanded string values
always get resolved properly, even when they contain environmental variables
themselves? I recommend you do this:
1. Manually create a couple of variables in HKCU\Environment, e.g.
vorpal1=%windir%
vorpal2=%logonserver%
2. Insert the following commands into your logon batch file:
echo Windir=%windir%
echo Logonserver=%logonserver%
echo Vorpal1=%vorpal1%
echo Vorpal2=%vorpal2%
pause

This was the first step. If this step does not already answer your question
then you should invoke your VB Script from the above batch file, just before
the Pause command. If there is a discrepancy in the output you see then it
may be necessary to examine your VB Script in greater detail.


My System SpecsSystem Spec
Old 03-18-2009   #3 (permalink)
bmacd5153


 
 

Re: REG_EXPAND_SZ only seems to expand some variables

On Mar 17, 1:26*am, "Pegasus [MVP]" <n...@xxxxxx> wrote:
Quote:

> You need to break your problem down into its invididual components in order
> to resolve it. The first question is, of course: Do expanded string values
> always get resolved properly, even when they contain environmental variables
> themselves? I recommend you do this:
> 1. Manually create a couple of variables in HKCU\Environment, e.g.
> * * vorpal1=%windir%
> * * vorpal2=%logonserver%
> 2. Insert the following commands into your logon batch file:
> * * echo Windir=%windir%
> * * echo Logonserver=%logonserver%
> * * echo Vorpal1=%vorpal1%
> * * echo Vorpal2=%vorpal2%
> * * pause
>
> This was the first step. If this step does not already answer your question
> then you should invoke your VB Script from the above batch file, just before
> the Pause command. If there is a discrepancy in the output you see then it
> may be necessary to examine your VB Script in greater detail.-
Thanks,
The test is an interesting one
When run, the script will expand any variable reliably as long as the
environment variable to be expanded is defined in HKLM\System
\CurrentControlSet\Control\Session Manager\Environment.

All others are ignored.
This is the code that is used to retrieve them:
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &
Machine & "\root\default:StdRegProv")
nRV = oReg.GetExpandedStringValue(hTree, sKeyPath, sValueName,
vValue)

I suspected the "Impersonate" section, but the results were the same
when I removed it. nRV is "0" after execution of the second line, so
it is finding the values in the registy, it is just not expanding the
contents UNLESS the variable is in system enviroment. Variables added
by the process, or variables in the User's default environment are not
expanded. I can find nothing that documents this, nor do I find
others complaining about it.

--Vorpal
My System SpecsSystem Spec
Old 03-18-2009   #4 (permalink)
Pegasus [MVP]


 
 

Re: REG_EXPAND_SZ only seems to expand some variables


<bmacd5153@xxxxxx> wrote in message
news:3e8b52f6-c37a-4697-92cf-3112308c1f6a@xxxxxx
On Mar 17, 1:26 am, "Pegasus [MVP]" <n...@xxxxxx> wrote:
Quote:

> You need to break your problem down into its invididual components in
> order
> to resolve it. The first question is, of course: Do expanded string values
> always get resolved properly, even when they contain environmental
> variables
> themselves? I recommend you do this:
> 1. Manually create a couple of variables in HKCU\Environment, e.g.
> vorpal1=%windir%
> vorpal2=%logonserver%
> 2. Insert the following commands into your logon batch file:
> echo Windir=%windir%
> echo Logonserver=%logonserver%
> echo Vorpal1=%vorpal1%
> echo Vorpal2=%vorpal2%
> pause
>
> This was the first step. If this step does not already answer your
> question
> then you should invoke your VB Script from the above batch file, just
> before
> the Pause command. If there is a discrepancy in the output you see then it
> may be necessary to examine your VB Script in greater detail.-
Thanks,
The test is an interesting one
When run, the script will expand any variable reliably as long as the
environment variable to be expanded is defined in HKLM\System
\CurrentControlSet\Control\Session Manager\Environment.

All others are ignored.
This is the code that is used to retrieve them:
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &
Machine & "\root\default:StdRegProv")
nRV = oReg.GetExpandedStringValue(hTree, sKeyPath, sValueName,
vValue)

I suspected the "Impersonate" section, but the results were the same
when I removed it. nRV is "0" after execution of the second line, so
it is finding the values in the registy, it is just not expanding the
contents UNLESS the variable is in system enviroment. Variables added
by the process, or variables in the User's default environment are not
expanded. I can find nothing that documents this, nor do I find
others complaining about it.

--Vorpal
==============

You can use the code below to extract any environmental variable, regardless
of the hive where it is stored.

Set wshShell = CreateObject("WScript.Shell")
For Each strEnv In Array("PROCESS", "SYSTEM", "USER", "VOLATILE")
WScript.Echo VbCrLf & VbCrLf & strEnv & "-Environment:" & VbCrLf
For Each str In wshShell.Environment(strEnv)
WScript.Echo wshShell.ExpandEnvironmentStrings(str)
Next
Next


My System SpecsSystem Spec
Old 03-19-2009   #5 (permalink)
vorpal


 
 

Re: REG_EXPAND_SZ only seems to expand some variables

On Mar 18, 10:12*am, "Pegasus [MVP]" <n...@xxxxxx> wrote:
Quote:

> <bmacd5...@xxxxxx> wrote in message
>
> ==============
>
> You can use the code below to extract any environmental variable, regardless
> of the hive where it is stored.
>
> Set wshShell = CreateObject("WScript.Shell")
> For Each strEnv In Array("PROCESS", "SYSTEM", "USER", "VOLATILE")
> * * WScript.Echo VbCrLf & VbCrLf & strEnv & "-Environment:" & VbCrLf
> * * For Each str In wshShell.Environment(strEnv)
> * * * * WScript.Echo wshShell.ExpandEnvironmentStrings(str)
> * * Next
If I'm not mistaken, though the code above only works on the machine
on which the script is running.
WMI works on remote machines, and it is for that reason I wanted to
use it.
--Vorpal

--Vorpal
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
math with "GB/MB/KB" in variables fails, without variables works? PowerShell
Expand Sub Directories Vista General
Expand C drive Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46