【发布时间】:2015-10-06 09:33:20
【问题描述】:
我有一个脚本(见下文),它返回从 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
【问题讨论】: