VBScript Environment Variable %COMPUTERNAME%
Solution 1
In case you run the script from the source pc you can use the following
dim oFso, oShell, oShellEnv, computerName, target, source
const overwrite = true
set oFso = CreateObject("Scripting.FileSystemObject")
set oShell = WScript.CreateObject("WScript.Shell")
set oShellEnv = oShell.Environment("Process")
computerName = oShellEnv("ComputerName")
source = "c:\test\yourZip.zip"
target = "\\JakePC\copiedFiles\" & computerName & ".zip"
'copiedFiles needs to be a share with write permission for the user who runs the script
oFso.CopyFile source, target, overwrite
'do check on errors and the cleanup of your objects
in case you run it from the targetpc you should use remote scripting but that would not be smart since you must know the name of the pc where you need to run it so there is no need for the environmentvariable.
Solution 2
I've found 2 snippets to get the hostname. Both runs ok in windows 7 sp1 and windows server 2012:
'
' How to get the hostname
'
' References
'
' Method1: http://www.robvanderwoude.com/vbstech_network_names_hostname.php
' method2: https://msdn.microsoft.com/en-us/library/s6wt333f(v=vs.84).aspx
WScript.Echo "Method 1 "
Set wshShell = WScript.CreateObject( "WScript.Shell" )
strRegValue = "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Hostname"
strHostName = wshShell.RegRead( strRegValue )
WScript.Echo "Host Name: " & strHostName
WScript.Echo "Method 2 (include other network values)"
Set WshNetwork = WScript.CreateObject("WScript.Network")
WScript.Echo "Domain = " & WshNetwork.UserDomain
WScript.Echo "Computer Name = " & WshNetwork.ComputerName
WScript.Echo "User Name = " & WshNetwork.UserName
The difference is WshNetwork.ComputerName
method retrieve the hostname in upper case.
parchambeau
Founding Partner at Angle Capital, Crypto/Angel investor, previously Engineering @CoinDesk Founder and CTO @Lawnmower (acquired)
Updated on December 08, 2020Comments
-
parchambeau over 3 years
I'm looking for some help with the environment variable %COMPUTERNAME%. It is working in my script as a means to name a file after the local host name. At another point I am using the script to build a folder in a different directory over the network (to a mapped drive) and I need to name the folder the local host name of the original computer. This may not make sense but I will provide an example below:
Comp1 = BobPC Comp2 = JakePC
I am making a zip file on BobPC, which is then being copied over to JakePC, but I need this file to be copied into a directory like... C:\CopiedFiles\BobPc because this script will be run on many computers and each of them needs a folder where the files will be located named after the computer it came from.
I hope this makes sense.
Right now I can make it build a folder just fine but it names it the name of "JakePC" obviously because I am using the environment variables to grab the local host name.
My question basically is how can I go about telling it to name the folder after the the original computer?
If you have any questions let me know, I will be more than happy to explain because I know that I might not be making much sense.