VBScript错误-需要对象ObjTSO

VBScript错误-需要对象ObjTSO,vbscript,Vbscript,我现在已经按照建议完成了完整的脚本。请帮助回答以下问题: 此行未写入日志文件 '======================================================================== '## Global Object and Variable Settings '======================================================================== Dim WshShell: Set Wsh

我现在已经按照建议完成了完整的脚本。请帮助回答以下问题:

  • 此行未写入日志文件

    '========================================================================
    '## Global Object and Variable Settings
    '======================================================================== 
    
    Dim WshShell: Set WshShell = CreateObject("WScript.Shell")
    Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
    Dim strSystemDrive: strSystemDrive = WshShell.ExpandEnvironmentStrings("%SystemDrive%")
    
    '========================================================================
    '## Main Code 
    '========================================================================
    
     'On Error Resume Next
    
    '---------------------------------------------------------------------------------------------------------------------------
    '## Script Variables 
    '---------------------------------------------------------------------------------------------------------------------------
    Dim strFolder: strFolder = strSystemDrive & "\Endpoint Reboot Info"
    Dim strFile: strFile = strSystemDrive & "\Endpoint Reboot Info\Endpoint_Reboot_Logfile.txt" 
    strcurrentDateTime = Now()
     Dim string0: string0 = "Endpoint restart analysis initialized"
    Dim string1: string1 = "Calculating the endpoint uptime .. If greater than 14 days, machine will be restarted"
    Dim string2: string2 = "Warning: The current OS Uptime exceeds 14 days! This system will be rebooted!"
     Dim string3: string3 = " As the current OS Uptime is less than 14 days, this system will NOT be rebooted currently"
    Dim string4: string4 = "This system will restart now!"
    Dim string5: string5 = "User has clicked cancel, hence PC was NOT restarted"
    Dim string6: string6 = "User has clicked OK. Restarting PC now.."
    
    '---------------------------------------------------------------------------------------------------------------------------
    '## Code for creating the folder and file necessary for logging and initializing the log file
    '---------------------------------------------------------------------------------------------------------------------------  
    Function CreateLogFile(filename)
    Dim f: set f = objFSO.OpenTextFile(strFile, 8, True)
    f.WriteLine strcurrentDateTime & " " & string0  
    f.WriteLine strcurrentDateTime & " " & string1
    Set CreateLogFile = f
    End Function
    
    '---------------------------------------------------------------------------------------------------------------------------
    '## Code for checking endpoint OS uptime and force restarting post message display to end user
    '---------------------------------------------------------------------------------------------------------------------------
    Dim objTSO: Set objTSO = CreateLogFile(strFile) 
     strComputer = "." 
    Const FOR_APPENDING = 8
    
    SET objWMIDateTime = CREATEOBJECT("WbemScripting.SWbemDateTime")
    SET objWMI = GETOBJECT("winmgmts:\\" & strComputer & "\root\cimv2")
    SET colOS = objWMI.InstancesOf("Win32_OperatingSystem")
    FOR EACH objOS in colOS
    objWMIDateTime.Value = objOS.LastBootUpTime 
    objTSO.WriteLine "Last Boot Up Time: " & objWMIDateTime.GetVarDate & vbcrlf & _
        "System Up Time: " &  TimeSpan(objWMIDateTime.GetVarDate,NOW) & _
        " (hh:mm:ss)"
    NEXT
    
    FUNCTION TimeSpan(dt1, dt2) 
    ' Function to display the difference between
    ' 2 dates in hh:mm:ss format
    IF (ISDATE(dt1) AND ISDATE(dt2)) = FALSE THEN 
        TimeSpan = "00:00:00" 
        EXIT FUNCTION 
        END IF 
    
        seconds = ABS(DATEDIFF("S", dt1, dt2)) 
        minutes = seconds \ 60 
        hours = minutes \ 60 
        minutes = minutes MOD 60 
        seconds = seconds MOD 60 
    
        IF LEN(hours) = 1 THEN hours = "0" & hours 
    
        TimeSpan = hours & ":" & _ 
            RIGHT("00" & minutes, 2) & ":" & _ 
            RIGHT("00" & seconds, 2) 
    
    
    If (hours > 336) Then
        f.WriteLine strcurrentDateTime & " " & string2
        Dim retval: retval = InputBox("Warning!: The current OS Uptime exceeds 14 days! This system will be rebooted! Please save ALL of your work and ONLY then click OK")
        If IsEmpty(retval) Then
                msgbox ("User has terminated the action by clicking cancel")
            objTSO.WriteLine string5                
        Else 
            objTSO.WriteLine string6            
            WshShell.Run "shutdown.exe -R -T 0"             
        End If  
    
    Else
        WScript.Sleep 10000 
        strcurrentDateTime = Now()  
        objTSO.WriteLine strcurrentDateTime & string3       
        WScript.Quit
    End If
    f.Close()
    END FUNCTION 
    '---------------------------------------------------------------------------------------------------------------------------
    '## End of code/VB Script
    '---------------------------------------------------------------------------------------------------------------------------  
    
  • 另外,请通过更改(小时数>336)条件来检查代码的逻辑。此脚本将在计算机上本地执行,因此我现在将其修改为WshShell,而不是以前的重新启动功能。运行“shutdown.exe-R-t0”


  • 请导游!谢谢

    您的函数
    CreateLogFile
    创建/打开文件,然后立即关闭:

    objTSO.WriteLine "Last Boot Up Time: " & objWMIDateTime.GetVarDate & vbcrlf & _
        "System Up Time: " &  TimeSpan(objWMIDateTime.GetVarDate,NOW) & _
        " (hh:mm:ss)"
    

    OpenTextFile
    方法的第3个参数设置为
    True
    会导致该方法创建文件,以防文件丢失。

    您的第3点与其他2点无关,我认为您需要能够做到这一点。请不要每次更新问题时都破坏格式?谢谢。即使像上面那样更改,点1行仍然没有写入日志文件。@ShrutiPramod检查WMI查询是否实际返回结果(
    WScript.Echo-colo.Count
    )。此外,您不能像在第78行中那样在函数
    CreateLogFile
    之外使用
    f
    。是@Ansgar,我检查了(WScript.Echo-colOS.Count)是否返回1。此外,我删除了createLogFile函数之外的f,并用objTSO替换它,但我得到了对象变量not set error。这是不可能的。请确保您正在实际运行您认为是的脚本,例如,在脚本开头添加一行
    WScript.Echo WScript.ScriptFullName
    Function CreateLogFile()
        If objFSO.FileExists(strFile) Then
            Set objTSO = objFSO.OpenTextFile(strFile, FOR_APPENDING)
            objTSO.WriteLine strcurrentDateTime & " " & string0
            objTSO.WriteLine strcurrentDateTime & " " & string1
            objTSO.Close()
        Else
            objFSO.CreateTextFile(strFile)
            Set objTSO = objFSO.OpenTextFile(strFile, FOR_APPENDING)
            objTSO.WriteLine strcurrentDateTime & " " & string0
            objTSO.WriteLine strcurrentDateTime & " " & string1
            objTSO.Close()
        End If
    End Function
    Function CreateLogFile(filename)
        Dim f : Set f = objFSO.OpenTextFile(filename, 8, True)
        f.WriteLine Now & " " & string0
        f.WriteLine Now & " " & string1
        Set CreateLogFile = f
    End Function
    
    Dim objTSO : Set objTSO = CreateLogFile(strFile)
    
    ...
    
    objTSO.Close   'at the end of the script