vbscript – 在Windows 10上运行的系统上设置本地计算机名称和静
发布时间:2020-12-14 02:51:43 所属栏目:Windows 来源:网络整理
导读:我正在尝试重命名计算机并通过运行VBScript为它们分配静态IP. 将从文本文件中读取名称和IP. 我在Windows 7上成功使用了以下脚本,但它在Windows 10上不起作用 要重命名Windows 7计算机: strComputer = "."Set objWMIService = GetObject("winmgmts:" _ "{imp
我正在尝试重命名计算机并通过运行VBScript为它们分配静态IP.
将从文本文件中读取名称和IP. 我在Windows 7上成功使用了以下脚本,但它在Windows 10上不起作用 要重命名Windows 7计算机: strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!" & strComputer & "rootcimv2") 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:USERSENGDESKTOPIP.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="HKLMsystemCurrentControlSetServicesTcpipParametersNV Hostname" sh.RegWrite strRegPath,newComputerName WScript.Sleep 1000 strRegPath="HKLMsystemCurrentControlSetcontrolComputerNameComputerNameComputerName" 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",1 WScript.Quit End If 'Get Ip configuration from Text file myTextFilePath="C:USERSENGDESKTOPIP.TXT" T = CreateObject("Scripting.FileSystemObject").OpenTextFile(myTextFilePath).ReadAll a=Split(T,vbcrlf) For i=0 To UBound(a) If InStr(1,"Name",1)>0 Then myInterfaceName=a(i) ElseIf InStr(1,"Address",1)>0 Then myIP=a(i) ElseIf InStr(1,"Mask",1)>0 Then myMask=a(i) ElseIf InStr(1,"Gateway",1)>0 Then myGateway=a(i) ElseIf InStr(1,"DNS1",1)>0 Then myPreferredDNS=right(a(i),len(a(i))-5) ElseIf InStr(1,"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",False sh.Run "cmd.exe /c netsh interface ipv4 Set dnsservers """&myInterfaceName&""" static "&myPreferredDNS&" primary",False sh.Run "cmd.exe /c netsh interface ipv4 Add dnsservers """&myInterfaceName&""" "&myAlternatedDNS&" Index=2",False sh.Run "cmd.exe /k netsh interface ipv4 show config",1,False (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- hosted-exchange – 使用Microsoft Online Services托管Exc
- Cryptoapi签名/验证无法在Windows 8.1上运行
- FPGA QuartusII 13.0.1+ModelSim SE 10.1a联合仿真以及Hell
- 是否有Windows CMD相当于Unix shell的exec?
- windows-7 – Windows 7有免费的NFS客户端吗?
- windows – 当connectiong:什么也不做,没有错误时RDP伪造
- .net core 任务调度
- windows-7 – Windows 7 RC在BootCamp上与MacBook Pro有任何
- winapi – 如何使用嵌入作为资源的清单? (Windows XP / Vi
- 用于打开“配置高级用户配置文件属性”的命令(Windows 7)