| 1 |
#!powershell |
| 2 |
|
| 3 |
# Copyright: (c) 2017, Liran Nisanov <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
#Requires -Module Ansible.ModuleUtils.Legacy |
| 7 |
|
| 8 |
######## |
| 9 |
|
| 10 |
Function Remove-Pagefile { |
| 11 |
[CmdletBinding(SupportsShouldProcess)] |
| 12 |
param( |
| 13 |
$path |
| 14 |
) |
| 15 |
Get-CIMInstance Win32_PageFileSetting | Where-Object { $_.Name -eq $path } | ForEach-Object { |
| 16 |
if ($PSCmdlet.ShouldProcess($Path, "remove pagefile")) { |
| 17 |
$_ | Remove-CIMInstance |
| 18 |
} |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
Function Get-Pagefile($path) { |
| 23 |
Get-CIMInstance Win32_PageFileSetting | Where-Object { $_.Name -eq $path } |
| 24 |
} |
| 25 |
|
| 26 |
######## |
| 27 |
|
| 28 |
$params = Parse-Args $args -supports_check_mode $true |
| 29 |
$check_mode = Get-AnsibleParam -obj $params -name '_ansible_check_mode' -type 'bool' -default $false |
| 30 |
|
| 31 |
$automatic = Get-AnsibleParam -obj $params -name "automatic" -type "bool" |
| 32 |
$drive = Get-AnsibleParam -obj $params -name "drive" -type "str" |
| 33 |
$fullPath = $drive + ":\pagefile.sys" |
| 34 |
$initialSize = Get-AnsibleParam -obj $params -name "initial_size" -type "int" |
| 35 |
$maximumSize = Get-AnsibleParam -obj $params -name "maximum_size" -type "int" |
| 36 |
$override = Get-AnsibleParam -obj $params -name "override" -type "bool" -default $true |
| 37 |
$removeAll = Get-AnsibleParam -obj $params -name "remove_all" -type "bool" -default $false |
| 38 |
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "query" -validateset "present", "absent", "query" |
| 39 |
$systemManaged = Get-AnsibleParam -obj $params -name "system_managed" -type "bool" -default $false |
| 40 |
$test_path = Get-AnsibleParam -obj $params -name "test_path" -type "bool" -default $true |
| 41 |
|
| 42 |
$result = @{ |
| 43 |
changed = $false |
| 44 |
} |
| 45 |
|
| 46 |
if ($removeAll) { |
| 47 |
$currentPageFiles = Get-CIMInstance Win32_PageFileSetting |
| 48 |
if ($null -ne $currentPageFiles) { |
| 49 |
$currentPageFiles | Remove-CIMInstance -WhatIf:$check_mode > $null |
| 50 |
$result.changed = $true |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
$regPath = "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management" |
| 55 |
|
| 56 |
if ($null -ne $automatic) { |
| 57 |
# change autmoatic managed pagefile |
| 58 |
try { |
| 59 |
$computerSystem = Get-CIMInstance -Class win32_computersystem |
| 60 |
} |
| 61 |
catch { |
| 62 |
Fail-Json $result "Failed to query WMI computer system object $($_.Exception.Message)" |
| 63 |
} |
| 64 |
if ($computerSystem.AutomaticManagedPagefile -ne $automatic) { |
| 65 |
if (-not $check_mode) { |
| 66 |
try { |
| 67 |
$computerSystem | Set-CimInstance -Property @{automaticmanagedpagefile = "$automatic" } > $null |
| 68 |
} |
| 69 |
catch { |
| 70 |
Fail-Json $result "Failed to set AutomaticManagedPagefile $($_.Exception.Message)" |
| 71 |
} |
| 72 |
} |
| 73 |
$result.changed = $true |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
if ($state -eq "absent") { |
| 78 |
# Remove pagefile |
| 79 |
if ($null -ne (Get-Pagefile $fullPath)) { |
| 80 |
try { |
| 81 |
Remove-Pagefile $fullPath -whatif:$check_mode |
| 82 |
} |
| 83 |
catch { |
| 84 |
Fail-Json $result "Failed to remove pagefile $($_.Exception.Message)" |
| 85 |
} |
| 86 |
$result.changed = $true |
| 87 |
} |
| 88 |
} |
| 89 |
elseif ($state -eq "present") { |
| 90 |
# Remove current pagefile |
| 91 |
if ($override) { |
| 92 |
if ($null -ne (Get-Pagefile $fullPath)) { |
| 93 |
try { |
| 94 |
Remove-Pagefile $fullPath -whatif:$check_mode |
| 95 |
} |
| 96 |
catch { |
| 97 |
Fail-Json $result "Failed to remove current pagefile $($_.Exception.Message)" |
| 98 |
} |
| 99 |
$result.changed = $true |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
# Make sure drive is accessible |
| 104 |
if (($test_path) -and (-not (Test-Path -LiteralPath "${drive}:"))) { |
| 105 |
Fail-Json $result "Unable to access '${drive}:' drive" |
| 106 |
} |
| 107 |
|
| 108 |
$curPagefile = Get-Pagefile $fullPath |
| 109 |
|
| 110 |
# Set pagefile |
| 111 |
if ($null -eq $curPagefile) { |
| 112 |
try { |
| 113 |
$pagefile = New-CIMInstance -Class Win32_PageFileSetting -Arguments @{name = $fullPath; } -WhatIf:$check_mode |
| 114 |
} |
| 115 |
catch { |
| 116 |
Fail-Json $result "Failed to create pagefile $($_.Exception.Message)" |
| 117 |
} |
| 118 |
if (-not ($systemManaged -or $check_mode)) { |
| 119 |
try { |
| 120 |
$pagefile | Set-CimInstance -Property @{ InitialSize = $initialSize; MaximumSize = $maximumSize } |
| 121 |
} |
| 122 |
catch { |
| 123 |
$originalExceptionMessage = $($_.Exception.Message) |
| 124 |
# Try workaround before failing |
| 125 |
try { |
| 126 |
Remove-Pagefile $fullPath -whatif:$check_mode |
| 127 |
} |
| 128 |
catch { |
| 129 |
Fail-Json $result "Failed to remove pagefile before workaround $($_.Exception.Message) Original exception: $originalExceptionMessage" |
| 130 |
} |
| 131 |
try { |
| 132 |
$pagingFilesValues = (Get-ItemProperty -LiteralPath $regPath).PagingFiles |
| 133 |
} |
| 134 |
catch { |
| 135 |
$msg = -join @( |
| 136 |
"Failed to get pagefile settings from the registry for workaround $($_.Exception.Message) " |
| 137 |
"Original exception: $originalExceptionMessage" |
| 138 |
) |
| 139 |
Fail-Json $result $msg |
| 140 |
} |
| 141 |
$pagingFilesValues += "$fullPath $initialSize $maximumSize" |
| 142 |
try { |
| 143 |
Set-ItemProperty -LiteralPath $regPath "PagingFiles" $pagingFilesValues |
| 144 |
} |
| 145 |
catch { |
| 146 |
$msg = -join @( |
| 147 |
"Failed to set pagefile settings to the registry for workaround $($_.Exception.Message) " |
| 148 |
"Original exception: $originalExceptionMessage" |
| 149 |
) |
| 150 |
Fail-Json $result $msg |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
$result.changed = $true |
| 155 |
} |
| 156 |
else { |
| 157 |
if ((-not $check_mode) -and |
| 158 |
-not ($systemManaged) -and |
| 159 |
-not ( ($curPagefile.InitialSize -eq 0) -and ($curPagefile.maximumSize -eq 0) ) -and |
| 160 |
( ($curPagefile.InitialSize -ne $initialSize) -or ($curPagefile.maximumSize -ne $maximumSize) ) |
| 161 |
) { |
| 162 |
$curPagefile.InitialSize = $initialSize |
| 163 |
$curPagefile.MaximumSize = $maximumSize |
| 164 |
try { |
| 165 |
$curPagefile.Put() | out-null |
| 166 |
} |
| 167 |
catch { |
| 168 |
$originalExceptionMessage = $($_.Exception.Message) |
| 169 |
# Try workaround before failing |
| 170 |
try { |
| 171 |
Remove-Pagefile $fullPath -whatif:$check_mode |
| 172 |
} |
| 173 |
catch { |
| 174 |
Fail-Json $result "Failed to remove pagefile before workaround $($_.Exception.Message) Original exception: $originalExceptionMessage" |
| 175 |
} |
| 176 |
try { |
| 177 |
$pagingFilesValues = (Get-ItemProperty -LiteralPath $regPath).PagingFiles |
| 178 |
} |
| 179 |
catch { |
| 180 |
$msg = -join @( |
| 181 |
"Failed to get pagefile settings from the registry for workaround $($_.Exception.Message) " |
| 182 |
"Original exception: $originalExceptionMessage" |
| 183 |
) |
| 184 |
Fail-Json $result $msg |
| 185 |
} |
| 186 |
$pagingFilesValues += "$fullPath $initialSize $maximumSize" |
| 187 |
try { |
| 188 |
|
| 189 |
Set-ItemProperty -LiteralPath $regPath -Name "PagingFiles" -Value $pagingFilesValues |
| 190 |
} |
| 191 |
catch { |
| 192 |
$msg = -join @( |
| 193 |
"Failed to set pagefile settings to the registry for workaround $($_.Exception.Message) " |
| 194 |
"Original exception: $originalExceptionMessage" |
| 195 |
) |
| 196 |
Fail-Json $result $msg |
| 197 |
} |
| 198 |
} |
| 199 |
$result.changed = $true |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
elseif ($state -eq "query") { |
| 204 |
$result.pagefiles = @() |
| 205 |
|
| 206 |
if ($null -eq $drive) { |
| 207 |
try { |
| 208 |
$pagefiles = Get-CIMInstance Win32_PageFileSetting |
| 209 |
} |
| 210 |
catch { |
| 211 |
Fail-Json $result "Failed to query all pagefiles $($_.Exception.Message)" |
| 212 |
} |
| 213 |
} |
| 214 |
else { |
| 215 |
try { |
| 216 |
$pagefiles = Get-Pagefile $fullPath |
| 217 |
} |
| 218 |
catch { |
| 219 |
Fail-Json $result "Failed to query specific pagefile $($_.Exception.Message)" |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
# Get all pagefiles |
| 224 |
foreach ($currentPagefile in $pagefiles) { |
| 225 |
$currentPagefileObject = @{ |
| 226 |
name = $currentPagefile.Name |
| 227 |
initial_size = $currentPagefile.InitialSize |
| 228 |
maximum_size = $currentPagefile.MaximumSize |
| 229 |
caption = $currentPagefile.Caption |
| 230 |
description = $currentPagefile.Description |
| 231 |
} |
| 232 |
$result.pagefiles += , $currentPagefileObject |
| 233 |
} |
| 234 |
|
| 235 |
# Get automatic managed pagefile state |
| 236 |
try { |
| 237 |
$result.automatic_managed_pagefiles = (Get-CIMInstance -Class win32_computersystem).AutomaticManagedPagefile |
| 238 |
} |
| 239 |
catch { |
| 240 |
Fail-Json $result "Failed to query automatic managed pagefile state $($_.Exception.Message)" |
| 241 |
} |
| 242 |
} |
| 243 |
Exit-Json $result |