vbscript - 在 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 的该脚本版本。
解决方案
更改计算机名称:
' 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
推荐阅读
- java - 在其他 Java 程序中打开带有参数的 Runnable .jar 文件
- javascript - 如何在数据属性值选择的元素上执行函数?
- amazon-web-services - @aws_subscribe 注解的目的是什么?
- reactjs - 从对象列表中获取值数组
- sql-server - 如何在 Django 中连接来自两个不同数据库的两个表?
- x86 - 在完全虚拟化环境中,来宾操作系统系统调用会发生什么?
- jenkins - Jenkins 作业未能执行日志轮换消息
- php - 如何在 docker 容器中启用 php-fpm 的访问日志?
- vbscript - WshShell.Run 在命令行参数中带有空格
- java - 当按下 Enter 时,Java 中的循环不会中断,当条件是 DataInputStream.isAvailable() == 0 并且应用程序使用 telnet 运行时