How to get the computer name 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 the moment, my call function to get the computer name from the registry does not work. 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) 
+4
source share
2 answers

Reading registry values ​​is error prone and may require elevated privileges in Windows 7. Another way to get a computer name is very similar to what you are doing right now:

 Set objNetwork = WScript.CreateObject("WScript.Network") ComputerName = objNetwork.ComputerName MsgBox ComputerName 

In addition, the last line in the script: oWrite.WriteLine(ComputerName,C:\text) will not work for two reasons:

  • C:\text must be in quotation marks, for example: "C:\text.txt"
  • In VB, only the calling function can be called using parentheses. Call WriteLine as follows: oWrite.WriteLine ComputerName, "C:\text.txt"

Finally, are you sure you don't mean VBScript instead of VB in your question?

+5
source

Your code does not work due to an error in this line:

 ComputerName = obj.shell.RegRead(regComputerName) 

Instead of obj.shell, you should reference 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 to get a 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 the environment 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 the last way for users of Windows XP ...

 Set objPC = CreateObject("Shell.LocalMachine") strComputerName = objPC.MachineName WScript.Echo "Computer Name: " & strComputerName 
+2
source

Source: https://habr.com/ru/post/1389171/


All Articles