class Chef::Resource::WindowsPagefile

Public Instance Methods

automatic_managed?() click to toggle source

see if the pagefile is automatically managed by Windows

@return [Boolean]

# File lib/chef/resource/windows_pagefile.rb, line 210
def automatic_managed?
  @automatic_managed ||= begin
    logger.trace("Checking if pagefiles are automatically managed")
    cmd = "$sys = Get-CimInstance Win32_ComputerSystem -Property *;"
    cmd << "return $sys.AutomaticManagedPagefile"
    powershell_exec!(cmd).result
  end
end
clarify_pagefile_name() click to toggle source

We are adding support for a number of possibilities for how users will express the drive and location they want the pagefile written to.

# File lib/chef/resource/windows_pagefile.rb, line 124
def clarify_pagefile_name
  case new_resource.path
  # user enters C, C:, C:\, C:\\
  when /^[a-zA-Z]/
    new_resource.path[0] + ":\\pagefile.sys"
  # user enters C:\pagefile.sys OR c:\foo\bar\pagefile.sys as the path
  when /^[a-zA-Z]:.*.sys/
    new_resource.path
  else
    raise "#{new_resource.path} does not match the format DRIVE:\\path\\pagefile.sys for pagefiles. Example: C:\\pagefile.sys"
  end
end
create(pagefile) click to toggle source

create a pagefile

@param [String] pagefile path to the pagefile

# File lib/chef/resource/windows_pagefile.rb, line 183
        def create(pagefile)
          converge_by("create pagefile #{pagefile}") do
            logger.trace("Running New-CimInstance -ClassName Win32_PageFileSetting to create new pagefile : #{pagefile}")
            powershell_exec! <<~ELM
              New-CimInstance -ClassName Win32_PageFileSetting -Property  @{Name = "#{pagefile}"}
            ELM
          end
        end
delete(pagefile) click to toggle source

delete a pagefile

@param [String] pagefile path to the pagefile

# File lib/chef/resource/windows_pagefile.rb, line 195
        def delete(pagefile)
          converge_by("remove pagefile #{pagefile}") do
            logger.trace("Running Remove-CimInstance for pagefile : #{pagefile}")
            powershell_exec! <<~EOL
              $page_file = "#{pagefile}"
              $driveLetter = $page_file.split(':')[0]
              $PageFile = (Get-CimInstance -ClassName Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveLetter):'" -ErrorAction Stop)
              $null = ($PageFile | Remove-CimInstance -ErrorAction SilentlyContinue)
            EOL
          end
        end
exists?(pagefile) click to toggle source

See if the pagefile exists

@param [String] pagefile path to the pagefile @return [Boolean]

# File lib/chef/resource/windows_pagefile.rb, line 148
        def exists?(pagefile)
          @exists ||= begin
            logger.trace("Checking if #{pagefile} exists by running: Get-CimInstance Win32_PagefileSetting | Where-Object { $_.name -eq $($pagefile)} ")
            powershell_code = <<~CODE
              $page_file_name = '#{pagefile}';
              $pagefile = Get-CimInstance Win32_PagefileSetting | Where-Object { $_.name -eq $($page_file_name)}
              if ([string]::IsNullOrEmpty($pagefile)) { return $false } else { return $true }
            CODE
            powershell_exec!(powershell_code).result
          end
        end
max_and_min_set?(pagefile, min, max) click to toggle source

is the max/min pagefile size set?

@param [String] pagefile path to the pagefile @param [String] min the minimum size of the pagefile @param [String] max the minimum size of the pagefile @return [Boolean]

# File lib/chef/resource/windows_pagefile.rb, line 166
        def max_and_min_set?(pagefile, min, max)
          logger.trace("Checking if #{pagefile} has max and initial disk size values set")
          powershell_code = <<-CODE
            $page_file = '#{pagefile}';
            $driveLetter = $page_file.split(':')[0];
            $page_file_settings = Get-CimInstance -ClassName Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveLetter):'" -Property * -ErrorAction Stop;
            if ($page_file_settings.InitialSize -eq #{min} -and $page_file_settings.MaximumSize -eq #{max})
              { return $true }
            else
              { return $false }
          CODE
          powershell_exec!(powershell_code).result
        end
pagefile_drive_exist?(pagefile) click to toggle source

raise an exception if the target drive location is invalid

# File lib/chef/resource/windows_pagefile.rb, line 138
def pagefile_drive_exist?(pagefile)
  if ::Dir.exist?(pagefile[0] + ":\\") == false
    raise "You are trying to create a pagefile on a drive that does not exist!"
  end
end
set_automatic_managed() click to toggle source

turn on automatic management of all pagefiles by Windows

# File lib/chef/resource/windows_pagefile.rb, line 220
        def set_automatic_managed
          converge_by("Set pagefile to Automatic Managed") do
            logger.trace("Running Set-CimInstance -InputObject $sys -Property @{AutomaticManagedPagefile=$true} -PassThru")
            powershell_exec! <<~EOH
              $sys = Get-CimInstance Win32_ComputerSystem -Property *
              Set-CimInstance -InputObject $sys -Property @{AutomaticManagedPagefile=$true} -PassThru
            EOH
          end
        end
set_custom_size(pagefile, min, max) click to toggle source

set a custom size for the pagefile (vs the defaults)

@param [String] pagefile path to the pagefile @param [String] min the minimum size of the pagefile @param [String] max the minimum size of the pagefile

# File lib/chef/resource/windows_pagefile.rb, line 248
        def set_custom_size(pagefile, min, max)
          unset_automatic_managed
          converge_by("set #{pagefile} to InitialSize=#{min} & MaximumSize=#{max}") do
            logger.trace("Set-CimInstance -Property @{InitialSize = #{min} MaximumSize = #{max}")
            powershell_exec! <<~EOD
              $page_file = "#{pagefile}"
              $driveLetter = $page_file.split(':')[0]
              Get-CimInstance -ClassName Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveLetter):'" -ErrorAction Stop | Set-CimInstance -Property @{InitialSize = #{min}; MaximumSize = #{max};}
            EOD
          end
        end
set_system_managed(pagefile) click to toggle source

set a pagefile size to be system managed

@param [String] pagefile path to the pagefile

# File lib/chef/resource/windows_pagefile.rb, line 263
        def set_system_managed(pagefile)
          converge_by("set #{pagefile} to System Managed") do
            logger.trace("Running ")
            powershell_exec! <<~EOM
              $page_file = "#{pagefile}"
              $driveLetter = $page_file.split(':')[0]
              Get-CimInstance -ClassName Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveLetter):'" -ErrorAction Stop | Set-CimInstance -Property @{
              InitialSize = 0
              MaximumSize = 0}
            EOM
          end
        end
unset_automatic_managed() click to toggle source

turn off automatic management of all pagefiles by Windows

# File lib/chef/resource/windows_pagefile.rb, line 231
        def unset_automatic_managed
          if automatic_managed?
            converge_by("Turn off Automatically Managed on pagefiles") do
              logger.trace("Running Set-CimInstance -InputObject $sys -Property @{AutomaticManagedPagefile=$false} -PassThru")
              powershell_exec! <<~EOH
                $sys = Get-CimInstance Win32_ComputerSystem -Property *
                Set-CimInstance -InputObject $sys -Property @{AutomaticManagedPagefile=$false} -PassThru
              EOH
            end
          end
        end