VBS脚本设置虚拟内存大小和位置

标签: , , , , , ,

VBS脚本设置虚拟内存(页面文件)大小和位置。

'VBS脚本设置虚拟内存(页面文件)大小和位置
'VBScript Change the Size and Location of PageFile.
'By powerbat @ www.bathome.net 批处理之家

Dim objWMIService, MemSize, objOS, ver, objCS, AutomaticManagedPagefile
Dim Drive, MinSize, MaxSize, strReboot

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate, (CreatePagefile, Shutdown)}!\\" _
    & strComputer & "\root\cimv2")

MemSize = 0
Set colMemory = OBJWMIService.Get("Win32_PhysicalMemory").Instances_()
for each objMemory in colMemory
    MemSize = MemSize + objMemory.Capacity
next
MemSize = MemSize / 1024^2 'in units of MB

Set colOS = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objOS in colOS
    SystemDrive = objOS.SystemDrive
    ver = objOS.Version
    Exit For 'Reserve object for subsequent use.
Next

Drive = Left(SystemDrive, 1)
MinSize = Fix(MemSize*1.5)
MaxSize = Fix(MemSize*2)
strReboot = "ask"

AutomaticManagedPagefile = False
if ver > "6" then
    Set colCS = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
    For Each objCS in colCS
        AutomaticManagedPagefile = objCS.AutomaticManagedPagefile
        Exit For 'Reserve object for subsequent use.
    Next
end if

if WScript.Arguments.length then
  Set ArgsNamed = WScript.Arguments.Named
  if Not ArgsNamed.Exists("drive") then
    Command = "cscript " & WScript.ScriptName & " "
    WScript.Echo "Usage:"
    WScript.Echo Command & "/drive:<DriveLetter> " _
      & "/min:<Num or Ratio> /max:<Num or Ratio> /r:<yes|no|ask>"
    WScript.Echo "Example: "
    WScript.Echo Command & "/drive:c /min:1 /max:2 /r:yes"
    WScript.Echo Command & "/drive:d /min:1.5 /max:768 /r:no"
    WScript.Echo Command & "/drive:e /min:512 /max:2 /r:ask"
    WScript.Echo Command & "/drive:f /min:512 /max:768 /r:ask"
    WScript.Quit
  end if
  Drive = ArgsNamed.Item("drive")
  if ArgsNamed.Exists("min") then
    MinSize = Left(ArgsNamed.Item("min"), 1)
    if CDbl(MinSize)<10 then MinSize = MemSize * MinSize
  end if
  if ArgsNamed.Exists("max") then
    MaxSize = ArgsNamed.Item("max")
    if CDbl(MaxSize)<10 then MaxSize = MemSize * MaxSize
  end if
  if ArgsNamed.Exists("r") then
    strReboot = ArgsNamed.Item("r")
  end if
  call SetPageFile()
  WScript.Quit
end if

CurrentSettings = "检测到物理内存大小为 " & MemSize & " MB。" & vbCrLf & vbCrLf
CurrentSettings = CurrentSettings & "当前虚拟内存为"
if AutomaticManagedPagefile then
    CurrentSettings = CurrentSettings & "系统自动管理大小:"
else
    CurrentSettings = CurrentSettings & "自定义大小:"
end if
CurrentSettings = CurrentSettings & vbCrLf & "页面文件位置 " _
    & vbTab & "最小" & vbTab & "最大" & vbCrLf
Set colPageFiles = objWMIService.ExecQuery(_
    "Select * from Win32_PageFileSetting")
For Each objPageFile in colPageFiles
    CurrentSettings = CurrentSettings _
        & objPageFile.Name & vbTab & objPageFile.InitialSize _
        & vbTab & objPageFile.MaximumSize & vbCrLf
Next
CurrentSettings = CurrentSettings & vbCrLf & "是否修改?" & vbCrLf
ans = msgbox(CurrentSettings, vbQuestion+vbYesNo, "批处理之家vbs脚本")
if ans <> vbYes then WScript.Quit

DiskInfo = "分区信息:" & vbCrLf
DiskInfo = DiskInfo & "盘符" & vbTab & "容量GB" & vbTab & "可用空间GB" & vbCrLf
Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk "_
        & " where DriveType='3'")
for each objDisk in colDisks
    DiskInfo = DiskInfo & objDisk.Caption & vbTab _
        & Round(objDisk.Size/1024^3, 2) & vbTab _
        & Round(objDisk.FreeSpace/1024^3, 2) & vbCrLf
next

Drive = InputBox(DiskInfo & vbCrLf & "虚拟内存放置在哪个盘?(输入一个字母)", _
    "批处理之家vbs脚本", Drive)
Drive = Left(Drive, 1)
ans = msgbox("虚拟内存设置信息,是否确认?" & vbCrLf & vbCrLf & _
    Drive & vbTab & MemSize & "*1.5 = " & MinSize & vbTab _
    & MemSize & "*2 = " & MaxSize, _
    vbYesNo+vbQuestion, "批处理之家vbs脚本")
if ans <> vbYes then WScript.Quit
SetPageFile

Sub SetPageFile()
  if ver > "6" then
    If AutomaticManagedPagefile Then
      objCS.AutomaticManagedPageFile = False
      objCS.Put_()
    End If
  end if

  Set colPageFiles = objWMIService.ExecQuery(_
    "Select * from Win32_PageFileSetting")
  For Each objPageFile in colPageFiles
    objPageFile.Delete_()
  Next

  Set objPageFile = objWMIService.Get(_
    "Win32_PageFileSetting").SpawnInstance_()
  objPageFile.Name = Drive & ":\pagefile.sys"
  objPageFile.InitialSize = MinSize
  objPageFile.MaximumSize = MaxSize
  objPageFile.Put_()

  If InStr(1,strReboot,"y",1) then
    objOS.Reboot()
  elseif InStr(1,strReboot,"a",1) then
    ans = msgbox("设置完成,是否重启?", _
        vbQuestion+vbYesNo, "批处理之家vbs脚本")
    if ans = vbYes then objOS.Reboot()
  else
    'not reboot
  end if
End Sub

原文链接:http://bbs.bathome.net/viewthread.php?tid=15768

赞赏

微信赞赏支付宝赞赏

随机文章:

  1. 64位系统VBS调用32位COM组件
  2. C语言标准库函数rand与多线程
  3. NDS模拟器Dualis
  4. 用VBS写的VBS代码格式化工具VbsBeautifier
  5. OpenWrt SSH远程端口转发

留下回复