[精讚] [會員登入]
294

使用vbs設定windows 10 靜態IP

setting static ip Set Adapter = GetObject("winmgmts:Win

分享此文連結 //n.sfs.tw/15545

分享連結 使用vbs設定windows 10 靜態IP@igogo
(文章歡迎轉載,務必尊重版權註明連結來源)
2021-12-28 09:24:08 最後編修
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


  

 

 

 

END

你可能感興趣的文章

臺中市雲端校務系統與Windows AD帳號整合(6) 修改Windows AD 密碼原則

對照mac address 批次更改電腦名稱 讀取mac address 對照表, 還原電腦後,自動更改密碼 'show logged on user Dim

臺中市雲端校務系統與Windows AD帳號整合(3) LDAP的基本概念

雲端校務系統與OPENLDAP帳號整合(2) openldap,雲端校務

apache 反向代理 80轉443轉8080 apache, proxy pass

網站無障礙規範 https://accessibility.ncc.gov.tw/News/Detail/3238?Category=4

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

centos 7 移机出現 dracut-initqueue timeout centos 7 移机出現 dracut-initqueue timeout 處理

vue.js modal 作兩個選項按鈕並導向不同頁面 vue.js modal 作兩個選項按鈕

tc web 問與答 Q. 填報網址 A. https://tiny.cc/tc-web Q. 出現了以下畫面, 怎麼辦 A. 建議瀏覽器開無

對照mac address 批次更改電腦名稱 讀取mac address 對照表, 還原電腦後,自動更改密碼 'show logged on user Dim

[web] 自訂模組-打字機與跑馬燈效果 自訂模組-打字機效果