2016-04-26 71 views
0

我正在編寫一個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有什麼關係?

+0

歡迎使用StackOverflow;感謝您發佈您的第一個問題。請確定你正在談論的具體陳述。這對新用戶閱讀以下內容很有幫助:[如何提出一個好問題](http://stackoverflow.com/help/how-to-ask),[完美問題](http://codeblog.jonskeet .uk/2010/08/29/writing-the-perfect-question /)和[Minimal,Complete,and Verifiable example](http://stackoverflow.com/help/mcve)。請考慮編輯你的問題。 – MikeC

回答

0

您的問題不符合Minimal, Complete, and Verifiable example標準。
然而,乍一看:讀取On Error StatementReadLine MethodWorking 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)...

+0

首先,我不得不不同意不符合代碼標準。可能有一些代碼太多,因此不符合「最小」方面,但我需要它來顯示變量來自哪裏。這就是爲什麼我提供了代碼牆。將來,我會盡量避免這樣做,因爲它一定會讓你更難回答。話雖如此,你的回答很有道理。我顯然不明白f.ReadLine的含義,但你已經明確表達了。謝謝。我將通過將f.ReadLine分配給一個變量並重新檢入來嘗試解決方案。再次感謝。 – StayFroztee

+0

這工作,謝謝。 – StayFroztee

相關問題