WriteLine不会用代码写行

时间:2015-06-08 18:41:29

标签: vbscript fso

我的方法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 & ")"

1 个答案:

答案 0 :(得分:0)

您的代码中有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 & ")"

这两行因类型不匹配错误而失败,因为您无法使用字符串连接数组。

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

问题的根本原因似乎是对Array函数的基本误解。您似乎认为它将字符串拆分为元素数组。情况并非如此(分割字符串是Split函数的用途)。 Array函数返回其参数的数组。 Array("a.b.c.d")生成的数组只包含一个字符串元素a.b.c.dArray("a.b", "c.d")一个包含两个元素的数组(字符串a.bc.d),依此类推。< / p>