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
真是個好主意。我用你的建議重複了腳本。但是它有另一個問題。新腳本只返回計算機OU中的最後一臺計算機。如何正確地將Dictionary中的每個實例傳遞給If語句? – user2847926
在你的函數中,你設置了s = colItems(i),它將在每個循環中重置s,並且在最後一個循環s將=最後一個項目。如果你想返回一個數組,只需設置array = objDictionary.Items。 我更新了代碼以幫助您。 – langstrom
你一直很有幫助。謝謝。我只是在學習如何做到這一點。我工作的公司是一家有3個姐妹的兒童公司。在活動目錄中有數千個我不關心的工作站。有什麼方法可以在這個新功能中指定一個特定的文件夾? – user2847926