2011-01-28 94 views
0

我運行下面的代碼:WMI EnableStatic的=類型不匹配

Dim strComputer As String = "." 
Dim objWMIService As Object = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 
Dim colNetAdapters As Object = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration " & "where IPEnabled=TRUE") 
Dim ipAdd As String = "83.185.88.205" 
Dim ipMask As String = "255.255.255.255" 

    For Each objNetAdapter In colNetAdapters 
       If objNetAdapter.Index = 458755 Then 
        objNetAdapter.EnableStatic(ipAdd, ipMask) 
       End If 

當我運行此我得到行:
objNetAdapter.EnableStatic(IPADD,ipMask)

「類型不匹配」 錯誤代碼

任何想法爲什麼?該接口是由我使用的移動寬帶附帶的軟件建立的PPP接口。

MSDN指定:

UINT32 EnableStatic的( [IN]串的IPAddress [], [IN]串子網掩碼[] );

[in]意味着什麼,我相信我必須以某種方式將變量變成數組?

回答

0

試着改變你的聲明來:

Dim ipAdd As String() = {"83.185.88.205"} 
Dim ipMask As String() = {"255.255.255.255"} 

編輯

尋找更多的,每一個解決方案,我發現作品已使用System.Management管理命名空間已經。看看這個代碼:

http://www.dreamincode.net/code/snippet2015.htm

+0

同樣的問題..不幸 – grandnasty 2011-01-28 14:51:55

0

我知道這是一個古老的線程,但發現了一些問題,返工後的執行已經承載 我設置一個變量的代碼,以保持IP地址的子網掩碼改劇本,網關等.. 希望這有助於

' Script name: Subnet Mask Adjuster 
' Date: April 2014 
' 
' VBscript that will change the subnet mask 
' Description 
' Changing the subnet mask on a list of computers and logging the success per machine 
' Script Requirements 
' Script needs to record the current IP, Gateway, Gateway metric prior to changing the subnet mask 
' When changing the subnet mask the script will once again reinstall the gathered information 
' to ensure that the system is still addressable 
' 
' Access reqquirements 
' Admin level access across all computers being targeted 
' 
' Script needs to be invoked as cscript.exe 
' example: Cscript.exe CSNMIPBLOC.vbs "xx.xx.xx.xx" 
' where the xx.xx.xx.xx is the ip address of the subnet 

' global variables declaration 
' --- Strings --- 
Dim strCurrentDirectory, strDateTime, strInputFile 
Dim strGateway, strGatewayMetric 
Dim strSubnetMask 
' --- File system objects --- 
Dim objFSO, objLog, objTextFile 
' --- Shell Objects --- 
Dim objWSH 
' --- File System Contants --- 
Const ForReading = 1 
Const ForWriting = 2 
Const ForAppending = 8 


' Create an instance of the file System object and shell object 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objWSH = CreateObject("Wscript.Shell") 

' Get the path of the script 
strCurrentDirectory = objWSH.CurrentDirectory 

' retrieve the time for the log file name 
strDatetime = funcGetDatetime 

' Get the path to the input file 
strInputFile = strCurrentDirectory & "\Servers.txt" 

' Create the log file 
Set objLog = objFSO.CreateTextFile(strCurrentDirectory & "\SubnetChanger_" & strDateTime & ".log", ForWriting) 

' Write the entry into the log file 
subRprtProg "Subnet Mask Adjuster Script" 
subRprtProg "Date/Time: " & strDateTime 
subRprtProg "-------------------------------" 

' Open the input file 
Set objTextFile = objFSO.OpenTextFile(strInputFile, ForReading) 
subRprtProg "Opening input file " & strInputFile 
subRprtProg "----------------------------------------------------" 

' Read the contents of the file one line at a time and perform the function 
Do Until objTextFile.AtEndOfStream 
    ' Read a line form the file and remove the spaces from the beginning and end 
    strComputer = Trim(objTextFile.ReadLine) 
    subRprtProg " " 
    subRprtProg "Connecting to computer " & strComputer 
    subRprtProg " " 
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 

    ' Enable Error Handling 
    'On Error Resume Next 
    ' Get the list on Network adapters from the Computer 
    subRprtProg "Retrieving the Network adapters" 
    Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE") 

    strSubnetMask = Array("255.255.255.192") 
    strSNMDisplay = strSubnetMask(i) 
    subRprtProg "SubnetMask is: " & strSNMDisplay 

    ' Go through the configured adaptors and nics 
    For Each objNetAdapter In colNetAdapters 
     If Not IsNull (objNetAdapter.IPAddress) Then 
      strIPAddress = objNetAdapter.IPAddress(i) 
      objIPaddress = objNetAdapter.IPAddress 
      subRprtProg "Current IP Address is: " & strIPAddress 
      strGateway = objNetAdapter.DefaultIPGateway(i) 
      objDefIPGW = objNetAdapter.DefaultIPGateway 
      subRprtProg "Current Default IP Gateway: " & strGateway 
      strGatewayMetric = objNetAdapter.GateWayCostMetric(i) 
      objGWMetric = objNetAdapter.GateWayCostMetric 
      subRprtProg "Current Gateway Metric: " & strGatewayMetric 

      ' Setting the subnet and resetting IP, Gateway and GatewayMetric 
      errEnable = objNetAdapter.EnableStatic(objIPaddress, strSubnetMask) 
      subRprtProg "Setting IP Address to: " & strIPaddress 
      subRprtProg "Setting Subnetmask to: " & strSNMDisplay 
      errGateways = objNetAdapter.SetGateways(objDefIPGW, objGWMetric) 
      subRprtProg "Setting IPGateway to: " & strGateway 
      subRprtProg "Setting GateWay Metric to: " & strGatewaymetric 
      Select Case errEnable 
       Case 0 
       subRprtProg "Successful completion, no reboot required." 
       Case 1 
       subRprtProg "Successful completion, reboot required." 
       Case 64 
       subRprtProg "Method not supported on this platform." 
       Case 65 
       subRprtProg "Unknown failure." 
       Case 66 
       subRprtProg "Invalid subnet mask." 
       Case 67 
       subRprtProg "An error occurred while processing an instance that was returned." 
       Case 68 
       subRprtProg "Invalid input parameter." 
       Case 69 
       subRprtProg "More than five gateways specified." 
       Case 70 
       subRprtProg "Invalid IP address." 
       Case 71 
       subRprtProg "Invalid gateway IP address." 
       Case 72 
       subRprtProg "An error occurred while accessing the registry for the requested information." 
       Case 73 
       subRprtProg "Invalid domain name." 
       Case 74 
       subRprtProg "Invalid host name." 
       Case 75 
       subRprtProg "No primary or secondary WINS server defined." 
       Case 76 
       subRprtProg "Invalid file." 
       Case 77 
       subRprtProg "Invalid system path." 
       Case 78 
       subRprtProg "File copy failed." 
       Case 79 
       subRprtProg "Invalid security parameter." 
       Case 80 
       subRprtProg "Unable to configure TCP/IP service." 
       Case 81 
       subRprtProg "Unable to configure DHCP service." 
       Case 82 
       subRprtProg "Unable to renew DHCP lease." 
       Case 83 
       subRprtProg "Unable to release DHCP lease." 
       Case 84 
       subRprtProg "IP not enabled on adapter." 
       Case 85**************************** script ******************************* 
       subRprtProg "IPX not enabled on adapter." 
       Case 86 
       subRprtProg "Frame or network number bounds error." 
       Case 87 
       subRprtProg "Invalid frame type." 
       Case 88 
       subRprtProg "Invalid network number." 
       Case 89 
       subRprtProg "Duplicate network number." 
       Case 90 
       subRprtProg "Parameter out of bounds." 
       Case 91 
       subRprtProg "Access denied." 
       Case 92 
       subRprtProg "Out of memory." 
       Case 93 
       subRprtProg "Already exists." 
       Case 94 
       subRprtProg "Path, file, or object not found." 
       Case 95 
       subRprtProg "Unable to notify service." 
       Case 96 
       subRprtProg "Unable to notify DNS service." 
       Case 97 
       subRprtProg "Interface not configurable." 
       Case 98 
       subRprtProg "Not all DHCP leases could be released or renewed." 
       Case 100 
       subRprtProg "DHCP not enabled on adapter." 
      End Select 

     Else 
      subRprtProg "No IP address detected on Adapter" 
     End If 
    Next 

    subRprtProg "Completed for computer " & strComputer 
    subRprtProg "Moving to next computer" 
    subRprtProg "----------------------------------------------------------------" 

    ' Close Error Handling 
    'On Error Goto 0 
Loop 

subRprtProg "Script completed at: " & Now() 
subRprtProg "Script will now exit, see log files for details" 

WScript.Quit 

' Function to return the date and time in file name friendly format 
' ----------------------------------------------------------------- 
Function funcGetDateTime 
    ' Variable Declaration 
    ' --- Integers --- 
    Dim intYear, intMonth, intDay, intHour, intMin, intSec 
    ' --- Strings --- 
    Dim strFileName 

    ' Get the date 
    intYear = Year(Now()) 
    intMonth = Month(Now()) 
    intDay = Day(Now()) 

    ' Get the time 
    intHour = Hour(Now()): If intHour < 10 Then intHour = "0" & intHour 
    intMin = Minute(Now()): If intMin < 10 Then intMin = "0" & intMin 
    intSec = Second(Now()): If intSec < 10 Then intSec = "0" & intSec 

    ' Build the filename 
    strFileName = intYear & intMonth & intDay & "_" & intHour & intMin & intSec 

    ' Return the filename 
    funcGetDateTime = strFileName 
End Function 

' SubRouotine to display in a cscript a message and 
' simultaneously write into the log file 
' --------------------------------------------------- 
Sub subRprtProg(strMessage) 
    WScript.Echo strMessage 
    objLog.WriteLine strMessage 
End Sub