2016-03-23 65 views
2

我有一個要求,以獲得我使用下面的操作的方式從VBS PC的IP地址:獲取IP,然後改變最後一個字節

strQuery = "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE MACAddress > ''" 

Set objWMIService = GetObject("winmgmts://./root/CIMV2") 
Set colItems  = objWMIService.ExecQuery(strQuery, "WQL", 48) 

For Each objItem In colItems 
    If IsArray(objItem.IPAddress) Then 
     If UBound(objItem.IPAddress) = 0 Then 
      strIP = "IP Address: " & objItem.IPAddress(0) 
     Else 
      strIP = "IP Addresses: " & Join(objItem.IPAddress, ",") 
     End If 
    End If 
Next 

我然後使用下面的映射網絡驅動器:

Option Explicit 
Dim objNetwork 
Dim strDriveLetter, strRemotePath, strUser, strPassword, strProfile 

strDriveLetter = "Z:" 
strRemotePath = "\\10.121.34.140\c$" 
strUser = "user" 
strPassword = "Password" 
strProfile = "false" 

Set objNetwork = WScript.CreateObject("WScript.Network") 
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, _ 
strProfile, strUser, strPassword 

我想不過做的是獲得hostmachine(所以10.121.34.130)的知識產權和公正的八位位組更改爲.140,並映射到這一點。

我該怎麼做?

回答

2

InStrRev()Left()函數應該是你所需要的。

' Sample starting address... 
strIP = "10.121.34.130" 

' Get the index of the last period... 
i = InStrRev(strIP, ".") 

' Append the new last octet starting there... 
strIP = Left(strIP, i) & "140" 

或者,作爲一個班輪:

strIP = Left(strIP, InStrRev(strIP, ".")) & "140" 

,或作爲一個班輪創建完整的遠程路徑:

strIP = "\\" & Left(strIP, InStrRev(strIP, ".")) & "140\c$" 
+0

完美 - 我調整,使帶材MyIPAddress和它的作品 - 很多謝謝! – Qazxswe

相關問題