Задайте имя локального компьютера и статический IP-адрес в системах под управлением Windows 10 - PullRequest
0 голосов
/ 24 июня 2018

Я пытаюсь переименовать компьютеры и назначить им статический IP-адрес, запустив VBScript. Имя и 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.

Ответы [ 2 ]

0 голосов
/ 04 октября 2018

Если вы хотите взять конфигурацию 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
0 голосов
/ 03 октября 2018

Изменение имени компьютера:

' 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
...