2013-05-20 25 views
0

想要使用VB腳本配置IP部分。我得到下面的腳本,這將獲得用戶輸入和相應地更改IP地址,但它只是改變IP地址,子網,網關以及我想添加首選和備用DNS服務器。在Windows XP上使用VBS更改IP地址

Option Explicit 'Enforce strict naming 
Dim WSH, FSO 
Dim objComputer, objWMIService, objOperatingSystem, objNetAdapter 
Dim strTitle, strVersion, strThisPC, strComputername, strCompNameRegPath, strWorkgroup 
Dim strCCS, strTcpipParamsRegPath, strIPAddress, strSubnetMask, strGateway,   strGatewayMetric 
Dim colMEM, colNetAdapters, colOperatingSystems 
Dim errEnable, errGateways 

'Setup scripting environment 
Set WSH = CreateObject ("WSCript.shell") 
Set FSO = CreateObject("Scripting.FileSystemObject") 

'Set Version info 
strVersion = "v04" 

'Set target PC for WMI 
strThisPC = "." 

'Set the Title for any dialogs 
strTitle = "XP Clone Changer " & strVersion 

'------------------------------------------------------------------------ 
'Section 1 - Use WMI to change IP address, subnet mask and gateway 
WSH.Popup "Preparing to change IP address, subnet mask and gateway... Please wait.",2,    strTitle 'Inform user 

'Setup WMI 
Set objWMIService = GetObject("winmgmts:\\" & strThisPC & "\root\cimv2") 

'Set specific WMI query so 1394 adapter is excluded 
Set colNetAdapters = objWMIService.ExecQuery _ 
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE") 

'Get user input into an array 
strIPAddress = Array(InputBox("Enter the new IP address")) 
strSubnetMask = Array(InputBox("Enter the new Subnet mask")) 
strGateway = Array(InputBox("Enter the new Gateway address")) 

'Inform user 
WSH.Popup "Please be patient whilst changes are made. This could take 10 seconds or  more.",3, strTitle 'Inform user 
strGatewayMetric = Array(1) 

'Make the changes 
For Each objNetAdapter in colNetAdapters 
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask) 
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric) 
If errEnable = 0 Then 
    WSH.Popup "The IP address has been changed.",2,strTitle 'Inform user of success 
Else 
    WSH.Popup "The IP address could not be changed automatically." & vbCrLf & vbCrLf &_ 
    "This may be because the PC/Laptop is not connected to an active network." &  vbCrLf & vbCrLf &_ 
    "Please check the IP settings manually afterwards.",2,strTitle 'Inform user of   failure 
End If 
Next 

Set WSH = Nothing 
Set FSO = Nothing 
Set objWMIService = Nothing 
Set colNetAdapters = Nothing 
Set colMEM = Nothing 
WScript.Quit 

回答

1

的DNS服務器可以使用SetDNSServerSearchOrder方法進行設置:

arrDNSServers = Array("192.168.23.13", "192.168.23.14") 
errDNS = objNetAdapter.SetDNSServerSearchOrder(arrDNSServers) 

如果希望用戶指定的名稱服務器,你可以做這樣的事情:

arrDNSServers = Split(InputBox("Enter the name servers (separated by spaces)")) 

進入名稱服務器以空格分隔(例如192.168.23.13 192.168.23.14)。該字符串將自動轉換爲具有地址的數組。

+0

@Ansgat Wiechers - 但不想在scrypt中硬編碼DNS值,它應該由用戶在彈出框中輸入 – user2365321

+1

以上只是一個示例。是什麼讓你無法像提示其他信息一樣提示用戶輸入名稱服務器? –

+0

我嘗試嵌入的代碼,但它不工作的地方,我做錯了 – user2365321