I need to use a VBS script to set the environment variable %computername% to "blah". This seems like a simple enough task but for some reason I'm finding it difficult this morning. I can do this via a command prompt (set computername=blah) and I can get the value of the environment variable via VBS but I cant seem to find the correct syntax to override the environment variable. Any help would be greatly appreciated. Thanks!
How you create it determines its type and lifetime. If you want the variable to be available until the user logs off, you can create it as VOLATILE.
Set objShell = CreateObject("WScript.Shell")
objShell.Environment("VOLATILE")("MyVariable") = "This is some data to share"
And to read it back...
strValue = objShell.Environment("VOLATILE")("MyVariable")
Other options for the variable's type are SYSTEM, USER, and PROCESS. See here for a nice article describing the differences.
Related
fso.GetSpecialFolder(2) correctly returns the temp folder for me, when run from a VBS file. However, when run from within an Autodesk Revit journal file, which has historically been VBS compliant, I get back the proper path and a GUID after Temp. I had never seen this before, and I am unsure if this is perhaps a known issue in newer builds of Windows 10 (it's been about three years since I tested this), or is this more likely an issue with Autodesk's implementation of VBScript support? I suspect the latter.
Which then raises the question, is there another good way to get the full temp path in VBScript?
I could use
Dim strUser : strUser = CreateObject("WScript.Network").UserName
"C:\Users\" & strUser & "\AppData\Local\Temp"
It's just been so long since I played with VBS, I'm not remembering if there is a better answer, or this is the consistently workable way to go. And, more than anything I want to know if .GetSpecialFolder(2) is broken in some way in Windows, or just by Autodesk.
Isn't it logical to have different values on different environments / scripting hosts?
GetSpecialFolder(2) simply returns the process environment variable named TMP. Any change on that variable -which is quite legal-, affects the value that GetSpecialFolder(2) returns.
GetSpecialFolder method
Constant: TemporaryFolder
Value: 2
The Temp folder is used to store temporary files. Its path is found in the TMP environment variable.
Since GetSpecialFolder(2) will always return an existing directory path I probably would use it with the thought that this is intended by the environment; the Autodesk Revit.
Other than that, I'd use something like below if I want the usual temporary path because even they are rare, there are installations where system drive is not C:. Relying on %localappdata% makes more sense in that manner.
Set WshShell = CreateObject("Wscript.Shell")
TempPath = WshShell.ExpandEnvironmentStrings("%localappdata%\Temp")
I am looking for a solution for this after struggling for most of the day. In short my dilemma seems simple. An effective solution appears elusive based on the Google and Stackoverflow hits I've had on this one.
(EDIT) I need to clarify the use-case. We may have 6 users needing to run certain scripts that use the "%USERNAME%" string to login to case-sensitive environments, Linux, MySQL, etc. One user-name is mixed-case. In the example I used my name: "Will" for the scripts it must-be: "will" (all lower-case).
The general question though is how can the %USERNAME% environment variable value be change when it is mis-entered?
At present my Windows 7 Professional username is "will" but the environment variable has this value:
echo. User = %USERNAME%
User = Will
I need it to come-out as:
User = will
rem * All lower-case
To be clear ...
I want to change (source) value Windows sets the USERNAME environment variable when I login.
I thought it would be easy to find the setting or config option to change that string -- Until I tried. I think this must happen frequently judging from questions related to getting a USERNAME to work with cygwin, etc.
The question
How to convert the value of %USERNAME% to lowercase within a Windows batch script?
Bothers me too, because that's a messy script to "fix" something basic in a login-environment.
I did a quick survey around the office as well. Some people have mixed case, one or two have lowercase. It seems arbitrary depending on what was typed-in when account was created or something.
Our admin guy looked up my account on the domain (no, not active directory) and is same as my login-screen, "will" (lowercase).
Finally I did a couple of experiments. I changed my username in the Users control panel
from: "will" to "xxwill"
Thinking that would surely update my USERNAME string. I logged out. I did it again and rebooted a second time. The result was extremely surprising (or maybe it shouldn't have been):
User = Will
rem * NO change for the "xxwill" name-change!
Some further grist. I created an account "dodo" and account "Dogdo" and changed the usernames to "Dodo" and "dogdo" respectively.
User = dodo
User = Dogdo
rem * NO change for name-changes!
I see that the USERNAME can't be altered that way. I can't edit the system environment variable in the Advanced options. It may be in the profile. But can you edit a profile or even look inside it with just Administrator powers? I don't know yet; I'm not sure that's my preferred route either since it is hacking with a bit of "output" from some original setting, somewhere.
I hope someone can set me straight so I don't need that script-solution.
If you want to change the windows USERNAME environment variable string, you can do this in the Advanced User Accounts Control Panel.
Open it by running netplwiz ("C:\Windows\System32\Netplwiz.exe").
Then select the name you want to change and click on the Properties button.
Now you get the properties of the selected user account: you can change the User name (, Full name & Description) here.
You'll (just) need administrator rights to do this.
Log out and log on, open a command prompt and try to output the value for %USERNAME%. You'll notice it has been changed according to your modification.
#ECHO OFF
SETLOCAL
PUSHD "%temp%"
COPY NUL "%username%" >NUL 2>NUL
FOR /f "delims=" %%a IN ('dir /L /b "%username%"') DO set "usernamelc=%%a"
del "%username%" >NUL 2>NUL
POPD
ECHO %USERNAME% --^> %usernamelc%
GOTO :EOF
Perhaps this may help. YMMV.
I don't think there is a way to do exactly what you are saying, but it might be achievable, you will know by playing around with the locations where usernames are defined.
• Firstly, there is the user folder C:\user\username1 and that is defined at user creation. This can (I think) be changed (I've only seen references to it, but there are many references, this is easy to find).
• Secondly, there is the Control Panel > User Accounts > Change My Name. This seems to tie in with your $env:USERNAME
• Finally, there is the lusrmgr.msc > Users. This is tied most closely to your "real" username, and aligns with [Security.Principal.WindowsIdentity]::GetCurrent()
I say "real" because I found this recently when trying to use ssh-keygen to create a public key and although my Display Name and $env:USERNAME were set to "Name1", ssh-keygen would only see "Name2" and I had to alter the name in lusrmgr.msc to fix this (a reboot was required).
This is an old post, but posting in case this can help anyone with similar issues.
If we are using this in custom action of an MSI, then WScript.ScriptName or WScript itself won't work.
The below will give the execution location (i.e., msiexec.exe) but not the VBScript or MSI file location.
Set WshShell = CreateObject("WScript.Shell")
strCurDir = WshShell.CurrentDirectory
We can use below one to get the script location but it will work only in Immediate Execution but not in Deferred.
cdsd = Session.Property("SourceDir")
Please let me know if any other alternative to use in MSI Custom Actions, specially for deferred.
Hope this helps! maybe? I can't tell if vbscript was or was not your intended usage.
SrcDirLen=Len(wscript.ScriptFullName)-Len(wscript.ScriptName)-1
SourceDir=Left(wscript.ScriptFullName, SrcDirLen)
I think this answer might be what you're looking for however.
SourceDir = Session.Property("SETUPEXEDIR") 'credit to link below
How to find the setup location in an VBScript custom action for InstallShield?
after edits... I got nothing else man... Sorry, the MSI is a little out of my element. =) I tried.
I'm trying to set a variable using AppleScript from automator:
do shell script "launchctl setenv LASTMAX \"" & today & "\""
I have a variable today that i want to assign to the variable LASTMAX
but when i check the property with echo $LASTMAX i keep getting the initial value,
even after i reboot.
How can i set variables with AppleScript?
Is there a different way to store persistant variables with automator?
Thanks!
You can use script objects to store your data in an out of the way place.
Persistant variable storage in Automator
storing the script seems to only work the first time.
When I paste the script refernced above into an Automator workflow and run the workflow several times, the returned value increments by one each time the script is run. Please post your code.
I'm having a hard time trying to get multiple environment variables configured at the same time. Here are some issues that I would like answered:
If I do have spaces in my PATH environment variable (i.e. C:\Program Files\Java\jdk1.6.0_25), is there any way to keep the same path, but make sure that nothing unexpected happens? I keep hearing that you shouldn't use spaces like in this link here: http://ist.berkeley.edu/as-ag/technology/howto/install-java-sdk-win.html.
Is there a difference in the ordering of the environment variables as listed in the fields?
What will happen if environment variables in the "User variables for user" fields and the "System variable" fields are different? Is it safe to assume that the result will be all the fields combined for that particular variable?
Thanks for any advice!