如何获取系统的计算机名称并将其输出到VBScript中的文件

Sob*_*one 4 registry vbscript wsh windows-7

我试图从注册表中获取计算机名称并将其写入文件.此时,从注册表获取计算机名称的函数调用无效.任何意见,将不胜感激.

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)
Run Code Online (Sandbox Code Playgroud)

Abb*_*bas 5

读取注册表值很容易出错,并且可能需要在Windows 7中提升权限.还有另一种获取计算机名称的方法,与您现在正在执行的操作非常相似:

Set objNetwork = WScript.CreateObject("WScript.Network")
ComputerName = objNetwork.ComputerName
MsgBox ComputerName
Run Code Online (Sandbox Code Playgroud)

此外,脚本中的最后一行:oWrite.WriteLine(ComputerName,C:\text)由于两个原因不起作用:

  1. C:\text 必须在引号中,如下所示: "C:\text.txt"
  2. 在VB中,只能使用括号调用产生值的函数.这样打电话WriteLine:oWrite.WriteLine ComputerName, "C:\text.txt"

最后,你确定在你的问题中你没有提到VBScript而不是VB吗?