2

Trying to write a script that will be run in WinPE, that essentially gets the IP address of the localhost and chooses an action based on the IP range.

In Windows, the script runs flawlessly. However, in WinPE, I'm getting the following error:

script.vbs(1,1) Microsoft VBScript runtime error: Subscript out of range

Google-fu is telling me that has something to do with my array being outside of the range. Here I thought I had a decent understanding, but apparently not.

Code that works as is on Windows:

Option Explicit

Dim sIP, sHostname,sPingBat
Dim aIP
Dim iOct1, iOct2, iOct3, iOct4, iReturn
Dim oWMIService, oCmd, oAdapter
Dim cAdapters

iReturn = 999
sHostname = "."
Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sHostname & "\root\cimv2")
Set cAdapters = oWMIService.ExecQuery("Select IPAddress from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
Set oCmd = CreateObject("Wscript.Shell")

For Each oAdapter in cAdapters
    If Not IsNull(oAdapter.IPAddress) Then 
        sIP = Trim(oAdapter.IPAddress(0))
    Else
        iReturn = 404
        WScript.Quit iReturn
    End If
Next

sIP = CStr(sIP)

aIP = Split(sIP, ".")

iOct1 = CInt(aIP(0))
iOct2 = CInt(aIP(1))
iOct3 = CInt(aIP(2))
iOct4 = CInt(aIP(3))

Now, if I change the declaration of the aIP array to either of the following:

aIP()
aIP(4)

and run

aIP = Split(sIP, ".")

I get

script.vbs(26, 1) Microsoft VBScript runtime error: Type mismatch

Changing the array assignment / split line to

aIP() = Split(sIP,".")

results in

script.vbs(26, 1) Microsoft VBScript runtime error: Subscript out of range

So I'm obviously doing something wrong.

It's also entirely possible that my original error message is completely unrelated to my array range, and WinPE just doesn't like my script (in which case, if anybody has any pointers, it'd be appreciated)

At the moment, I'm mounting my wim to get the install packages to make sure the WMI and Scripting packages are installed from the ADK.

3
  • I don't understand your first error message. It is saying Array out of subscript for Line 1 Column 1 which makes no sense. Commented Apr 27, 2016 at 21:31
  • 1
    My guess is it's returning the IP6 address which looks nothing like a IP4 address, so the expected split fails because there isn't the four expected octets. From the documentation - Array of all of the IP addresses associated with the current network adapter. This property can contain either IPv6 addresses or IPv4 addresses. Commented Apr 28, 2016 at 7:56
  • 1
    The IPAddress at index 0 will be an IP6 one which don't contain . so the Split() will just return a array containing the full string. Simple debug is to write out sIP and check the value. One approach would be to check the value for . first using InStr(). Commented Apr 28, 2016 at 8:02

2 Answers 2

1

There is nothing wrong with the code except the assumption being made about what Win32_NetworkAdapterConfiguration returns.

From MSDN - Win32_NetworkAdapterConfiguration class
Array of all of the IP addresses associated with the current network adapter. This property can contain either IPv6 addresses or IPv4 addresses. For more information, see IPv6 and IPv4 Support in WMI.

Because sIP could contain an IPv6 address the Split() will not work as expected. IPv6 addresses don't contain . as a separator so Split() will return a Array containing the original string as the first index only. Hence attempting to read anything other then aIP(0) will cause an

Microsoft VBScript runtime error:
Subscript out of range

error.

To avoid this use InStr() to check for the existence of . in the sIP variable first, you will also need to iterate through the oAdapter.IPAddress array to check each address to get the correct one, you can't assume IPAddress(0) will always be the right one.

Try this

Dim ips, ip

For Each oAdapter in cAdapters
    ips = oAdapter.IPAddress
    If IsArray(ips) Then
        For Each ip In ips
            If InStr(1, ip, ".") > 0 Then
                sIP = Trim(ip)
                Exit For
            End If
        Next
        If Len(sIP) > 0 Then Exit For
    Else
        iReturn = 404
        WScript.Quit iReturn
    End If
Next

Untested on iPad sorry

Sign up to request clarification or add additional context in comments.

Comments

0

I guess sIP variable contains some string which can not be splitted wity delimiter "."(ex: "somestringwithNoDOT")

So in the 1st case

aIP = Split(sIP,".")   ' Split("somestringwithNoDOT",".")

statement returned only 1 string, which can not be coverted to Integer. So i returned Type mismatch error in below line

iOct1 = CInt(aIP(0))  ' returns Type mismatch error

In the 2nd case

aIP() = Split(sIP,".")  ' Split("somestringwithNoDOT",".")

above statement will return 1 element, but aIP is array with NO elements. So this statement rturned "Subscript out of range" error

Resolution for this issue is to check whether correct value is passing to sIP

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.