Solved

New Environmental Variable

Posted on 2010-09-17
14
437 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
  • 5
  • 3
  • 2
  • +3
14 Comments
 
LVL 3

Expert Comment

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

should do the trick
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
Wow, when did SETX arrive?!!? Wow looks useful with the /f /a /d params etc.

Steve
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
Ah thats why it was on this vanilla Win7 machine then.  Will have to have a play with it!
0
 
LVL 11

Expert Comment

by:Ben Personick
Comment Utility
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 11

Expert Comment

by:Ben Personick
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 26

Expert Comment

by:pony10us
Comment Utility
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 21

Author Comment

by:Joseph Moody
Comment Utility
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 11

Expert Comment

by:Ben Personick
Comment Utility
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 21

Author Comment

by:Joseph Moody
Comment Utility
That worked great! Could I do that with group policy preferences?
0
 
LVL 11

Accepted Solution

by:
Ben Personick earned 500 total points
Comment Utility
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 11

Expert Comment

by:Ben Personick
Comment Utility
@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 21

Author Closing Comment

by:Joseph Moody
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial will teach you the basics of configuring your computer to improve its speed. It will also teach you how to disable programs that are running in the background simultaneously. This will be demonstrated using Windows 7 operating…
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now