Skip to content

Instantly share code, notes, and snippets.

@natemccurdy
Last active February 8, 2021 16:46
Show Gist options
  • Save natemccurdy/3c04f1583b4bb1acc257 to your computer and use it in GitHub Desktop.
Save natemccurdy/3c04f1583b4bb1acc257 to your computer and use it in GitHub Desktop.
Windows Pagefile management with Puppet
#
# win_pagefile/manifests/init.pp
#
# Move pagefile to D: drive, reboot.
class win_pagefile (
$pagefile_drive = 'd',
) {
if $::pagefile_location =~ '[Cc]\:' or $::pagefile_automanaged == true {
exec { 'pagefile':
command => template('win_pagefile/set_pagefile.ps1.erb'),
provider => powershell,
logoutput => false,
}
reboot { 'afterpagefile':
apply => immediately,
timeout => 5,
subscribe => Exec['pagefile'],
}
}
}
#
# win_pagefile/lib/facter/pagefile_automanaged.rb
#
Facter.add('pagefile_automanaged') do
confine :osfamily => "windows"
setcode do
powershell = 'C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe'
command = '(Get-WmiObject Win32_computersystem -EnableAllPrivileges).AutomaticManagedPagefile'
value = Facter::Util::Resolution.exec(%Q{#{powershell} -command "#{command}"})
if value == "True"
true
else
false
end
end
end
#
# win_pagefile/lib/facter/pagefile_location.rb
#
Facter.add('pagefile_location') do
confine :osfamily => "windows"
setcode do
powershell = 'C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe'
command = '(Get-WmiObject Win32_pagefilesetting -ComputerName .).name'
Facter::Util::Resolution.exec(%Q{#{powershell} -command "#{command}"})
end
end
#
# win_pagefile/templates/set_pagefile.ps1.erb
#
$computer = Get-WmiObject Win32_computersystem -EnableAllPrivileges
$computer.AutomaticManagedPagefile = $false
$computer.Put()
$CurrentPageFile = Get-WmiObject -Query "select * from Win32_PageFileSetting where name='c:\\pagefile.sys'"
$CurrentPageFile.delete()
Set-WMIInstance -Class Win32_PageFileSetting -Arguments @{name="<%= @pagefile_drive %>:\pagefile.sys";InitialSize = 0; MaximumSize = 0}
@natemccurdy
Copy link
Author

win_pagefile
├── lib/
│   └── facter/
│       ├── pagefile_automanaged.rb
│       └── pagefile_location.rb
├── manifests/
│   └── init.pp
└── templates/
    └── set_pagefile.ps1.erb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment