Return 远程机器上次启动时间
Return remote machine last boot time
我有一个脚本(见下文)returns 从 txt 文件读取主机名列表的最后一次启动时间。
然而,对于无法访问的机器,它只写入最后一台机器的时间戳。
我需要为无法访问的机器添加什么以输出空白或 'Unreachable' 字符串?
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
当您调用 objWMIService.ExecQuery
并且由于无法访问主机而出错时,您的 On Error Resume Next
导致代码从下一行继续执行并继续执行此操作,直到找到一行不会导致错误。您的变量仍将保留上次调用的值。您应该在调用后使用以下方法测试错误:
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
我有一个脚本(见下文)returns 从 txt 文件读取主机名列表的最后一次启动时间。
然而,对于无法访问的机器,它只写入最后一台机器的时间戳。
我需要为无法访问的机器添加什么以输出空白或 'Unreachable' 字符串?
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
当您调用 objWMIService.ExecQuery
并且由于无法访问主机而出错时,您的 On Error Resume Next
导致代码从下一行继续执行并继续执行此操作,直到找到一行不会导致错误。您的变量仍将保留上次调用的值。您应该在调用后使用以下方法测试错误:
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