;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       PageFile() 
;; 
;;ACTION         Defines the pagefile size & location 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;VERSION        1.0  - 2003/04/14 
;; 
;;HISTORY        1.0  - 2003/04/14 - Initial Release 
;; 
;;SYNTAX         PageFile(Drives, [Update], [Target]) 
;; 
;;PARAMETERS     Drives  - Comma separated list of target pagefile drives 
;; 
;;               Update  - Flag indicating whether the current setting should be updated 
;; 
;;               Target  - Name of remote system to process 
;; 
;;REMARKS        calculate desired PageFileSize (PFILE) on first drive D: or higher 
;;               w/ adquate space. Size the pagefile at 2x(RAM) if under 1.5G,  
;;               1.5x(RAM) if 1.5-2.5G, and 1.25x(RAM) if over 2.5G to a max of 4G 
;;               Places a minimal pagefile on the system drive 
;; 
;;RETURNS        Updated PageFile values 
;; 
;;DEPENDENCIES   OSInfo(), SNVerify() 
;; 
;;TESTED WITH    NT4, Win2K, WinXP, Win2K3, Vista, x64 
;; 
;;EXAMPLES        
; 
Function PageFile($_fDrives, Optional $_fUpdate, Optional $_fTarget)
 
  Dim $_, $_fMemSize, $_PFile, $_Drive, $_DPath, $_PF, $_SF, $_DL, $_RC, $_x64
  Dim $_wDrive, $_FreeSpace, $_LastResort, $_aMem, $_PFReq, $_SysDrive
 
 
  ; Insure $_System has "\\System\" format if it is specified  
  If $_fTarget <> '' $_fTarget = '\\' + Join(Split($_fTarget, '\'), '', 3) + '\' EndIf
 
  ; determine system's O/S type based on architecture 
  $_ = $_fTarget + 'HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
  $_x64 = IIf(ReadValue($_, 'PROCESSOR_ARCHITECTURE') = 'AMD64', 1, 0)
 
  ; need to determine current pagefile size/location 
  $_aMem = Split(OSInfo($_fTarget)[19], ' ')
  If @ERROR Exit @ERROR EndIf
  $_fMemSize = Val($_aMem[0])
 
  ; set the target pagefile size 
  Select
  Case $_fMemSize => 2560
    $_PFile = ($_fMemSize * 125) / 100	; 1.25X RAM 
  Case $_fMemSize => 1536
    $_PFile = ($_fMemSize * 150) / 100	; 1.5X RAM 
  Case $_fMemSize <= 1536
    $_PFile = ($_fMemSize * 2)		; 2X RAM 
  EndSelect
 
  ; Limit max size to 4095 for x86 systems 
  $_PFile = IIf($_PFile > 4095 And Not $_x64, 4095,$_PFile)
 
  ; get size in KBytes, plus 10% 
  $_PFReq = ($_PFile * 1024.0) * 1.1
 
  ; Process each drive in the list...  
  $_PF = 0					; flag is not set 
  For Each $_DL in Split($_fDrives, ',')
    $_SF = 0
    $_wDrive = Left(Trim($_DL), 1)		; get raw drive letter 
 
    ; Set the path to either "D:" or "\\server\D$_" format 
    $_DPath = IIf($_fTarget = '', $_wDrive + ':', $_fTarget + $_wDrive + Chr(36))
 
    ; Skip if local process (fTarget='') and drive is not local 
    If $_fTarget = ''
      Shell '%ComSpec% /c net use | %SystemRoot%\system32\find /i "' + $_DPath + '" >NUL:'
      $_SF = IIf(@ERROR = 0,1,0)
    EndIf
 
    ; determine the drive's free space 
    $_FreeSpace = GetDiskSpace($_DPath)
 
    ; If accessible and not set and not network drive 
    If @ERROR = 0 and $_PF = 0 And $_SF = 0
 
      ; If the drive has a paging file, add the size to what's available 
      If InStr($_aMem[1], $_Drive) > 0
        $_FreeSpace = Val($_FreeSpace) + (Val($_aMem[3]) * 1024)
      EndIf
 
      ; If there is adequate free space, define the page file and quit 
      If $_FreeSpace > $_PFreq
        ; Adequate space, but SystemDrive is not a preferred location 
        $_SysDrive = Left(ReadValue($_fTarget + 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion', 'SystemRoot'), 1)
        If $_wDrive = Left($_SysDrive, 1)
          $_LastResort = $_wDrive
        Else
          $_Drive = $_wDrive
          $_PF = 1
        EndIf
      EndIf
    EndIf
  Next
  
  $_Drive = IIf($_PF <> 1, $_LastResort, $_Drive)
  $_Drive = $_Drive + ':'
  ; Set size of SYSTEMDRIVE pagefile based on RAM 
  $_ = CInt(((1.05*$_fMemSize) - (1.05*$_fMemSize) Mod 256) / 256) * 32
  $_ = CStr(IIf($_ < 64, 64, $_)) + ' '
  $Pagefile = IIf($_PF <> 1, '', '%SYSTEMDRIVE%\pagefile.sys ' + $_ + $_ + '|')
 
  $Pagefile = $Pagefile + $_Drive + '\pagefile.sys ' + $_PFile + ' ' + $_PFile
 
  If $_fUpdate
    $_RC = WriteValue($_fTarget + 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management', 'PagingFiles', $PageFile, 'REG_MULTI_SZ')
    Exit @ERROR  
  Else
    Exit 0
  EndIf
 
 
EndFunction