用于设置 pagefile.sys 大小的 PowerShell 脚本

PowerShell Script to set the size of pagefile.sys

如何通过 PowerShell 在 Windows(pagefile.sys) 上设置页面文件的大小?

这是我们可以通过 PowerShell 更新 pagefile.sys 大小的方法:

# PowerShell Script to set the size of pagefile.sys

$computersys = Get-WmiObject Win32_ComputerSystem -EnableAllPrivileges;
$computersys.AutomaticManagedPagefile = $False;
$computersys.Put();
$pagefile = Get-WmiObject -Query "Select * From Win32_PageFileSetting Where Name like '%pagefile.sys'";
$pagefile.InitialSize = <New_Value_For_Size_In_MB>;
$pagefile.MaximumSize = <New_Value_For_Size_In_MB>;
$pagefile.Put();

执行如下脚本:

PS> .\update_pagefile_size.ps1;

嗯,要使用 Powershell 获取页面文件,请使用我从 Mike Kanakos 那里得到的这个函数:“function-getpagefilessize.ps1”,出于某种原因, 从配置文件作为 PS 文件或 PSM 文件工作:

Function Get-PageFileInfo {

<# 
 .Synopsis 
  Returns info about the page file size of a Windows computer. Defaults to local machine. 

 .Description 
  Returns the pagefile size info in MB. Also returns the PageFilePath, PageFileTotalSize, PagefileCurrentUsage,
  and PageFilePeakusage. Also returns if computer is using a TempPafeFile and if the machine's pagefile is
  managed by O/S (AutoManaged = true) or statically set (AutoManaged = False)

  

 .Example 
  Get-PageFileInfo -computername SRV01
  Returns pagefile info for the computer named SRV01

  Computer             : SRV01
  FilePath             : C:\pagefile.sys
  AutoManagedPageFile  : True
  TotalSize (in MB)    : 8192
  CurrentUsage (in MB) : 60
  PeakUsage (in MB)    : 203
  TempPageFileInUse    : False


 .Example 
  Get-PageFileInfo SRV01, SRV02
  Returns pagefile info for two computers named SRV01 & DC02.

  Computer             : SRV01
  FilePath             : C:\pagefile.sys
  AutoManagedPageFile  : True
  TotalSize (in MB)    : 8192
  CurrentUsage (in MB) : 60
  PeakUsage (in MB)    : 203
  TempPageFileInUse    : False

  Computer             : SRV02
  FilePath             : C:\pagefile.sys
  AutoManagedPageFile  : True
  TotalSize (in MB)    : 8192
  CurrentUsage (in MB) : 0
  PeakUsage (in MB)    : 0
  TempPageFileInUse    : False

.Example 
  Get-PageFileInfo SRV01, SRV02, SRV03 | Format-Table
  Returns pagefile info for three computers named SRV01, SRV02 & SRV03 in a table format.


  Computer  FilePath        AutoManagedPageFile TotalSize (in MB) CurrentUsage (in MB) PeakUsage (in MB) TempPageFileInUse
  --------  --------        ------------------- ----------------- -------------------- ----------------- -----------------
  SRV01    C:\pagefile.sys                True              8192                   60               203             False
  SRV02    C:\pagefile.sys                True             13312                    0                 0             False
  SRV03    C:\pagefile.sys                True              2432                    0                 0             False

 
  .Parameter computername 
  The name of the computer to query. Required field.

 .Notes 
  NAME: Get-PageFileInfo 
  AUTHOR: Mike Kanakos 
  Version: v1.1
  LASTEDIT: Thursday, August 30, 2018 2:19:18 PM
  
  .Link 
  
  
#> 

[CmdletBinding()]
Param(
    [Parameter(Mandatory=$True,ValueFromPipeline=$True)]  
    [string[]]$ComputerName
)

# Main Part of function


Foreach ($computer in $ComputerName)
{

  $online= Test-Connection -ComputerName $computer -Count 2 -Quiet
    if ($online -eq $true)
     {
      $PageFileResults = Get-CimInstance -Class Win32_PageFileUsage -ComputerName $Computer | Select-Object *
      $CompSysResults = Get-CimInstance win32_computersystem -ComputerName $computer -Namespace 'root\cimv2'
    
      $PageFileStats = [PSCustomObject]@{
        Computer = $computer
        FilePath = $PageFileResults.Description
        AutoManagedPageFile = $CompSysResults.AutomaticManagedPagefile
        "TotalSize(in MB)" = $PageFileResults.AllocatedBaseSize
        "CurrentUsage(in MB)"  = $PageFileResults.CurrentUsage
        "PeakUsage(in MB)" = $PageFileResults.PeakUsage
        TempPageFileInUse = $PageFileResults.TempPageFile
      } #END PSCUSTOMOBJECT
     } #END IF
    else
     {
        # Computer is not reachable!
        Write-Host "Error: $computer not online" -Foreground white -BackgroundColor Red
     } # END ELSE


  $PageFileStats
 
} #END FOREACH


} #END FUNCTION

#作者:Mike Kanakos#

但是 设置 页面文件然后 运行 陷入各种问题。就像,它得到了真正的越野车。如果已设置,您可以更改它,如果未设置,则必须先设置为系统管理,然后设置为某些内容,例如 C 上的 16384/16384 和 D: 上的系统管理。 我正在自己研究答案,因为我需要这个,当我整理好它时(在我的一长串其他脚本中)我会回复你...... 但是,ITMT,该功能会有所帮助。对这样的列表执行 ForEach:

remove-item -Force volumeletter:\folder\outputfile.txt
$results = (Get-PageFileInfo -Verbose server1.domain,server2.domain |select * |format-table -AutoSize)
$results |out-file volumeletter:\folder\outputfile.txt -Force ascii

或者您在 ISE 中加载函数,运行 将其加载到内存中,然后从 PS cmd:

手动查询每个服务器
Get-PageFileInfo server1.domain

Computer            : server1.domain
FilePath            : {C:\pagefile.sys, D:\pagefile.sys}
AutoManagedPageFile : False
TotalSize(in MB)    : {16384, 768}
CurrentUsage(in MB) : {88, 62}
PeakUsage(in MB)    : {120, 84}
TempPageFileInUse   : {False, False}

如果您需要使用 FQDN,您将看到空白... 如果您有标准系统管理,该功能将为您提供用法,并告诉您是否必须设置静态尺寸:

Get-PageFileInfo server2.domain

Computer            : server2.domain
FilePath            : C:\pagefile.sys
AutoManagedPageFile : True
TotalSize(in MB)    : 7679
CurrentUsage(in MB) : 1763
PeakUsage(in MB)    : 4867
TempPageFileInUse   : False

这里是解决方案 这是将 C VOL 页面文件设置为 16384MB,将静态和 D VOL 页面文件设置为系统管理:

# PowerShell Script to set the size of pagefile.sys
# update_pagefile_size.ps1
$pagefile = Get-WmiObject Win32_ComputerSystem -EnableAllPrivileges
$pagefile.AutomaticManagedPagefile = $false
#$pagefile.put() | Out-Null
Gwmi win32_pagefilesetting | where{$_.caption -like 'C:*'}
$pagefileset = Gwmi win32_pagefilesetting | where{$_.caption -like 'C:*'}
$pagefileset.InitialSize = 16384
$pagefileset.MaximumSize = 16384
$pagefileset.Put() | Out-Null

if((Set-WmiInstance -Class Win32_PageFileSetting -Arguments @{name="C:\pagefile.sys";InitialSize = $pagefileset.InitialSize; MaximumSize = $pagefileset.MaximumSize} -EnableAllPrivileges -Verbose) -icontains "already exists"){
$pagefileset = Gwmi win32_pagefilesetting | where{$_.caption -like 'C:*'}
$pagefileset.Delete()
$pagefileset.InitialSize = 16384
$pagefileset.MaximumSize = 16384
Set-WmiInstance -Class Win32_PageFileSetting -Arguments @{name="C:\pagefile.sys";InitialSize = $pagefileset.InitialSize; MaximumSize = $pagefileset.MaximumSize} -EnableAllPrivileges
Gwmi win32_pagefilesetting | where{$_.caption -like 'C:*'}
}
$pagefileset = Gwmi win32_pagefilesetting | where{$_.caption -like 'D:*'}
$pagefileset.InitialSize = 0
$pagefileset.MaximumSize = 0
$pagefileset.Put() | Out-Null

Gwmi win32_pagefilesetting | where{$_.caption -like 'D:*'}
Set-WmiInstance -Class Win32_PageFileSetting -Arguments @{name="D:\pagefile.sys";InitialSize = 0; MaximumSize = 0} -EnableAllPrivileges | Out-Null

Write-host "Don't forget to reboot"
#shutdown /r /t 120 /c "rebooting to fix pagefile"