?
Solved

New Environmental Variable

Posted on 2010-09-17
14
Medium Priority
?
449 Views
Last Modified: 2012-05-10
I am wanting to create a new environmental variable named something like shortname and have the value be %COMPUTERNAME:~0,-2%.

I want to be able to type echo %shortname% and see the computer name minus the last two characters.

I tried creating the environment variable by entering in "%COMPUTERNAME:~0,-2%." as the value (under environmental variables in Advanced system settings but it literally echoed "%COMPUTERNAME:~0,-2%."

How can I achieve this? My client OS is Windows 7. My end goal is to be able to use this variable in group policy preferences. I am wanting to deploy a registry key where the value of the registry key is the computer name minus the last two characters of the name.
0
Comment
Question by:Joseph Moody
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +3
14 Comments
 
LVL 3

Expert Comment

by:latchways
ID: 33701254
set <new varialbe name> = %COMPUTERNAME:~0,-2%

should do the trick
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 33701550
You can do the following at a command prompt:

SETX ShortName %COMPUTERNAME:~0,-2%

and that will set a new variable name ShortName into the permanent system variables.  But keep in mind that it will only resolve the computername at the time you run the STX command, if you change the computername later it will not automatically be reflected in the system variable.

I tried a few tests, it didn't look like you could create a new system variable referencing %computername% and have it get the value of computername into the new variable.

You could add a line to the autoexec.bat file with the setx in it, that way each time the system restarts it would at least be updated.

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33701974
Wow, when did SETX arrive?!!? Wow looks useful with the /f /a /d params etc.

Steve
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 56

Expert Comment

by:Bill Prew
ID: 33703308
It was originally in some of the resource kits, I know at least as far back as Windows 2000.  It's now in the base O/S as of Windows 7 also I believe.

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33703905
Ah thats why it was on this vanilla Win7 machine then.  Will have to have a play with it!
0
 
LVL 14
ID: 33704152
Hey All,

It appears to be that the only reason the requester wants to do this is so that he can set a registry key to be used in GPOs later..
 Therefore We should consider doing the addition to or substitution of creating the environment variable as the reg key is the ultimate solution for him.
The normal reg key where the Computer name is located is thus:
  • Key:    HKEY_LOCAL_MACHINE\system\currentcontrolset\control\computername
  • Value:    ComputerName
  • Type:    REG_SZ
  • Data:    [The name of the computer]
Since author's ultimate goal would best be served by adding this value minus the last two characters to every machine.  We could, using a logon script or GPO have this key created  at the same location:
  • Key:    HKEY_LOCAL_MACHINE\system\currentcontrolset\control\computername
  • Value:    ShortName
  • Type:    REG_EXPAND_SZ
  • Data:    %ComputerName:~0,-2%
When evaluated by a GPO this Registry value will always evaluate as the current computer name minus the last two characters, and you aren't creating a hard entry, it will change if you change the name of the computer to come other value without you having to re-run any scripts.

Thats the beauty of the string expansion feature of the Windows Registry ^^

To add this value run this:
REG ADD HKLM\system\currentcontrolset\control\computername /v Shortname /t REG_EXPAND_SZ /d ^%ComputerName:~0,-2^%
Then a GPO will be able to reference this value, and it will not be a string which could be differant than the actual value of the computer.

Furthermore, as Bill deftly pointed out above you can set this value as a system variable at any time by using SETX and either using %ComputerName:~0,-2% or in fact querying the registry for the value we've set here. ^^
REG ADD HKLM\system\currentcontrolset\control\computername /v Shortname /t REG_EXPAND_SZ /d ^%ComputerName:~0,-2^%

Open in new window

0
 
LVL 14
ID: 33704360
also @ Bill re: how to set the system variable and evalutate it correctly every time:


I don;t think this is particularly useful in this instance but there may be other times you'd want to use this. So one thing you can do is set and echo the variable like this if need be

SETX Shortname=^%ComputerName:~0,-2^%

THEN:

ECHO %ShortName%

This will echo the literal string: %ComputerName:~0,-2%
CALL ECHO %ShortName%

This will echo the computer's actual name minus the last two characters.

Since SET is Transient and SETX is permanent we'd run the SETX above and then on each system start-up you could run

CALL SET "ShortName=%ShortName%"

However in this case when it comes to the system variable we're all trying to reinvent the wheel.

If the process is a batch script it can just set short name every time it runs on a remote machine or simply just evaluate %ComputerName:~0,-2% when it needs to reference shortname.

If we're going to be setting the reg key for a GPO to evaluate, I think it's best to use the registry expansion method I posted above because that will be evaluated as a string equal to the computer's ACTUAL name minus the last two characters every time.


0
 
LVL 26

Expert Comment

by:pony10us
ID: 33704754
Wow.  another new command SETX to look into.  I don't have it natively on my XP box at work but will look into how to get it if possible.

This would have made life so much easier in so many ways.  :)

Thanks again Bill
0
 
LVL 22

Author Comment

by:Joseph Moody
ID: 33715590
Thank you for helping me so far! QCubed got me thinking and I would like to show what exactly I am wanting to do.
I have a registry key at HKLM\SOFTWARE\Policies\NetSupport\PCICTL\ConfigList\Standard that is named Startup\\BrowsePrefix. It is a REG_SZ
Netsupport is a remote desktop program that we use.  When the program starts up, it checks that BrowsePrefix key for a computer name prefix. I am wanting that key to have the computer name (minus the last two characters) in the data field of that key.
Right now, I have a ton of policies with that manually specify that key. I am wanting to go down to one policy that dynamically sets that key.
 
Thanks!
0
 
LVL 14
ID: 33716010
Add this to a startup script:

REG ADD "HKLM\SOFTWARE\Policies\NetSupport\PCICTL\ConfigList\Standard that is named Startup" /v "BrowsePrefix" /t REG_SZ /d "%ComputerName:~0,-2%"


-OR-

0
 
LVL 22

Author Comment

by:Joseph Moody
ID: 33716035
That worked great! Could I do that with group policy preferences?
0
 
LVL 14

Accepted Solution

by:
Ben Personick (Previously QCubed) earned 2000 total points
ID: 33716193
Sorry here is the full post

Add this to a start-up script:

REG ADD "HKLM\SOFTWARE\Policies\NetSupport\PCICTL\ConfigList\Standard that is named Startup" /v "BrowsePrefix" /t REG_SZ /d "%ComputerName:~0,-2%" /f

-OR-

Use this to be a bit more rock-solid in case you ever change the computer name but don't have the script running anymore:

REG ADD "HKLM\SOFTWARE\Policies\NetSupport\PCICTL\ConfigList\Standard that is named Startup" /v "BrowsePrefix" /t REG_EXPAND_SZ /d "^%ComputerName:~0,-2^%" /f
0
 
LVL 14
ID: 33716399
@JMoody,

Yes, you can set this to run as a start-up or logon script  via the associated GPOs.  Honestly you should only need to set this to be a start-up script, though you could set it as a logon script which woudl refresh the value whenever someone logs onto the computer but that requires they be local administrators when running the script so less useful, for sure.

(In the case of the second option where we change the key to be a REG_EXPAND_SZ it only needs to be done once per machine (assuming the key does not get over-written by your program.. so perhaps even still a start-up script woudl be most appropriate  ^^))

First you'll need to take the command you prefer REG_SZ or REG_EXPAND_SZ and put it in a batch file, open Notepad (don't use a rich text editor like word or wordpad!), put "@ECHO OFF" (Without the quotes)as the first line, put the second line to the the one you prefer from my expample, and save the batch file as something you will remember the purpose of (Lets say "SetRDPShortName.bat" -without thee quotes-) under "\\YourDomain.com\Netlogon\SetRDPShortName.bat" (quotes again not needed ^^).

Once you've done that:

(Lifted from http://support.microsoft.com/kb/198642 ^^):
"To configure the computer startup and shutdown scripts, start the Group Policy snap-in, expand Computer Configuration, expand Windows Settings, click Scripts (Startup/Shutdown), and then in the right pane, double-click the script that you want to configure. These scripts run on the Local System account."

Then simply browse to the script you just saved or type the path into the GPO "\\YourDomain.com\Netlogon\SetRDPShortName.bat" (here you may want to use quotes around the path, not sure)..

save it, apply it to an OU or the domain. select "Enforce".
@ECHO OFF
REG ADD "HKLM\SOFTWARE\Policies\NetSupport\PCICTL\ConfigList\Standard that is named Startup" /v "BrowsePrefix" /t REG_SZ /d "%ComputerName:~0,-2%" /f

Open in new window

0
 
LVL 22

Author Closing Comment

by:Joseph Moody
ID: 33716470
That did it!!!! This is the coolest thing ever!

Thank you for being able to come up with a solution even though my first question was not here specific enough!!!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many software programs on offer that will claim to magically speed up your computer. The best advice I can give you is to avoid them like the plague, because they will often cause far more problems than they solve. Try some of these "do it…
This process allows computer passwords to be managed and secured without using LAPS. This is an improvement on an existing process, enhanced to store password encrypted, instead of clear-text files within SQL
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question