如果那就不行了

时间:2014-03-31 14:41:56

标签: active-directory

我已经搜索了很多关于这个问题的内容,但我没有找到任何特定于此案例的内容。我正在尝试运行一个脚本,从AD中抓取所有主机,然后检查每个主机的特定文件。创建了两个txt文件,一个有一个,一个没有。我遇到的问题是所有主机名都被添加到'without'文件中,'with'文件是空的,但我知道两个列表都应该列出主机。我知道循环正在运行,因为列出了所有主机名,但它无法应用if / then检查。我为我的剧本的粗暴道歉,我对此非常陌生。我非常感谢任何提示...

Const ADS_SCOPE_SUBTREE = 2

Dim cn
Set cn = CreateObject("ADODB.Connection")
cn.Provider = "ADsDSOObject"
cn.Open "Active Directory Provider"
Dim cmd
Set cmd = CreateObject("ADODB.Command")
Set cmd.ActiveConnection = cn
Dim ou
ou = "DC=mydomain,DC=COM"
cmd.CommandText =   "SELECT name " & _
                    "FROM 'LDAP://" & ou & "' " & _
                    "WHERE objectClass='computer' " & _
                    "ORDER BY name"
cmd.Properties("Page Size") = 1000
cmd.Properties("Searchscope") = ADS_SCOPE_SUBTREE

Const ForAppending = 8
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim yesFile
Set yesFile = fso.OpenTextFile("pcswithsw.txt", ForAppending, True)
Dim noFile
Set noFile = fso.OpenTextFile("pcswithoutsw.txt", ForAppending, True)
Dim rs
Set rs = cmd.Execute
strComputer = rs(0)
rs.MoveFirst

Do Until rs.EOF
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colFiles = objWMIService.ExecQuery("Select * From CIM_DataFile Where Name = 'C:\\somefile.exe'")
        If colFiles.Count = 0 Then
            noFile.WriteLine rs(0)
        Else
            yesFile.WriteLine rs(0)
        End If
    rs.MoveNext
Loop

yesFile.Close
noFile.Close
Set yesFile = Nothing
Set noFile = Nothing
Set fso = Nothing

1 个答案:

答案 0 :(得分:0)

终于搞清楚了。我将主机列表加载到一个数组中,然后将数组送入If / Then语句。它运行缓慢,但大部分处理时间是等待不存在的主机回复的脚本(需要清理AD)。

Const ADS_SCOPE_SUBTREE = 2
Const ForAppending = 8

Dim hostArray()

Dim cn
Set cn = CreateObject("ADODB.Connection")
cn.Provider = "ADsDSOObject"
cn.Open "Active Directory Provider"
Dim cmd
Set cmd = CreateObject("ADODB.Command")
Set cmd.ActiveConnection = cn
Dim ou
ou = "DC=mydomain,DC=COM"
cmd.CommandText =   "SELECT name " & _
                    "FROM 'LDAP://" & ou & "' " & _
                    "WHERE objectClass='computer' " & _
                    "ORDER BY name"
cmd.Properties("Page Size") = 1000
cmd.Properties("Searchscope") = ADS_SCOPE_SUBTREE

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim outFile
Set outFile = fso.OpenTextFile("computers.txt", ForAppending, True)
Dim yesFile
Set yesFile = fso.OpenTextFile("pcswithsw.txt", ForAppending, True)
Dim noFile
Set noFile = fso.OpenTextFile("pcswithoutsw.txt", ForAppending, True)

Dim j
j=0
Dim rs
Set rs = cmd.Execute
rs.MoveFirst

Do Until rs.EOF
    REDIM PRESERVE hostArray(j)
    for i = 0 to rs.EOF
    hostArray(j)=rs(0)
    outFile.WriteLine rs(0)
    rs.MoveNext
    i = i + 1
    j = j + 1
    next
Loop

    For Each strComputer in hostArray
        On Error Resume Next
        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
        Set colFiles = objWMIService.ExecQuery("Select * From CIM_DataFile Where Name = 'C:\\folder\\subfolder\\file.ext'")
            If colFiles.Count = 0 Then
                noFile.WriteLine strComputer
            Else
                yesFile.WriteLine strComputer
            End If
    Next

yesFile.Close
noFile.Close
Set yesFile = Nothing
Set noFile = Nothing
Set fso = Nothing
msgbox("All done")