Board logo

标题: [原创] VBS脚本设置虚拟内存(页面文件)大小和位置 [打印本页]

作者: powerbat    时间: 2012-3-5 18:44     标题: VBS脚本设置虚拟内存(页面文件)大小和位置

如需转载请注明出处:http://www.bathome.net/thread-15768-1-1.html
  1. 'VBS脚本设置虚拟内存(页面文件)大小和位置
  2. 'VBScript Change the Size and Location of PageFile.
  3. 'By powerbat @ www.bathome.net 批处理之家
  4. Dim objWMIService, MemSize, objOS, ver, objCS, AutomaticManagedPagefile
  5. Dim Drive, MinSize, MaxSize, strReboot
  6. strComputer = "."
  7. Set objWMIService = GetObject("winmgmts:" _
  8.     & "{impersonationLevel=impersonate, (CreatePagefile, Shutdown)}!\\" _
  9.     & strComputer & "\root\cimv2")
  10. MemSize = 0
  11. Set colMemory = OBJWMIService.Get("Win32_PhysicalMemory").Instances_()
  12. for each objMemory in colMemory
  13.     MemSize = MemSize + objMemory.Capacity
  14. next
  15. MemSize = MemSize / 1024^2 'in units of MB
  16. Set colOS = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
  17. For Each objOS in colOS
  18.     SystemDrive = objOS.SystemDrive
  19.     ver = objOS.Version
  20.     Exit For 'Reserve object for subsequent use.
  21. Next
  22. Drive = Left(SystemDrive, 1)
  23. MinSize = Fix(MemSize*1.5)
  24. MaxSize = Fix(MemSize*2)
  25. strReboot = "ask"
  26. AutomaticManagedPagefile = False
  27. if ver > "6" then
  28.     Set colCS = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
  29.     For Each objCS in colCS
  30.         AutomaticManagedPagefile = objCS.AutomaticManagedPagefile
  31.         Exit For 'Reserve object for subsequent use.
  32.     Next
  33. end if
  34. if WScript.Arguments.length then
  35.   Set ArgsNamed = WScript.Arguments.Named
  36.   if Not ArgsNamed.Exists("drive") then
  37.     Command = "cscript " & WScript.ScriptName & " "
  38.     WScript.Echo "Usage:"
  39.     WScript.Echo Command & "/drive:<DriveLetter> " _
  40.       & "/min:<Num or Ratio> /max:<Num or Ratio> /r:<yes|no|ask>"
  41.     WScript.Echo "Example: "
  42.     WScript.Echo Command & "/drive:c /min:1 /max:2 /r:yes"
  43.     WScript.Echo Command & "/drive:d /min:1.5 /max:768 /r:no"
  44.     WScript.Echo Command & "/drive:e /min:512 /max:2 /r:ask"
  45.     WScript.Echo Command & "/drive:f /min:512 /max:768 /r:ask"
  46.     WScript.Quit
  47.   end if
  48.   Drive = ArgsNamed.Item("drive")
  49.   if ArgsNamed.Exists("min") then
  50.     MinSize = Left(ArgsNamed.Item("min"), 1)
  51.     if CDbl(MinSize)<10 then MinSize = MemSize * MinSize
  52.   end if
  53.   if ArgsNamed.Exists("max") then
  54.     MaxSize = ArgsNamed.Item("max")
  55.     if CDbl(MaxSize)<10 then MaxSize = MemSize * MaxSize
  56.   end if
  57.   if ArgsNamed.Exists("r") then
  58.     strReboot = ArgsNamed.Item("r")
  59.   end if
  60.   call SetPageFile()
  61.   WScript.Quit
  62. end if
  63. CurrentSettings = "检测到物理内存大小为 " & MemSize & " MB。" & vbCrLf & vbCrLf
  64. CurrentSettings = CurrentSettings & "当前虚拟内存为"
  65. if AutomaticManagedPagefile then
  66.     CurrentSettings = CurrentSettings & "系统自动管理大小:"
  67. else
  68.     CurrentSettings = CurrentSettings & "自定义大小:"
  69. end if
  70. CurrentSettings = CurrentSettings & vbCrLf & "页面文件位置 " _
  71.     & vbTab & "最小" & vbTab & "最大" & vbCrLf
  72. Set colPageFiles = objWMIService.ExecQuery(_
  73.     "Select * from Win32_PageFileSetting")
  74. For Each objPageFile in colPageFiles
  75.     CurrentSettings = CurrentSettings _
  76.         & objPageFile.Name & vbTab & objPageFile.InitialSize _
  77.         & vbTab & objPageFile.MaximumSize & vbCrLf
  78. Next
  79. CurrentSettings = CurrentSettings & vbCrLf & "是否修改?" & vbCrLf
  80. ans = msgbox(CurrentSettings, vbQuestion+vbYesNo, "批处理之家vbs脚本")
  81. if ans <> vbYes then WScript.Quit
  82. DiskInfo = "分区信息:" & vbCrLf
  83. DiskInfo = DiskInfo & "盘符" & vbTab & "容量GB" & vbTab & "可用空间GB" & vbCrLf
  84. Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk "_
  85.         & " where DriveType='3'")
  86. for each objDisk in colDisks
  87.     DiskInfo = DiskInfo & objDisk.Caption & vbTab _
  88.         & Round(objDisk.Size/1024^3, 2) & vbTab _
  89.         & Round(objDisk.FreeSpace/1024^3, 2) & vbCrLf
  90. next
  91. Drive = InputBox(DiskInfo & vbCrLf & "虚拟内存放置在哪个盘?(输入一个字母)", _
  92.     "批处理之家vbs脚本", Drive)
  93. Drive = Left(Drive, 1)
  94. ans = msgbox("虚拟内存设置信息,是否确认?" & vbCrLf & vbCrLf & _
  95.     Drive & vbTab & MemSize & "*1.5 = " & MinSize & vbTab _
  96.     & MemSize & "*2 = " & MaxSize, _
  97.     vbYesNo+vbQuestion, "批处理之家vbs脚本")
  98. if ans <> vbYes then WScript.Quit
  99. SetPageFile
  100. Sub SetPageFile()
  101.   if ver > "6" then
  102.     If AutomaticManagedPagefile Then
  103.       objCS.AutomaticManagedPageFile = False
  104.       objCS.Put_()
  105.     End If
  106.   end if
  107.   Set colPageFiles = objWMIService.ExecQuery(_
  108.     "Select * from Win32_PageFileSetting")
  109.   For Each objPageFile in colPageFiles
  110.     objPageFile.Delete_()
  111.   Next
  112.   Set objPageFile = objWMIService.Get(_
  113.     "Win32_PageFileSetting").SpawnInstance_()
  114.   objPageFile.Name = Drive & ":\pagefile.sys"
  115.   objPageFile.InitialSize = MinSize
  116.   objPageFile.MaximumSize = MaxSize
  117.   objPageFile.Put_()
  118.   If InStr(1,strReboot,"y",1) then
  119.     objOS.Reboot()
  120.   elseif InStr(1,strReboot,"a",1) then
  121.     ans = msgbox("设置完成,是否重启?", _
  122.         vbQuestion+vbYesNo, "批处理之家vbs脚本")
  123.     if ans = vbYes then objOS.Reboot()
  124.   else
  125.     'not reboot
  126.   end if
  127. End Sub
复制代码
可以直接运行,交互式设置相关参数;也可以带参数运行,自动化设置。
Usage:
cscript pf.vbs /drive:<DriveLetter> /min:<Num or Ratio> /max:<Num or Ratio> /r:<
yes|no|ask>
Example:
cscript pf.vbs /drive:c /min:1 /max:2 /r:yes
cscript pf.vbs /drive:d /min:1.5 /max:768 /r:no
cscript pf.vbs /drive:e /min:512 /max:2 /r:ask
cscript pf.vbs /drive:f /min:512 /max:768 /r:ask

作者: 9zhmke    时间: 2013-7-8 23:19

我简化一下弄到D盘的:
  1. Function SetPageFile()'设置页面文件
  2.   Set colDrives = objFSO.Drives
  3.   drvnum=0
  4.   For Each objDrive in colDrives
  5.    if objDrive.DriveType=2 then drvnum=drvnum+1:if objDrive.DriveLetter="D" then t3=objDrive.FreeSpace
  6.   Next
  7.   if int(t3/1024/1024) < 4096 then SetPageFile="D盘空间太小":返回
  8.   Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate, (CreatePagefile, Shutdown)}!\\.\root\cimv2")
  9.   Set colPageFiles = objWMIService.ExecQuery("Select * from Win32_PageFileSetting")
  10.   For Each objPageFile in colPageFiles
  11.     objPageFile.Delete_()
  12.   Next
  13.   Set objPageFile = objWMIService.Get("Win32_PageFileSetting").SpawnInstance_()
  14.   objPageFile.Name = "d:\pagefile.sys"
  15.   if int(t3/1024/1024) < 8000 then
  16.       SetPageFile="D盘小于8G"
  17.       objPageFile.InitialSize = 1024 'MinSize
  18.       objPageFile.MaximumSize = 1024 'MaxSize
  19.       'SetPageFile="1024"
  20.   else
  21.       objPageFile.InitialSize = 2048 'MinSize
  22.       objPageFile.MaximumSize = 2048 'MaxSize
  23.       'SetPageFile="2048"
  24.   end if
  25.   objPageFile.Put_()
  26.   SetPageFile="ok"
  27. End Function
复制代码





欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2