WriteLine 不会用代码写行

WriteLine will not write lines with code

我的方法 CreateScript 将写入除底部列出的三行之外的所有行。

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

我试过用几种不同的方式编写这段代码,但我就是无法将以下行写到我的 wsf txt 文件中。每隔一行都会完美地写入文件。为什么我的 WriteLine 方法不会写那三行?

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

您的代码中有一个 On Error Resume Next 没有告诉我们。不要那样做。

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

这一行因索引越界错误而失败,因为 Array(ip) 创建了一个包含一个元素的数组:变量(参数?)的字符串值 ip,这很可能是一个根据您的评论字符串。

要完成这项工作,您需要 Split(ip)(这显然是您现在正在做的事情)。

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

这两行因类型不匹配错误而失败,因为您不能将数组与字符串连接起来。

要完成这些工作,您需要将连接的变量定义为字符串(无论如何 "arrays" 只包含一个元素),或者将数组连接到字符串 (Join(arr, "."))。

你的问题的根本原因似乎是对 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 函数的用途的根本误解。 Array 函数 returns 其参数的数组。 Array("a.b.c.d") 生成一个只有一个字符串元素的数组 a.b.c.dArray("a.b", "c.d") 生成一个包含两个元素(字符串 a.bc.d)的数组,等等。