2013-10-07 78 views
0

真是個好主意。我用你的建議重複了腳本。但是它有另一個問題。新腳本只返回計算機OU中的最後一臺計算機。如何正確地將Dictionary中的每個實例傳遞給If語句?AD查詢「遠程服務器不存在或不可用」

dim strComputer, objFileToWrite, objWMIService 

If Reachable(QueryAD) Then 

Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile("\\cheeng.net\winc\IT\NuanceKey.txt",8,true) 

Set objWMIService = GetObject("winmgmts:" _ 
& "{impersonationLevel=impersonate}!\\" & QueryAD & "\root\cimv2") 
Set colComputer = objWMIService.ExecQuery _ 
("Select * from Win32_ComputerSystem") 
For Each objComputer in colComputer 
    objFileToWrite.Write VBNewLine & "User Name = " & objComputer.UserName _ 
     & VBNewLine & "Computer Name = " & objComputer.Name 
Next 
WScript.Echo QueryAD & " Computer is Reachable!" 
Else 
WScript.Echo QueryAD & "Computer is Unreachable!" 
End If 



Function QueryAD 

Dim objDictionary, strItem, colItems, i, s 
Set objDictionary = CreateObject("Scripting.Dictionary") 
Set objOU = GetObject("LDAP://OU=Computers,OU=WINC,DC=cheeng,DC=net") 
objOU.Filter = Array("Computer") 

For Each objComputer in objOU ' Add Workstations to Dictionary 
    objDictionary.Add a, objComputer.CN 
    a = a + 1 
colItems = objDictionary.Items ' Get the workstations. 

for i = 0 to objDictionary.count -1 ' Iterate the array. 
    s = colItems(i) ' Create return string. 

next 
QueryAD = s 

Next 
End Function 


Function Reachable(strComputer) 'Test Connectivty to computer 
Dim wmiQuery, objWMIService, objStatus 

' Define the WMI query 
wmiQuery = "Select * From Win32_PingStatus Where Address = '" & strComputer & "'" 

' Run the WMI query 
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2").ExecQuery(wmiQuery) 

' Translate the query results to either True or False 
For Each objStatus in objWMIService 
If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then 
    Reachable = False 'if computer is unreachable, return false 
Else 
    Reachable = True 'if computer is reachable, return true 
End If 
Next 

Set objWMIService = Nothing 
End Function 

回答

1

在連接到遠程計算機之前,您需要對它進行ping操作以查看它是否在線。這是一個可以做到的功能。

Function Reachable(strComputer) 'Test Connectivty to computer 
Dim wmiQuery, objWMIService, objPing, objStatus 
wmiQuery = "Select * From Win32_PingStatus Where Address = '" & strComputer & "'" 
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2") 
Set objPing = objWMIService.ExecQuery(wmiQuery) 
For Each objStatus in objPing 
    If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then 
     Reachable = False 'if computer is unreachable, return false 
    Else 
     Reachable = True 'if computer is reachable, return true 
    End If 
Next 
End Function 

然後使用這個功能,你可以做一個

If Reachable("computername") Then 
    Set objWMIService = GetObject...etc 

編輯:

你想添加的功能可達您在for循環中,並在發送一個計算機時間的功能。

您也可能想查詢AD只有活動的計算機。例如:

Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile("\\cheeng.net\winc\IT\NuanceKey.txt",8,true) 

arrComps = QueryAD 

For Each strComputer in arrComps 

If Reachable(strComputer) Then 
    Wscript.Echo strComputer & " Computer is Reachable!" 
    Set objWMIService = GetObject("winmgmts:" _ 
     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
    Set colComputer = objWMIService.ExecQuery _ 
     ("Select * from Win32_ComputerSystem") 
    For Each objComputer in colComputer 
     objFileToWrite.Write VBNewLine & "User Name = " & objComputer.UserName _ 
     & VBNewLine & "Computer Name = " & objComputer.Name 
     'You could also use strComputer here instead of objComputer.Name 
Else 'If not reachable 
    Wscript.Echo strComputer & " Computer is Unreachable!" 
End If 'End Reachable If 

Next 'Loop to next computer 

Function QueryAD 
    Const ADS_SCOPE_SUBTREE = 2 
    Dim objDictionary, colItems, strComputer 
    Set objDictionary = CreateObject("Scripting.Dictionary") 

    Set objRootDSE = GetObject("LDAP://RootDSE") 
    strDomain = objRootDSE.Get("DefaultNamingContext") 

    Set objConnection = CreateObject("ADODB.Connection") 
    Set objCommand = CreateObject("ADODB.Command") 
    objConnection.Provider = "ADsDSOObject" 
    objConnection.Open "Active Directory Provider" 

    Set objCOmmand.ActiveConnection = objConnection 
    objCommand.CommandText = _ 
     "Select Name from 'LDAP://" & strDomain & "' " _ 
     & "Where objectClass='computer' and userAccountControl <> 4098 and userAccountControl <> 4130" 
    'This will get all computers except disabled computers from AD 
    objCommand.Properties("Page Size") = 1000 
    objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
    Set objRecordSet = objCommand.Execute 
    objRecordSet.MoveFirst 

    Do Until objRecordSet.EOF 
     strComputer = objRecordSet.Fields("Name").Value 
     objDictionary.Add strComputer,strComputer 
     objRecordSet.MoveNext 
    Loop 
    objRecordSet.Close 
    QueryAD = objDictionary.Items 
End Function 

Function Reachable(strComputer) 'Test Connectivty to computer 
    'keep the same as you had it 
End Function 
+0

真是個好主意。我用你的建議重複了腳本。但是它有另一個問題。新腳本只返回計算機OU中的最後一臺計算機。如何正確地將Dictionary中的每個實例傳遞給If語句? – user2847926

+0

在你的函數中,你設置了s = colItems(i),它將在每個循環中重置s,並且在最後一個循環s將=最後一個項目。如果你想返回一個數組,只需設置array = objDictionary.Items。 我更新了代碼以幫助您。 – langstrom

+0

你一直很有幫助。謝謝。我只是在學習如何做到這一點。我工作的公司是一家有3個姐妹的兒童公司。在活動目錄中有數千個我不關心的工作站。有什麼方法可以在這個新功能中指定一個特定的文件夾? – user2847926

相關問題