我有一个脚本(见下文),它返回从txt文件读入的主机名列表的上次启动时间。
但是对于无法访问的计算机,它只会写入最后一台计算机的时间戳。
对于无法输出空白或“无法访问”字符串的计算机,我需要添加什么内容?
On Error Resume Next
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
' =====================================================================
'Gets the script to run against each of the computers listed
'in the text file path for which should be specified in the syntax below
' =====================================================================
Set objTextFile = objFSO.OpenTextFile("C:\temp\reboot\machines.txt", ForReading)
Set outfile = objFSO.CreateTextFile("Report.txt")
Do Until objTextFile.AtEndOfStream
strComputer = objTextFile.Readline
' ===============================================================================
' Code to get the Last Boot Time using LastBootupTime from Win32_Operating System
' ===============================================================================
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
For Each objOS in colOperatingSystems
dtmBootup = objOS.LastBootUpTime
dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
'OutFile.WriteLine "=========================================="
OutFile.WriteLine "Computer: " & strComputer
OutFile.WriteLine "Last Reboot: " & dtmLastBootupTime
dtmSystemUptime = DateDiff("h", dtmLastBootUpTime, Now)
OutFile.WriteLine "System is online since " & dtmSystemUptime & " hours"
OutFile.WriteLine "=========================================="
Next
' =====================================================================
' End
' =====================================================================
Loop
objTextFile.Close
' ===============================================================================
' Displaying to the user that the script execution is completed
' ===============================================================================
MsgBox "Script Execution Completed. The Report is saved as Report.txt in the current directory"
' ===============================================================================
' Function to convert UNC time to readable format
' ===============================================================================
Function WMIDateStringToDate(dtmBootup)
WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & "/" & _
Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) _
& " " & Mid (dtmBootup, 9, 2) & ":" & _
Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup, _
13, 2))
End Function
以下是更新的工作脚本
On Error Resume Next
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
' =====================================================================
'Gets the script to run against each of the computers listed
'in the text file path for which should be specified in the syntax below
' =====================================================================
Set objTextFile = objFSO.OpenTextFile("C:\temp\reboot\machines.txt", ForReading)
Set outfile = objFSO.CreateTextFile("Report.txt")
Do Until objTextFile.AtEndOfStream
strComputer = objTextFile.Readline
' ===============================================================================
' Code to get the Last Boot Time using LastBootupTime from Win32_Operating System
' ===============================================================================
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
If Err.Number <> 0 Then
OutFile.WriteLine "=========================================="
OutFile.WriteLine "Computer: " & strComputer
OutFile.WriteLine "Unreachable"
OutFile.WriteLine "=========================================="
Else
For Each objOS in colOperatingSystems
dtmBootup = objOS.LastBootUpTime
dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
OutFile.WriteLine "=========================================="
OutFile.WriteLine "Computer: " & strComputer
OutFile.WriteLine "Last Reboot: " & dtmLastBootupTime
dtmSystemUptime = DateDiff("h", dtmLastBootUpTime, Now)
OutFile.WriteLine "=========================================="
Next
End if
Err.Clear()
' =====================================================================
' End
' =====================================================================
Loop
objTextFile.Close
' ===============================================================================
' Displaying to the user that the script execution is completed
' ===============================================================================
MsgBox "Script Execution Completed. The Report is saved as Report.txt in the current directory"
' ===============================================================================
' Function to convert UNC time to readable format
' ===============================================================================
Function WMIDateStringToDate(dtmBootup)
WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & "/" & _
Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) _
& " " & Mid (dtmBootup, 9, 2) & ":" & _
Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup, _
13, 2))
End Function
答案 0 :(得分:1)
当您拨打电话objWMIService.ExecQuery
且由于主机无法访问而导致错误时,您的On Error Resume Next
会导致代码从下一行继续执行,并继续执行此操作,直到找到不符合要求的行为止t导致错误。您的变量仍将保留上一次调用的值。您应该在通话后使用以下方法测试错误:
If Err.Number <> 0 Then
'An error has occurred so output the "Unreachable" message
Else
'Call was successful so output last boot time.
End If