[精讚] [會員登入]
402

使用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帳號整合(3) LDAP的基本概念

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

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

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

臺中市雲端校務系統與Windows AD帳號整合(2) 安裝Active Directory網域服務

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

2018 hoc 掃地機器人 掃地機器人只能打掃沒有障礙物(桌椅、牆壁)的範圍,請寫程式控制機器人打掃餐廳的所有走道, 並在清掃完畢後回到充電器。

臺中市雲端校務系統與Windows AD帳號整合(4) 雲端校務系統與Windows AD的架構

臺中市雲端校務系統與Windows AD帳號整合(7) 設定校端更改密碼程式及執行

spring boot jpa 使用多個欄位排序 我有一entity 叫 team 資料欄下如下 @Id @GeneratedValue(strategy = Gener

使用vbs設定windows 10 靜態IP setting static ip Set Adapter = GetObject("winmgmts:Win