首页 > 解决方案 > 在 Windows 10 上运行的系统上设置本地计算机名称和静态 IP 地址

问题描述

我正在尝试重命名计算机并通过运行 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 的该脚本版本。

标签: vbscriptwindows-10

解决方案


更改计算机名称:

' run script as administrator

 If WScript.Arguments.Length=0 Then
  CreateObject("Shell.Application").ShellExecute "Wscript.exe",Chr(34)&WScript.ScriptFullName&Chr(34)&" RunAs",Null,"runas",1
  WScript.Quit
 End If 

' change Computer Name for registry
Dim newComputerName : newComputerName="android"
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 Computer In GetObject("Winmgmts:{(Shutdown)}").InstancesOf("Win32_OperatingSystem")
Computer.Win32Shutdown(6) 
Next
'sh.Run "cmd.exe /c shutdown /r /f /t 00",0,False

重新启动后,您可以使用此脚本更改 IP 地址和 DNS

' run script as administrator
 If WScript.Arguments.Length=0 Then
  CreateObject("Shell.Application").ShellExecute "Wscript.exe",Chr(34)&WScript.ScriptFullName&Chr(34)&" RunAs",Null,"runas",1
  WScript.Quit
 End If 

' change Computer IP and DNS 
myInterfaceName="wi-fi"
myIP="192.168.1.7"
myMask="255.255.255.0"
myGateway="192.168.1.100" 
myPreferredDNS ="2.2.2.2"
myAlternatedDNS ="8.8.8.8"
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

推荐阅读