[精讚] [會員登入]
384

使用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

你可能感興趣的文章

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

在docker裡跑spring boot+mongo(二) 使用docker-compose 管理多個container 前篇使用docker run 一次建一個container

批次檢查tls憑證是否過期 tls憑證一年一簽, 台中市有三百多所學校, 寫成一檔案 skaps.tc.edu.tw www.skaps.tc.ed

在docker裡跑spring boot+mongo(一) 系統安裝docker ce centos7 + docker ce https://docs.docker.com/in

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

臺中市雲端校務系統與Windows AD帳號整合(8) 三 開機啟動服務 3-1 Windows Server上隨系統開機啟動服務 寫一個bat檔 cd C:UsersAdmi

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

[vue.js] 設定 content type 今天在wickt 端怎麼就是收不到vue.js 以post 傳過來的資料 找了好久才發現 application/jso

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

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

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

Google sheet 建立成績單的總分及排名並提供名字下拉查詢 Google sheet, sum, rank, pull down list