4

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)
Abbas
  • 6,720
  • 4
  • 35
  • 49
Soberone
  • 45
  • 1
  • 2
  • 5

2 Answers2

5

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:

  1. C:\text has to be in quotes, like this: "C:\text.txt"
  2. 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?

Abbas
  • 6,720
  • 4
  • 35
  • 49
2

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
Nilpo
  • 4,675
  • 1
  • 25
  • 39