WMI EnableStatic = Type mismatch
I run the following code:
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
When I run this I get on row:
开发者_如何学编程 objNetAdapter.EnableStatic(ipAdd, ipMask)"Type mismatch" error code
Any ideas why? The interface is a PPP interface established by the software that comes with the mobile broadband I use.
MSDN specifies:
uint32 EnableStatic( [in] string IPAddress[], [in] string SubnetMask[] );
What does [in] signify and I believe I have to turn the variables into arrays somehow?
Try changing your declarations to:
Dim ipAdd As String() = {"83.185.88.205"}
Dim ipMask As String() = {"255.255.255.255"}
EDIT
Looking more, every solution that I found that works has been using the System.Management
managed namespace. Check out this code:
http://www.dreamincode.net/code/snippet2015.htm
I know this is an old thread but found some issues and reworked the script to perform the subnetmask change that has bearing I set a variable for the code to hold the ip address, gateway, etc... Hope this helps
' 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
精彩评论