【问题标题】:Return remote machine last boot time返回远程机器上次启动时间
【发布时间】:2015-10-06 09:33:20
【问题描述】:

我有一个脚本(见下文),它返回从 t​​xt 文件中读取的主机名列表的上次启动时间。

但是对于无法访问的机器,它只会写入最后一台机器的时间戳。

我需要为无法访问的机器添加什么以输出空白或“无法访问”字符串?

    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 

【问题讨论】:

    标签: vbscript wmi


    【解决方案1】:

    当您调用 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
    

    【讨论】:

    • 我已经用你的 If 语句更新了上面的内容,但它不会运行。有什么想法吗?
    • 当你说不会运行时实际发生了什么?
    • 结束后如果抛出“预期语句错误”
    • Next 应该在End If 内,在End If 之后你需要Err.Clear()
    猜你喜欢
    • 2018-12-04
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 2017-10-12
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    • 2018-07-30
    相关资源
    最近更新 更多