vbscript - 在 Windows 10 上运行的系统上设置本地计算机名称和静态 IP 地址

标签 vbscript windows-10

我正在尝试通过运行 VBScript 来重命名计算机并为其分配静态 IP。
名称和 IP 将从文本文件中读取。

我已在 Windows 7 上成功使用以下脚本,但在 Windows 10 上不起作用

要重命名 Windows 7 计算机:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colComputers = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")

Dim objComputer 'as Win32_ComputerSystem
For Each objComputer In colComputers
    err = objComputer.Rename("NewName")
Next

要设置 Windows 7 静态 IP:
Set colNetAdapters = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")

strIPAddress = Array("192.168.1.xxx")
strSubnetMask = Array("255.255.255.x")
strGateway = Array("192.168.1.xxx")
strGatewayMetric = Array(1)

我找不到可以在 Windows 10 上运行的脚本版本。

最佳答案

如果您想从外部文本文件中获取 Tcpip 配置(IP 和 DNS)和计算机新名称

首先以这种格式创建文本文件删除引号之间存在的内容并添加配置但不要更改其他任何内容然后保存此文件例如“IP.txt”:

newComputerName="ALHAMDULILLAH"
Name="wi-fi"
Address="192.168.1.7"
Mask="255.255.255.0"
Gateway="192.168.1.1" 
DNS1="2.2.2.2"
DNS2="8.8.8.8"

然后使用此脚本更改计算机的名称
' run script as administrator
 If WScript.Arguments.Length=0 Then
  CreateObject("Shell.Application").ShellExecute "Wscript.exe",Chr(34)&WScript.ScriptFullName&Chr(34)&" HOLLOPOST",Null,"runas",1
  WScript.Quit
 End If 

' Get the new computer name from Text File
myTextFilePath="C:\USERS\ENG\DESKTOP\IP.TXT"                   'add here the full path of text fill
T = CreateObject("Scripting.FileSystemObject").OpenTextFile(myTextFilePath).ReadAll
a=Split(T,vbcrlf)
For i=0 To UBound(a)
If InStr(1,a(i),"newComputerName",1)>0 Then
newComputerName=right(a(i),len(a(i))-16)
End If
Next 
' change Computer Name for registry
Dim sh : Set sh = CreateObject("Wscript.Shell")
Dim strRegPath : strRegPath="HKLM\system\CurrentControlSet\Services\Tcpip\Parameters\NV Hostname" 
sh.RegWrite strRegPath, newComputerName
WScript.Sleep 1000
strRegPath="HKLM\system\CurrentControlSet\control\ComputerName\ComputerName\ComputerName"
sh.RegWrite strRegPath,newComputerName
WScript.Sleep 1000
' restart the Computer
For Each ComputerObject In GetObject("Winmgmts:{(Shutdown)}").InstancesOf("Win32_OperatingSystem"): ComputerObject.Win32Shutdown(6) : Next

然后在重新启动计算机后使用第二个脚本更改 IP 配置和 DNS:
' run script as administrator
 If WScript.Arguments.Length=0 Then
  CreateObject("Shell.Application").ShellExecute "Wscript.exe",Chr(34)&WScript.ScriptFullName&Chr(34)&" HOLLOPOST",Null,"runas",1
  WScript.Quit
  End If

'Get Ip configuration from Text file
myTextFilePath="C:\USERS\ENG\DESKTOP\IP.TXT"
 T = CreateObject("Scripting.FileSystemObject").OpenTextFile(myTextFilePath).ReadAll
a=Split(T,vbcrlf)
For i=0 To UBound(a)
 If  InStr(1,a(i),"Name",1)>0 Then
      myInterfaceName=a(i) 
  ElseIf  InStr(1,a(i),"Address",1)>0 Then
      myIP=a(i)
  ElseIf  InStr(1,a(i),"Mask",1)>0 Then
      myMask=a(i)  
  ElseIf  InStr(1,a(i),"Gateway",1)>0 Then
     myGateway=a(i)
   ElseIf  InStr(1,a(i),"DNS1",1)>0 Then
     myPreferredDNS=right(a(i),len(a(i))-5)
   ElseIf  InStr(1,a(i),"DNS2",1)>0 Then
     myAlternatedDNS=right(a(i),len(a(i))-5)  
 End If 
 Next 
' change Computer IP and DNS 
Set sh=CreateObject("WScript.Shell")
sh.Run "cmd.exe /c netsh interface ipv4 set address """&myInterfaceName&""" static "&myIP&" "&myMask&" "&myGateway &" 1",0,False
sh.Run "cmd.exe /c netsh interface ipv4 Set dnsservers """&myInterfaceName&""" static "&myPreferredDNS&" primary",0,False 
sh.Run "cmd.exe /c netsh interface ipv4 Add dnsservers """&myInterfaceName&""" "&myAlternatedDNS&" Index=2",0,False 
sh.Run "cmd.exe  /k netsh interface ipv4 show config",1,False

关于vbscript - 在 Windows 10 上运行的系统上设置本地计算机名称和静态 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51012508/

相关文章:

sql-server - 如何从 VBScript 运行 Microsoft SQL Server 代理作业

javascript - localStorage 与 localSettings

windows - 如何使用 `nasm` 和 `alink` 的组合链接 kernel32.lib 和 user32.lib ?

python-3.x - 无法在 Windows 10 和 Python 3.7 上安装 dlib

asp.net - 如何在 ASP.NET 中使用经典 ASP Session 对象读取 session 变量

loops - 如何在VB脚本中逐行读取文件?

xslt - 使用 msxsl :script 从 XSLT 中使用 VBScript 扩展函数时出现错误 "cannot create ActiveX component"

python - 在运行 Windows 批处理文件、vbscript/wscript 或 Python 控制台脚本时将图像显示为初始屏幕

java - 无法使用 java 运行 First Kaa 应用程序

c# - ListView 项中的多个文本项