如果未满足语句条件,则代码仍在执行

时间:2016-04-26 21:13:04

标签: if-statement vbscript

我正在编写一个VBScript,在Active Directory中搜索计算机对象。如果该对象不存在或存在且位于正确的OU中,则它应运行单独的脚本,以创建/将计算机连接到AD。

ObjExist_CorrectOU_7 = Null
ObjExist_CorrectOU_10 = Null

If compare = True Then
  Win7_OU = "OU=DisallowRDP,OU=64Bit,OU=Win8"
  Win10_OU = "OU=DisallowRDP,OU=64Bit,OU=Win10"

  For x = 16 To 46
    If Asc(Mid(objRS.Fields("distinguishedName"), x, 1)) = Asc(Mid(Win7_OU, (x - 15), 1)) Then
      ObjExist_CorrectOU_7 = True
    Else
      ObjExist_CorrectOU_7 = False
    End If
  Next

  For y = 16 To 46
    If Asc(Mid(objRS.Fields("distinguishedName"), y, 1)) = Asc(Mid(Win10_OU, (y - 15), 1)) Then
      ObjExist_CorrectOU_10 = True
    Else
      ObjExist_CorrectOU_10 = False
    End If
  Next
End If

If ObjExist_CorrectOU_7 = True Then
  WScript.Echo "TRUE"
End If

Dim objShell

Set objShell = WScript.CreateObject("WScript.Shell")

filename = "C:\programdata\dell\kace\k2000_deployment_info.conf"
Win7_Deployment = "deployment_name=Windows 7 x64 with SP1, join AD"
Win10_Deployment = "deployment_name=Development Windows 10 (x64), join AD"

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filename)

Do While Not f.AtEndOfStream
  If ((f.ReadLine = Win7_Deployment) Or ((f.ReadLine = Win7_Deployment) And (ObjExist_CorrectOU_7 = True))) Then
    WScript.Echo "IT WORKED!"
    'objShell.Run "JoinAD_Win7.vbs"
    Exit Do
  End If
  On Error Resume Next
Loop

f.Close
Set g = fso.OpenTextFile(filename)

Do While Not f.AtEndOfStream
  If ((g.ReadLine = Win10_Deployment) Or ((g.ReadLine = Win10_Deployment) And (ObjExist_CorrectOU_10 = True))) Then
    'objShell.Run "JoinAD_Win10.vbs"
    WScript.Echo "IT WORKED AGAIN!"
    Exit Do
  End If
  On Error Resume Next
Loop

g.Close

Set objShell = Nothing

我遇到的问题是每次都会执行两个If..Then语句,即使我知道条件绝对没有得到满足。

是否与我使用OrAnd

有关

1 个答案:

答案 0 :(得分:0)

您的问题不符合Minimal, Complete, and Verifiable example条件。
然而,乍一看:阅读On Error StatementReadLine Method以及Working with Files文档。

Do While Not f.AtEndOfStream
  '''  ↓ this `ReadLine` reads every uneven line i.e. the 1st, 3rd, 5th, …
  If ((f.ReadLine = Win7_Deployment) Or ((f.ReadLine = Win7_Deployment) And (ObjExist_CorrectOU_7 = True))) Then
  '''      this one reads every even line ↑      i.e. the 2nd, 4th, 6th, …
    WScript.Echo "IT WORKED!"
    'objShell.Run "JoinAD_Win7.vbs"
    Exit Do
  End If
  On Error Resume Next ' this causes that script continues on line next to IF … THEN
                       ' in case of uneven records in file.
                       ' i.e. runtimme error "Input past end of file"
Loop

使用类似

的内容
Do While Not f.AtEndOfStream
  sReadLine = f.ReadLine
  If ((sReadLine = Win7_Deployment) Or ((sReadLine = Win7_Deployment) And (ObjExist_CorrectOU_7 = True))) Then
    WScript.Echo "IT WORKED!"
    'objShell.Run "JoinAD_Win7.vbs"
    Exit Do
  End If
  ''' get rid of `On Error Resume Next` statement at all
Loop

Do While Not f.AtEndOfStream后跟g.ReadLine怎么办?使用fg(两者中的TextStream object相同)...