使用vbs設定windows 10 靜態IP

URL Link //n.sfs.tw/15545

2021-12-21 21:43:22 By igogo

 

setting static ip

'https://social.technet.microsoft.com/Forums/en-US/44cc5939-a1f7-4bf7-a38a-7f8d2591bc2d/vbscript-that-changes-network-portion-of-ip-address?forum=ITCG
'show logged on user
Dim CurrentUser
Set objNetwork = CreateObject("Wscript.Network")
CurrentUser=objNetwork.UserName

'read mac_table 
Dim row,csvfile

row=0
csvfile="data.csv"
'Wscript.Echo csv_file
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile (csvfile, ForReading)

Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    row=row+1   
Loop

objTextFile.Close
 
ReDim mac_tables(row-1,1)
Dim j
j=0

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile (csvfile, ForReading)
 
Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    arrServiceList = Split(strNextLine , ",")
    'Wscript.Echo "Server name: " & arrServiceList(0)
    mac_tables(j,0) = arrServiceList(0)
    For i = 1 to Ubound(arrServiceList)
        'Wscript.Echo "Service: " & arrServiceList(i)
    mac_tables(j,i) = arrServiceList(i)
    Next
    j=j+1   
Loop



dim NIC1, Nic, nowIP
Set NIC1 = GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")


For Each Nic in NIC1
    if Nic.IPEnabled then
        nowIP = Nic.IPAddress(0)       
    End if
Next

dim macAddress
dim WMI:  set WMI = GetObject("winmgmts:\\.\root\cimv2")
dim Nads: set Nads = WMI.ExecQuery("Select * from Win32_NetworkAdapter where physicaladapter=true") 
dim nad
for each Nad in Nads
	
	If not isnull(Nad.MACAddress) And InStr(Nad.MACAddress, "98:EE:CB") > 0 Then
		
		macAddress = UCase(Nad.MACAddress)   
	End If
   
next 



For i = 0 To UBound(mac_tables) 
    
	If UCase(mac_tables(i,1)) = macAddress then
		expectedIP = Trim(mac_tables(i,0))
		
		If nowIP <> mac_tables(i,0) then

			Set objWMIService = GetObject("Winmgmts:root\cimv2")		
			
			'change ip
			Set Adapters = objWMIService.ExecQuery _
				("Select * from Win32_NetworkAdapterConfiguration " _
					& "where IPEnabled=TRUE")					
					
				
				strIPAddress = Array(expectedIP)
				strSubnetMask = Array("255.255.255.0")
				strGateway = Array("192.168.2.254")
				strGatewayMetric = Array(1)
			For Each Adapter in Adapters	
				errEnable = Adapter.EnableStatic( _
        strIPAddress, strSubnetMask)
				Adapter.SetGateways(_
        strGateway)
				
				
				Adapter.SetDNSServerSearchOrder(Array("163.17.40.3","8.8.8.8"))
				if errEnable = 0 then 
					WScript.Echo "DHCP disabled, using static IP address"
				else
					WScript.Echo "DHCP disable failed"
				end if
			Next
 
					
			computerName = "com410-" & Mid(expectedIP, 12, 2)			
			For Each objComputer in _ 
				objWMIService.InstancesOf("Win32_ComputerSystem")
			
												
				Return = objComputer.rename(computerName)
				If Return <> 0 Then
					WScript.Echo "Rename failed. Error = " & Err.Number
				Else
					             
					Const TIMEOUT = 5
					Set objShell = WScript.CreateObject("WScript.Shell") 
					objShell.Popup "Rename succeeded. Reboot for new name to go into effect", TIMEOUT
     
					
					strComputer = "."
					Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Shutdown)}!\\" & strComputer & "\root\cimv2")
					Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
              
					For Each objOperatingSystem in colOperatingSystems
						ObjOperatingSystem.Reboot()
					Next
 
				End If
				
				
			Next
			
			
		End If
		
		
		
		
	End If
  
Next

'Wscript.Echo macAddress
'Wscript.Echo nowIP
Wscript.quit