none
Setting Pagefile

    Question

  • I've been using the following script to set pagefile on my servers however for some reason it's not working on server 2012.  I get an error "Exception calling "Put" with "0" arguments "value out of range".  What am I doing wrong?  Can you recommend a better way of doing it?  Thanks.
    $PageFile = Get-WmiObject -class Win32_PageFileSetting
    $RAM = Get-WmiObject Win32_OperatingSystem | select TotalVisibleMemorySize
    $RAM = ($RAM.TotalVisibleMemorySize / 1kb).tostring("00")
    
    if ($RAM -gt 67584){
    $PageFile.InitialSize = 102400
    $PageFile.MaximumSize = 102400
    }
    else
    {
    $PageFile.InitialSize = ([int]$RAM * 1.5)
    $PageFile.MaximumSize = ([int]$RAM * 1.5)
    }
    
    $model = gwmi win32_computersystem | select model
    
    if ($model.model -like "*virtual*"){
    wmic computersystem set AutomaticManagedPagefile=true}
    else
    {
    wmic computersystem set AutomaticManagedPagefile=false
    set-itemproperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management\" -name "pagingfiles" -type multistring -value "c:\pagefile.sys 0 0"
    $PageFile.Put()}


    Wednesday, July 17, 2013 10:11 PM

Answers

  • I think the problem is you're trying to use ToString() for rounding numbers.  Use Round() instead.

    $PageFile = Get-WmiObject -class Win32_PageFileSetting
    $RAM = [Math]::Round((Get-WmiObject Win32_OperatingSystem).TotalVisibleMemorySize / 1kb)
    
    if ($RAM -gt 67584){
        $PageFile.InitialSize = 102400
        $PageFile.MaximumSize = 102400
    }
    else
    {
        $PageFile.InitialSize = [uint32]($RAM * 1.5)
        $PageFile.MaximumSize = [uint32]($RAM * 1.5)
    }
    
    if ((Get-WmiObject win32_computersystem).model -like "*virtual*")
    {
        wmic computersystem set AutomaticManagedPagefile=true
    }
    else
    {
        wmic computersystem set AutomaticManagedPagefile=false
        set-itemproperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management\" -name "pagingfiles" -type multistring -value "c:\pagefile.sys 0 0"
        $PageFile.Put()
    }

    Thursday, July 18, 2013 4:38 AM
  • Hi,

    Do you have automatic management enabled for the pagefile? If so, Win32_PageFileSetting isn't available.

    I noticed that $PageFile was null when I was playing around with your code, which reminded me about this thread from quite a while ago that I recently found (it's about 2008 R2, but it seems to apply to 2012 as well). I tried unchecking the automatic management option and allowing the system to manage the size. Rebooted the server and I was then able get results from Win32_PageFileSetting.

    EDIT:

    Also, AverageJoeOfToronto is totally right about the RAM calculation, you'll need to use that method instead.


    Don't retire TechNet!



    Thursday, July 18, 2013 5:18 AM

All replies

  • I think the problem is you're trying to use ToString() for rounding numbers.  Use Round() instead.

    $PageFile = Get-WmiObject -class Win32_PageFileSetting
    $RAM = [Math]::Round((Get-WmiObject Win32_OperatingSystem).TotalVisibleMemorySize / 1kb)
    
    if ($RAM -gt 67584){
        $PageFile.InitialSize = 102400
        $PageFile.MaximumSize = 102400
    }
    else
    {
        $PageFile.InitialSize = [uint32]($RAM * 1.5)
        $PageFile.MaximumSize = [uint32]($RAM * 1.5)
    }
    
    if ((Get-WmiObject win32_computersystem).model -like "*virtual*")
    {
        wmic computersystem set AutomaticManagedPagefile=true
    }
    else
    {
        wmic computersystem set AutomaticManagedPagefile=false
        set-itemproperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management\" -name "pagingfiles" -type multistring -value "c:\pagefile.sys 0 0"
        $PageFile.Put()
    }

    Thursday, July 18, 2013 4:38 AM
  • Hi,

    Do you have automatic management enabled for the pagefile? If so, Win32_PageFileSetting isn't available.

    I noticed that $PageFile was null when I was playing around with your code, which reminded me about this thread from quite a while ago that I recently found (it's about 2008 R2, but it seems to apply to 2012 as well). I tried unchecking the automatic management option and allowing the system to manage the size. Rebooted the server and I was then able get results from Win32_PageFileSetting.

    EDIT:

    Also, AverageJoeOfToronto is totally right about the RAM calculation, you'll need to use that method instead.


    Don't retire TechNet!



    Thursday, July 18, 2013 5:18 AM
  • That worked perfectly AverageJoeOfToronto.  Thank you.  And... Mike is also right, I noticed that if the pagefile is initially set to Automatic, I have to run my script twice for it to take, the first time it changes it to "system managed" the second time it actually sets the page file. 

    Thanks for your help guys!

    Thursday, July 18, 2013 12:32 PM