加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读