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

gv8xihay  于 2022-12-23  发布在  Shell
关注(0)|答案(4)|浏览(131)

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

jrcvhitl

jrcvhitl1#

下面是我们如何通过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;
hgc7kmma

hgc7kmma2#

此处为解决方案这是将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"
d4so4syb

d4so4syb3#

好吧,要使用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

作者:迈克·卡纳科斯
但是***设置***页面文件会遇到各种各样的问题。比如,它会有真实的的bug。如果设置了,你可以更改它,如果没有,你必须先设置为系统管理,然后设置为一些东西,比如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

或者您在伊势中加载函数,运行它以加载到内存中,然后从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
q0qdq0h2

q0qdq0h24#

对于较新版本的PowerShell:
从PowerShell 3.0开始,Get-WmiObject cmdlet已被Get-CimInstance取代。

$PageFile = Get-CimInstance -ClassName Win32_PageFileSetting -Filter "Name like '%pagefile.sys'"
$PageFile | Remove-CimInstance
$PageFile = New-CimInstance -ClassName Win32_PageFileSetting -Property @{ Name= "C:\pagefile.sys" }
$PageFile | Set-CimInstance -Property @{ InitialSize = 0; MaximumSize = 0 }

**注意:**您需要管理员权限才能更改页面文件。

相关问题