locked
SWbemObjectEx: Type mismatch error RRS feed

  • Question

  • I am new to VBScripting - basically trying to setup active NIC on server and calling a subroutine to enable static IP knowing the MAC address of NIC interface, but getting "SWbemObjectEx: Type mismatch".

    Anybody can explain what I'm doing wrong (I can be sure it is something very obvious for a seasoned scripter)?

    Script extract below:

    Sub IPSetup(strNICName,strMACAddress,strIPAddress,strSubnetMask,strGateway,strDNSOrder)
    Dim sIPAddress
    Dim sSubnetMask
    Dim sGateway
    Dim sMACAddress
    Dim sDNSOrder
    Dim sNICName
    Dim sGatewaymetric

    strComputer = "."
    sGatewayMetric = 1

    sNICName = strNICName
    sMACAddress = strMACAddress
    sIPAddress = strIPAddress
    sSubnetMask = strSubnetMask
    sGateway = strGateway
    sDNSOrder = strDNSOrder

    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery _
        ("Select * from Win32_NetworkAdapterConfiguration where MACAddress='" & sMACAddress & "'")
    For Each objItem in colItems
        errEnable = objItem.EnableStatic(sIPAddress, sSubnetMask)
        errGateways = objItem.SetGateways(sGateway, sGatewaymetric)
        errDNSOrder = objItem.SetDNSServerSearchOrder(sDNSOrder)
        errDNSRegister = obItem.SetDynamicDNSRegistration(True)
        If sNICName = "DATA" Then
            errDNSRegister = objItem.SetDynamicDNSRegistration(True)
        Else
            errDNSRegister = objItem.SetDynamicDNSRegistration(False)
        End If
            If errEnable = 0 Then
                WScript.Echo "The IP address has been changed."
            Else
                WScript.Echo "The IP address could not be changed."
            End If
    Next  
    End Sub

    Thank you

    Jeff

    Sunday, November 11, 2012 2:41 AM

Answers

  • There is an example in the link:

    http://msdn.microsoft.com/en-us/library/windows/desktop/aa394595(v=vs.85).aspx

    strComputer = "."
    Set objWMIService = GetObject( _
        "winmgmts:\\" & strComputer & "\root\cimv2")
    Set colNetAdapters = objWMIService.ExecQuery _
        ("Select * from Win32_NetworkAdapterConfiguration " _
            & "where IPEnabled=TRUE")
    strIPAddress = Array("192.168.1.141")
    strSubnetMask = Array("255.255.255.0")
    strGateway = Array("192.168.1.100")
    strGatewayMetric = Array(1)
     
    For Each objNetAdapter in colNetAdapters
        errEnable = objNetAdapter.EnableStatic( _
            strIPAddress, strSubnetMask)
        errGateways = objNetAdapter.SetGateways(_
            strGateway, strGatewaymetric)
    Next
    


    Richard Mueller - MVP Directory Services

    • Marked as answer by Jeffso Sunday, November 18, 2012 4:14 AM
    Sunday, November 11, 2012 5:12 PM

All replies

  • Statis addresses and masks are arrays an dnot strings.

    See the repository for working examples of setting static address.


    ¯\_(ツ)_/¯

    Sunday, November 11, 2012 3:31 AM
  • There is an example in the link:

    http://msdn.microsoft.com/en-us/library/windows/desktop/aa394595(v=vs.85).aspx

    strComputer = "."
    Set objWMIService = GetObject( _
        "winmgmts:\\" & strComputer & "\root\cimv2")
    Set colNetAdapters = objWMIService.ExecQuery _
        ("Select * from Win32_NetworkAdapterConfiguration " _
            & "where IPEnabled=TRUE")
    strIPAddress = Array("192.168.1.141")
    strSubnetMask = Array("255.255.255.0")
    strGateway = Array("192.168.1.100")
    strGatewayMetric = Array(1)
     
    For Each objNetAdapter in colNetAdapters
        errEnable = objNetAdapter.EnableStatic( _
            strIPAddress, strSubnetMask)
        errGateways = objNetAdapter.SetGateways(_
            strGateway, strGatewaymetric)
    Next
    


    Richard Mueller - MVP Directory Services

    • Marked as answer by Jeffso Sunday, November 18, 2012 4:14 AM
    Sunday, November 11, 2012 5:12 PM