WriteLine will not write lines with code

859 views Asked by At

My method CreateScript will write all lines except for the three listed at the bottom.

Sub CreateScript   
  Dim objWMIService, arrIPAddress, colNetAdapters
  Dim objNetAdapter, arrSubnetMask, errEnableStatic
  Dim arrGateway, errGateways, fsow, line

  ip = FindIP   'My function to lookup the ip. Works fine, no issues there.

  arrIPAddress = Array(ip)
  arrSubnetMask = Array("255.255.255.0")
  arrGateway = Array(ip)

  Set fsow = fso.OpenTextFile(strSigPath & "\" & user & ".wsf", 2, True)

  fsow.WriteLine "<" & "job>" & "<" & "script language=" & chr(34) & "VBScript" & chr(34) & ">"

  fsow.WriteLine "Dim arrIPAddress, arrSubnetMask, arrGateway, machinename colNetAdapters, errEnableStatic, objWMIService, objNetAdapter"
  fsow.WriteLine "arrIPAddress = " & arrIPAddress(0) & "." & arrIPAddress(1) & "." & arrIPAddress(2) & "." & arrIPAddress(3) & ""
  fsow.WriteLine "arrSubnetMask = Array(" & chr(34) & "255.255.255.0" & chr(34) & ")"
  fsow.WriteLine "arrGateway = " & Array(ip) & ""
  fsow.WriteLine "arrGateway(0) = 10"
  fsow.WriteLine "arrGateway(3) = 250"

  fsow.WriteLine "Set objWMIService = GetObject(" & chr(34) & "winmgmts:\\" & chr(34) & " & machinename & " & chr(34) & "\root\cimv2" & chr(34) & ")"
  fsow.WriteLine "Set colNetAdapters = objWMIService.ExecQuery(" & chr(34) & "Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE" & chr(34) & ")"
  fsow.WriteLine "For Each objNetAdapter in colNetAdapters"
  fsow.WriteLine
  fsow.WriteLine "errEnableStatic = objNetAdapter.EnableStatic(" & arrIPAddress & "," & arrSubnetMask & ")"
  fsow.WriteLine "If Not errEnableStatic = 0 Then"
  fsow.WriteLine "WScript.Echo" & chr(34) & "Failure assigning IP/Subnet." & chr(34)
  fsow.WriteLine "End If"
  fsow.WriteLine
  fsow.WriteLine "errGateways = objNetAdapter.SetGateways(" & arrGateway & ")"
  fsow.WriteLine "If Not errGateways = 0 Then"
  fsow.WriteLine "WScript.Echo" & chr(34) & "Failure assigning Gateway." & chr(34)
  fsow.WriteLine "End If"
  fsow.WriteLine "Next"
  fsow.WriteLine "WScript.Quit"
  fsow.WriteLine "</sc" & "ript></job>"
End Sub '***** CreateScript

I've tried writing this code in several different ways, but I just can't get the following lines to write out to my wsf txt file. Every other line will write to the file perfectly. Why will my WriteLine method not write those three lines?

fsow.WriteLine "arrIPAddress = " & arrIPAddress(0) & "." & arrIPAddress(1) & "." & arrIPAddress(2) & "." & arrIPAddress(3) & ""
fsow.WriteLine "errEnableStatic = objNetAdapter.EnableStatic(" & arrIPAddress & "," & arrSubnetMask & ")"
fsow.WriteLine "errGateways = objNetAdapter.SetGateways(" & arrGateway & ")"
1

There are 1 answers

0
Ansgar Wiechers On BEST ANSWER

You have an On Error Resume Next in your code that you didn't tell us about. Don't do that.

fsow.WriteLine "arrIPAddress = " & arrIPAddress(0) & "." & arrIPAddress(1) & "." & arrIPAddress(2) & "." & arrIPAddress(3) & ""

This line fails with an index out of bounds error, because Array(ip) creates an array with one element: the string value of the variable (parameter?) ip, which is most likely a string according to your comments.

To make this work you need to Split(ip) (which is apparently what you're doing now).

fsow.WriteLine "errEnableStatic = objNetAdapter.EnableStatic(" & arrIPAddress & "," & arrSubnetMask & ")"
fsow.WriteLine "errGateways = objNetAdapter.SetGateways(" & arrGateway & ")"

These two lines fail with type mismatch errors, because you cannot concatenate arrays with strings.

To make these work you need to either define the concatenated variables as strings (the "arrays" contain just a single element anyway), or join the arrays to strings (Join(arr, ".")).

The root cause of your problem appears to be a fundamental misunderstanding of what the Array function does. You seem to be under the impression that it splits a string into an array of elements. That is not the case (splitting strings is what the Split function is for). The Array function returns an array of its arguments. Array("a.b.c.d") produces an array with just one string element a.b.c.d, Array("a.b", "c.d") an array with two elements (the strings a.b and c.d), and so on.