[精讚] [會員登入]
288

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

你可能感興趣的文章

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

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

openldap資料移到 docker 拉docker 上的 image 回來 docker pull osixia/openldap 在正在運行的openld

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

如何在docker 的nginx 做另一個container的反向代理 環境 docker 中一container 跑 nginx 對外服務80 port, http://server 另一c

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

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

java-身份証字號驗証 FormatCheck.java public class FormatCheck { private volatile

利用maven建立一個可執行的jar檔 利用maven建立一個可執行的jar檔

python 的RE python re

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

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