Can I pick up environment variables in vbscript WSH script? - vbscript

Is is possible to read system environment variables in a Windows Scripting Host (WSH) VBS script?
(I am writing a VBScript using Windows Scripting Host for task for a Cruise Control and want to pick up the project build URL.)

Here's an example (taken from here):
Set oShell = CreateObject( "WScript.Shell" )
user=oShell.ExpandEnvironmentStrings("%UserName%")
comp=oShell.ExpandEnvironmentStrings("%ComputerName%")
WScript.Echo user & " " & comp

The existing answers are all helpful, but let me attempt a pragmatic summary:
Typically, you want the current process's definition of an environment variable:
CreateObject("WScript.Shell").ExpandEnvironmentStrings("%TEMP%")
This is the equivalent of (note the absence of % around the variable name):
CreateObject("WScript.Shell").Environment("Process").Item("TEMP")
Caveat: Do not omit the ("Process) part: if you do, you'll get the system scope's definition of the variable; see below.
.ExpandEnvironmentStrings is conceptually simpler and more flexible: It can expand arbitrary strings with embedded (%-enclosed) environment-variable references; e.g.:
CreateObject("WScript.Shell").ExpandEnvironmentStrings("My name is %USERNAME%")
On rare occasions you may have to access environment-variable definitions from a specific scope (other than the current process's).
sScope = "System" ' May be: "Process", "User", "Volatile", "System"
CreateObject("WScript.Shell").Environment(sScope).Item("TEMP")
Note: As stated above, omitting the scope argument defaults to the System scope.
Caveat: Accessing a value this way does not expand it: Environment-variable values can be nested: they can refer to other environment variables.
In the example above, the return value is %SystemRoot%\TEMP, which contains the unexpanded reference to %SystemRoot%.
To expand the result, pass it to .ExpandEnvironmentStrings(), as demonstrated above.

From here ...
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshProccessEnv = WshShell.Environment("Process")
Set WshSysEnv = WshShell.Environment("System")
Wscript.Echo WshSysEnv("NUMBER_OF_PROCESSORS")
Wscript.Echo WshProccessEnv("Path")
Also, much more detail on TechNet.

Set WshShell = CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment
WScript.Echo "WINDIR=" & WshEnv.Item("WINDIR") & vbCrLf & vbCrLf
Set WshShell = CreateObject("WScript.Shell")
WScript.Echo "Environment System:" & vbCrLf & _
"..............................................."
For Each IEnv In WshShell.Environment("System")
WScript.Echo IEnv
Next
WScript.Echo vbCrLf & "Environment User:" & vbCrLf & _
"..............................................."
For Each IEnv In WshShell.Environment("User")
WScript.Echo IEnv
Next
WScript.Echo vbCrLf & "Environment Volatile:" & vbCrLf & _
"..............................................."
For Each IEnv In WshShell.Environment("Volatile")
WScript.Echo IEnv
Next
WScript.Echo vbCrLf & "Environment Process:" & vbCrLf & _
"..............................................."
For Each IEnv In WshShell.Environment("Process")
WScript.Echo IEnv
Next

This works for me:
Dim objNetwork
Set objNetwork = CreateObject("WScript.Network")
MsgBox objNetwork.UserName
or from the shell:
Set wshShell = WScript.CreateObject( "WScript.Shell" )
strUserName = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )
or from environment variable (it should work, but when i tested it was wrong!):
Set WshShell = CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment
MsgBox "USERNAME=" & WshEnv.Item("USERNAME")

Related

TargetPath is blank - on remote drives

I can't seem to get the WShell to return a value for objShortcut.TargetPath, although it passes the full name fine.
I've been reading that the WShell can have issues with remote disks, and I had been using an external drive.
After testing it on shortcuts on my C: drive with files located on my C: drive, I am finding it still does not work. Instead of echoing the traget path, it echos a blank value.
Edited. Thanks for the tip.
getshorty.vbs
Dim objWSHShell
set objWSHShell = CreateObject("WScript.Shell")
Set wshShell = WScript.CreateObject("WScript.Shell")
strTargetPath=objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(0))
Set objShortcut = wshShell.CreateShortcut(strTargetPath)
WScript.Echo objShortcut.TargetPath
Set objShortcut = Nothing
Set wshShell = Nothing
This a function to create a shortcut :
Call Shortcut("C:\The Absolute Path of your application goes here","Name of your Shortcut")
'*********************************************************************************
Sub Shortcut(PathApplication,Name)
Dim objShell,DesktopPath,objShortCut,MyTab
Set objShell = CreateObject("WScript.Shell")
MyTab = Split(PathApplication,"\")
If Name = "" Then
Name = MyTab(UBound(MyTab))
End if
DesktopPath = objShell.SpecialFolders("Desktop")
Set objShortCut = objShell.CreateShortcut(DesktopPath & "\" & Name & ".lnk")
objShortCut.TargetPath = Dblquote(PathApplication)
ObjShortCut.IconLocation = "%SystemRoot%\system32\SHELL32.dll,-25"
objShortCut.Save
End Sub
'*********************************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'*********************************************************************************
Of course, TargetPath property results to "" (a string of zero length) if the .lnk or .url file does not exist.
The CreateShortcut method returns either a WshShortcut object or a WshURLShortcut object. Simply calling the CreateShortcut method opens an existing shortcut but does not result in the creation of a shortcut.
option explicit
On Error GoTo 0
Dim wshShell, strTargetPath, objShortcut
Set wshShell = WScript.CreateObject("WScript.Shell")
strTargetPath = wshShell.ExpandEnvironmentStrings(WScript.Arguments.Item(0))
Set objShortcut = wshShell.CreateShortcut(strTargetPath)
WScript.Echo TypeName(objShortcut) & vbTab & VarType(objShortcut) _
& vbNewLine & "FullName" & vbTab & objShortcut.FullName _
& vbNewLine & "TargetPath" & vbTab & objShortcut.TargetPath
If TypeName(objShortcut) = "IWshShortcut" Then
WScript.Echo "Arguments" & vbTab & objShortcut.Arguments _
& vbNewLine & "Description" & vbTab & objShortcut.Description _
& vbNewLine & "WorkingDir" & vbTab & objShortcut.WorkingDirectory
End If
Output
==>dir /B d:\xxxx\*Shortcut.*
32421790 Shortcut.url
pisma - Shortcut.lnk
==>cscript D:\VB_scripts\SO\32421790.vbs "d:\xxxx\32421790 Shortcut.url"
IWshURLShortcut 8
FullName d:\xxxx\32421790 Shortcut.url
TargetPath http://stackoverflow.com/q/32421790/3439404
==>cscript D:\VB_scripts\SO\32421790.vbs "d:\xxxx\nonexistent Shortcut.url"
IWshURLShortcut 8
FullName d:\xxxx\nonexistent Shortcut.url
TargetPath
==>cscript D:\VB_scripts\SO\32421790.vbs "d:\xxxx\pisma - Shortcut.lnk"
IWshShortcut 8
FullName d:\xxxx\pisma - Shortcut.lnk
TargetPath D:\bat\SU\Files\ruzna pisma.png
Arguments
Description font samples
WorkingDir D:\bat\SU\Files
==>cscript D:\VB_scripts\SO\32421790.vbs "d:\xxxx\nonexistent Shortcut.lnk"
IWshShortcut 8
FullName d:\xxxx\nonexistent Shortcut.lnk
TargetPath
Arguments
Description
WorkingDir
==>

Get a VBS file to scan computer for a file

This is my first post, but I have been programming for a long time now
I just want to ask a quick question and the title explains it all. I want my VBS to run a file, but I dont want it to search just for a specific directory, I want it to just find the file if you know what I mean, because if I gave the script to anyone else, this file could be ANYWHERE on their computer.
This is the current couple of important lines that I am using for running files:
set wshshell = wscript.CreateObject("wscript.shell")
and
wshshell.run <program directory here>
You need a recursive function like this one searching for shortcuts.
Sub GenerateHotkeyInFolder(Fldr)
on error resume next
set WshShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set FldrItems=Fldr.Items
For Each oFile in FldrItems
With oFile
If .IsFileSystem = true And .IsLink = true And .Type <> "Shortcut to MS-DOS Program" then
set lnk = WshShell.CreateShortcut(oFile.Path)
If lnk.hotkey <> "" then
Set fsop = fso.GetFile(.Path)
LnkName = "<b>" & fso.GetBaseName(fso.GetFile(.Path)) & "</b><br>" & fsop.ParentFolder.path & "\" & fso.GetBaseName(fso.GetFile(.Path)) & "." & fso.GetExtensionName(fso.GetFile(.Path))
TableVar = TableVar & "<tr><td><b>" & lnk.hotkey & "</b></td><td><a class=TblURL onmouseover='MakeRed()' onmouseout='MakeBlack()' onclick='FindShortcut(" & Chr(34) & lnk.fullname & Chr(34) & ")'>" & lnkname & "</a>" & "</td><td><a class=TblURL onmouseover='MakeRed()' onmouseout='MakeBlack()' onclick='FindShortcut(" & Chr(34) & lnk.targetpath & Chr(34) & ")'>" & lnk.targetpath & "</a></td></tr>" & vbcrlf
End If
ElseIf .IsFileSystem = true And .IsFolder = true then
GenerateHotkeyInFolder(.GetFolder)
End If
End With
Next
End Sub

Starting a process in VBS: path not found

I need to make a simple vbs script to run some process' automatically. I found the following script on microsoft's website. It works fine to run notepad.exe the way the original example shows, but I'm trying to modify it to run myprog.exe. The full path to this program is: C:\myprogdir\myprog.exe
Const SW_NORMAL = 1
strComputer = "."
strCommand = "myprog.exe"
strPath = "C:\myprogdir\"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
' Configure the Notepad process to show a window
Set objStartup = objWMIService.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = SW_NORMAL
' Create Notepad process
Set objProcess = objWMIService.Get("Win32_Process")
intReturn = objProcess.Create _
(strCommand, strPath, objConfig, intProcessID)
If intReturn <> 0 Then
Wscript.Echo "Process could not be created." & _
vbNewLine & "Command line: " & strCommand & _
vbNewLine & "Return value: " & intReturn
Else
Wscript.Echo "Process created." & _
vbNewLine & "Command line: " & strCommand & _
vbNewLine & "Process ID: " & intProcessID
End If
I keep getting Return value: 9, which indicates "Path Not Found". However the path is correct. Is there something I'm not getting?
You don't need all that to start a process, you just need the Shell object. Also, be sure to wrap the path of your executable in quotes (in case the path has spaces). Like this:
Option Explicit
Dim shl
Set shl = CreateObject("Wscript.Shell")
Call shl.Run("""C:\myprogdir\myprog.exe""")
Set shl = Nothing
WScript.Quit
Unless the path to your program is included in the system's %PATH% environment variable you need to specify the commandline with the full path to the executable. Specifying the path just as the working directory will not work.
strProgram = "myprog.exe"
strPath = "C:\myprogdir"
Set fso = CreateObject("Scripting.FileSystemObject")
strCommand = fso.BuildPath(strPath, strProgram)
...
intReturn = objProcess.Create(strCommand, strPath, objConfig, intProcessID)
Using the BuildPath method will save you the headaches caused by having to keep track of leading/trailing backslashes.
Note that you need to put double quotes around a path that contains spaces, e.g. like this:
strCommand = Chr(34) & fso.BuildPath(strPath, strProgram) & Chr(34)
As others have already pointed out, there are simpler ways to start a process on the local computer, like Run:
Set sh = CreateObject("WScript.Shell")
sh.Run strCommand, 1, True
or ShellExecute:
Set app = CreateObject("Shell.Application")
app.ShellExecute strCommand, , strPath, , 1
There are some notable differences between Run and ShellExecute, though. The former can be run either synchronously or asynchronously (which means the command either does or doesn't wait for the external program to terminate). The latter OTOH always runs asynchronously (i.e. the method returns immediately without waiting for the external program to terminate), but has the advantage that it can be used to launch programs with elevated privileges when UAC is enabled by specifying the verb "runas" as the 4th argument.
However, these methods only allow for launching processes on the local computer. If you want to be able to launch processes on remote computers you will have to use WMI:
strComputer = "otherhost"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
See here for more information about WMI connections to remote hosts.

How can I pull the correct data from the HK Current User registry key instead of temp profile information

I am working on a script to pull the value in the key
HKCurrentUser\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Desktop
Currently all it is returning is:
C:\WINDOWS\system32\config\systemprofile\Desktop
When I want/think it should return:
%USERPROFILE%\Desktop
Below is the script that is pulling the infomration from the key and as far as I can tell it should be pulling the correct information. Just wondering if someone can enlighten me as to what I am missing. It also returns the computer name and the logged in username which both return correctly. This is going to be run on quite a few machines remotely.
'These are the constants for the following KEYS'
Const HKClassesRoot = &H80000000 'HKEY_CLASSES_ROOT
Const HKCurrentUser = &H80000001 'HKEY_CURRENT_USER
Const HKLocalMachine = &H80000002 'HKEY_LOCAL_MACHINE
Const HKUsers = &H80000003 'HKEY_USERS
Const HKCurrentConfig = &H80000005 'HKEY_CURRENT_CONFIG
'Setup objects to interact with here'
Set wshShell = WScript.CreateObject("Wscript.Shell")
strComputer = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
Set objNetwork = CreateObject("Wscript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Define variable to store the current user and then pull the current user
Dim currentUser
strCurrentUser = objNetwork.UserName
'find the data in the string we want to get the value from'
strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\"
strValueName = "Desktop"
'pull the info and store it in strValue'
objRegistry.GetStringValue HKCurrentUser,strKeyPath,strValueName,strValue
'setup for output of data to the file'
Dim strSpacer
Dim strData
strSpace = "+-------------------------------------------------------------------------------------------------------------------------+"
strData = "| " & strComputer & " == " & strCurrentUser & " == " & strValue & " |"
Dim strFileName
strFileName = "\\server\share\" & strCurrentUser & ".txt"
Set objFile = objFSO.OpenTextFile(strFileName,8,true)
objFile.write vbCrLf & strSpace & vbCrLf
objFile.write strData & vbCrLf
objFile.write strSpace & vbCrLf
'Close file'
objFile.Close
After review I found the answer to my own question. I was reading the registry incorrectly for what I was doing.
strRegkey = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Desktop"
strDataValue = wshShell.RegRead(strRegKey)
this returns the value stored currently in the key.
I suspect that the environment variable %USERPROFILE% from the registry value gets expanded to the profile of the user running the WMI service (LOCAL SYSTEM). GetStringValue seems to behave the same as GetExpandedStringValue when reading REG_EXPAND_SZ values.

How do I get the computer name of a system and output it to a file in VBScript

I am trying to get the computer name from the registry and write it to a file. At this point, my function call for obtaining the computer name from registry isn't working. Any advice would be appreciated.
Option Explicit
On Error Resume Next
Dim regComputerName, ComputerName
Set objShell = WScript.CreateObject("WScript.Shell")
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
regComputerName = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\Computername"
ComputerName = obj.shell.RegRead(regComputerName)
oWrite.WriteLine(ComputerName,C:\text)
Reading registry values is error prone and may require elevated privileges in Windows 7. There's another way of getting the computer name, very similar to what you are doing right now:
Set objNetwork = WScript.CreateObject("WScript.Network")
ComputerName = objNetwork.ComputerName
MsgBox ComputerName
Also, the last line in your script: oWrite.WriteLine(ComputerName,C:\text) will not work for 2 reasons:
C:\text has to be in quotes, like this: "C:\text.txt"
In VB, only a function that results a value can be called with parenthesis. Call WriteLine like this instead: oWrite.WriteLine ComputerName, "C:\text.txt"
Finally, are you sure you are not referring to VBScript instead of VB in your question?
Your code is not working because of an error in this line:
ComputerName = obj.shell.RegRead(regComputerName)
Instead of obj.shell you should be referencing objShell. It should look like this:
Set objShell = WScript.CreateObject("WScript.Shell")
strRegKey = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\Computername"
strComputerName = objShell.RegRead(strRegKey)
WScript.Echo strComputerName
However, there are much more reliable ways of getting the computer name without having to deal with the registry.
From WSH (as suggested above)
Set WshNetwork = WScript.CreateObject("WScript.Network")
strComputerName = WshNetwork.ComputerName
WScript.Echo "Computer Name: " & strComputerName
From an environmental variable...
Set wshShell = WScript.CreateObject("WScript.Shell")
strComputerName = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
WScript.Echo "Computer Name: " & strComputerName
From WMI...
strcomputer = "."
Set objWMISvc = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMISvc.ExecQuery("Select * from Win32_ComputerSystem",, 48)
For Each objItem in colItems
strComputerName = objItem.Name
WScript.Echo "Computer Name: " & strComputerName
Next
From ADSI...
Set objSysInfo = CreateObject("WinNTSystemInfo")
strComputerName = objSysInfo.ComputerName
WScript.Echo "Computer Name: " & strComputerName
From ADSI (only works for domain members)...
Set objSysInfo = CreateObject("ADSystemInfo")
strComputerName = objSysInfo.ComputerName
WScript.Echo "Computer Name: " & strComputerName
...and one last way for Windows XP users only...
Set objPC = CreateObject("Shell.LocalMachine")
strComputerName = objPC.MachineName
WScript.Echo "Computer Name: " & strComputerName

Resources