VBScript错误-必需的对象ObjTSO

时间:2018-07-01 05:45:04

标签: vbscript

'========================================================================
'## 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
'---------------------------------------------------------------------------------------------------------------------------  

我现在按照建议放置了完整的脚本。请帮助以下查询:

  1. 此行未写入日志文件

    objTSO.WriteLine "Last Boot Up Time: " & objWMIDateTime.GetVarDate & vbcrlf & _
        "System Up Time: " &  TimeSpan(objWMIDateTime.GetVarDate,NOW) & _
        " (hh:mm:ss)"
    
  2. 还请通过更改(hours> 336)条件来检查代码的逻辑。该脚本将在计算机上本地执行,因此,我已将其修改为WshShell,而不是先前的重新启动功能。运行“ shutdown.exe -R -T 0”

请指导!谢谢!

1 个答案:

答案 0 :(得分:0)

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

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

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