0

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

1 回答 1

0

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

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

此行因索引超出范围错误而失败,因为Array(ip)创建了一个包含一个元素的数组:变量 (parameter?) 的字符串值,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)的数组,依此类推。

于 2015-06-08T20:54:07.253 回答