'XP_PageFile_Monitor.vbs - Logs the size of pagefile.sys every x minutes.
'For Windows XP.  Creates a scheduled task which runs this script.
'Results logged in pagefile.log in the same folder as this script.

'Installation: Run the script and respond to the prompts.  Uses XP schtasks.exe
'to create a scheduled task with user choice of minutes between runs.

'Uninstall: Disable or remove the scheduled task.

'Author: Bill James - bill@billsway.com - rev 23 Jan 2002

'---------------------------------------------------------------

Option Explicit
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

If Setup() Then UpdateLog

Cleanup()

'---------------------------------------------------------------

Sub UpdateLog()
  Dim ThisFldr, wmi, i, NewRead, DateLine, arOldLines, StartLine
  ThisFldr = fso.GetFile(WScript.ScriptFullName).ParentFolder
  Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate}")
  For Each i In wmi.ExecQuery("Select Name, CurrentUsage, PeakUsage, " & _
                              "AllocatedBaseSize From Win32_PageFileUsage")
    NewRead = Now & " " & i.Name & " - In Use: " & i.CurrentUsage & _
              " MB, Peak Size: " & i.PeakUsage & " MB, Allocated: " & _
              i.AllocatedBaseSize & " MB"
  Next
  Set wmi = Nothing

  If NOT fso.OpenTextFile(ThisFldr & "\pagefile.log", 1, True).AtEndOfStream Then
    'read existing log to array
    arOldLines = Split(fso.OpenTextFile(ThisFldr & "\pagefile.log", 1, True).ReadAll, vbcrlf)
    'get session date & time
    DateLine = Cdate(Mid(arOldLines(0), 18))
  Else 'create new logfile
    With fso.OpenTextFile(ThisFldr & "\pagefile.log", 2, True)
      .WriteLine "Session Started: " & LastBoot
      .WriteLine NewRead
      .WriteLine String(80, "=")
      .Close
    End With
    Exit Sub
  End If

  If DateDiff("s", DateLine, LastBoot) <> 0 Then 'new session
    StartLine = 0
  Else
    StartLine = 3
  End If
  
  'rewrite log
  With fso.OpenTextFile(ThisFldr & "\pagefile.log", 2, True)
    .WriteLine "Session Started: " & LastBoot
    .WriteLine NewRead
    .WriteLine String(80, "=")
    For i = StartLine to UBound(arOldLines)
      If arOldLines(i) <> "" Then .WriteLine arOldLines(i)
    Next
    .Close
  End With
End Sub

Sub Cleanup()
  Set fso = Nothing
  WScript.Quit
End Sub

Function Setup()
  'Check for schedule job, exit if found.
  Dim PageFileJob, Freq, ThisScript
  PageFileJob = fso.GetSpecialFolder(0) & "\Tasks\Pagefile Monitor.job"
  If fso.FileExists(PageFileJob) Then
    Setup = True
    Exit Function
  End If
  'Prompt to create scheduled task
  If MsgBox("Do you want to create a scheduled task to monitor the " & _
            "size of pagefile.sys?", 4, WScript.ScriptName) = 6 Then
    'Prompt for schedule frequency
    Freq = InputBox("How often, in minutes, do you want to check " & _
                    "the size of pagefile.sys", WScript.ScriptName, 30)
    If Freq = "" OR NOT IsNumeric(Freq) Then
      Exit Function
    Else
      Freq = CInt(Freq)
    End If
    'Create the scheduled job (task scheduler doesn't like long file names)
    ThisScript = fso.GetFile(WScript.ScriptName).ShortPath
    'Use XP schtasks.exe
    CreateObject("WScript.Shell").Run _
        "cmd /c schtasks /create /sc minute /mo " & Freq & _
        " /tn ""Pagefile Monitor"" /tr " & ThisScript & _
        " /ru ""System""", 0, True
    MsgBox "Pagefile size will be checked every " & Freq & " minutes." & _
           vbcrlf & vbcrlf & "To stop monitoring, remove the sceduled task.", _
           4096, WScript.ScriptName
    UpdateLog() 'First run
  End If
End Function

Function LastBoot()
  Dim i, DateTime
  For Each i in GetObject("winmgmts:{impersonationLevel=impersonate}")._
      ExecQuery("Select LastBootUpTime, CurrentTimeZone, " & _
                "LocalDateTime From Win32_OperatingSystem")
    With CreateObject("WbemScripting.SWbemDateTime")
      .Value = i.LastBootUpTime
      LastBoot = DateAdd("n", -i.CurrentTimeZone, .GetVarDate)
    End With
  Next
End Function