Skip to content

Commit a554214

Browse files
authored
Merge pull request #64 from nyanhp/feature/xVirtualMemory
Added new DSC resource xVirtualMemory
2 parents 7adf6e2 + b14817a commit a554214

8 files changed

Lines changed: 815 additions & 1 deletion
Lines changed: 385 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,385 @@
1+
<#
2+
.SYNOPSIS
3+
Returns the current state of the virtual memory configuration
4+
.PARAMETER Drive
5+
The drive for which the virtual memory configuration needs to be returned
6+
.PARAMETER Type
7+
The type of the virtual memory configuration
8+
#>
9+
function Get-TargetResource
10+
{
11+
[CmdletBinding()]
12+
[OutputType([System.Collections.Hashtable])]
13+
param
14+
(
15+
[parameter(Mandatory = $true)]
16+
[System.String]
17+
$Drive,
18+
19+
[ValidateSet("AutoManagePagingFile", "CustomSize", "SystemManagedSize", "NoPagingFile")]
20+
[parameter(Mandatory = $true)]
21+
[System.String]
22+
$Type
23+
)
24+
25+
Write-Verbose -Message 'Getting current page file settings'
26+
27+
$returnValue = @{
28+
Drive = [string]::Empty
29+
Type = [string]::Empty
30+
InitialSize = 0
31+
MaximumSize = 0
32+
}
33+
34+
[bool] $isSystemManaged = (Get-CimInstance -ClassName Win32_ComputerSystem).AutomaticManagedPagefile
35+
36+
if ($isSystemManaged)
37+
{
38+
$returnValue.Type = 'AutoManagePagingFile'
39+
return $returnValue
40+
}
41+
42+
$driveItem = [System.IO.DriveInfo] $Drive
43+
44+
Write-Verbose -Message "Pagefile was not automatically managed. Retrieving detailed page file settings with query Select * from Win32_PageFileSetting where SettingID='pagefile.sys @ $($driveItem.Name.Substring(0,2))'"
45+
46+
# Find existing page file settings by drive letter
47+
$virtualMemoryInstance = Get-CimInstance -Namespace root\cimv2 -Query "Select * from Win32_PageFileSetting where SettingID='pagefile.sys @ $($driveItem.Name.Substring(0,2))'"
48+
49+
if (-not $virtualMemoryInstance)
50+
{
51+
$returnValue.Type = 'NoPagingFile'
52+
return $returnValue
53+
}
54+
55+
if ($virtualMemoryInstance.InitialSize -eq 0 -and $virtualMemoryInstance.MaximumSize -eq 0)
56+
{
57+
$returnValue.Type = 'SystemManagedSize'
58+
}
59+
else
60+
{
61+
$returnValue.Type = "CustomSize"
62+
}
63+
64+
$returnValue.Drive = $virtualMemoryInstance.Name.Substring(0, 3)
65+
$returnValue.InitialSize = $virtualMemoryInstance.InitialSize
66+
$returnValue.MaximumSize = $virtualMemoryInstance.MaximumSize
67+
68+
$returnValue
69+
}
70+
71+
<#
72+
.SYNOPSIS
73+
Sets the virtual memory settings based on the parameters supplied
74+
.PARAMETER Drive
75+
The drive for which the virtual memory configuration should be set.
76+
.PARAMETER Type
77+
The paging type. When set to AutoManagePagingFile, drive letters are ignored
78+
.PARAMETER InitialSize
79+
The initial page file size in megabyte
80+
.PARAMETER MaximumSize
81+
The maximum page file size in megabyte. May not be smaller than InitialSize
82+
#>
83+
function Set-TargetResource
84+
{
85+
[CmdletBinding()]
86+
param
87+
(
88+
[parameter(Mandatory = $true)]
89+
[System.String]
90+
$Drive,
91+
92+
[ValidateSet("AutoManagePagingFile", "CustomSize", "SystemManagedSize", "NoPagingFile")]
93+
[parameter(Mandatory = $true)]
94+
[System.String]
95+
$Type,
96+
97+
[System.Int64]
98+
$InitialSize,
99+
100+
[System.Int64]
101+
$MaximumSize
102+
)
103+
104+
Write-Verbose -Message 'Setting page file'
105+
106+
$SystemInfo = Get-CimInstance -Class Win32_ComputerSystem
107+
108+
switch ($Type)
109+
{
110+
"AutoManagePagingFile"
111+
{
112+
$setParams = @{
113+
Namespace = 'root\cimv2'
114+
Query = 'Select * from Win32_ComputerSystem'
115+
Property = @{AutomaticManagedPageFile = $true}
116+
}
117+
118+
Write-Verbose -Message 'Enabling AutoManagePagingFile'
119+
120+
Set-CimInstance @setParams
121+
$global:DSCMachineStatus = 1
122+
break
123+
}
124+
"CustomSize"
125+
{
126+
if ($SystemInfo.AutomaticManagedPageFile)
127+
{
128+
# First set AutomaticManagedPageFile to $false to be able to set a custom one later
129+
130+
$setParams = @{
131+
Namespace = 'root\cimv2'
132+
Query = 'Select * from Win32_ComputerSystem'
133+
Property = @{AutomaticManagedPageFile = $false}
134+
}
135+
136+
Write-Verbose -Message 'Disabling AutoManagePagingFile'
137+
138+
Set-CimInstance @setParams
139+
}
140+
141+
$driveInfo = [System.IO.DriveInfo] $Drive
142+
if (-not $driveInfo.IsReady)
143+
{
144+
throw "Drive $($driveInfo.Name) is not ready. Please ensure that the drive exists and is available"
145+
}
146+
147+
$pageFileName = Join-Path -Path $driveInfo.Name -ChildPath 'pagefile.sys'
148+
149+
Write-Verbose -Message ('Checking if a paging file already exists at {0}' -f $pageFileName)
150+
$existingPageFileSetting = Get-CimInstance -Namespace root\cimv2 -Query "Select * from Win32_PageFileSetting where SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'"
151+
if (-not $existingPageFileSetting)
152+
{
153+
[void] (New-CimInstance -Namespace 'root\cimv2' -ClassName 'Win32_PageFileSetting' -Property @{Name = $pageFileName})
154+
}
155+
156+
<#
157+
New-CimInstance does not support properties InitialSize and MaximumSize. Therefore, create
158+
a New-CimInstance with the page file name only if it does not exist and Set-CimInstance on the instance
159+
#>
160+
$setParams = @{
161+
Namespace = 'root\cimv2'
162+
Query = "Select * from Win32_PageFileSetting where SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'"
163+
Property = @{
164+
InitialSize = $InitialSize
165+
MaximumSize = $MaximumSize
166+
}
167+
}
168+
169+
Write-Verbose -Message ("Setting page file to {0}. Initial size {1}MB, maximum size {2}MB" -f $pageFileName, $InitialSize, $MaximumSize)
170+
171+
Set-CimInstance @setParams
172+
$global:DSCMachineStatus = 1
173+
break
174+
}
175+
"SystemManagedSize"
176+
{
177+
if ($SystemInfo.AutomaticManagedPageFile)
178+
{
179+
$setParams = @{
180+
Namespace = 'root\cimv2'
181+
Query = 'Select * from Win32_ComputerSystem'
182+
Property = @{AutomaticManagedPageFile = $false}
183+
}
184+
185+
Write-Verbose -Message 'Disabling AutoManagePagingFile'
186+
187+
Set-CimInstance @setParams
188+
}
189+
190+
$driveInfo = [System.IO.DriveInfo] $Drive
191+
if (-not $driveInfo.IsReady)
192+
{
193+
throw "Drive $($driveInfo.Name) is not ready. Please ensure that the drive exists and is available"
194+
}
195+
196+
$pageFileName = Join-Path -Path $driveInfo.Name -ChildPath 'pagefile.sys'
197+
198+
Write-Verbose -Message ('Checking if a paging file already exists at {0}' -f $pageFileName)
199+
200+
$existingPageFileSetting = Get-CimInstance -Namespace root\cimv2 -Query "Select * from Win32_PageFileSetting where SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'"
201+
if (-not $existingPageFileSetting)
202+
{
203+
[void] (New-CimInstance -Namespace 'root\cimv2' -ClassName 'Win32_PageFileSetting' -Property @{Name = $pageFileName})
204+
}
205+
206+
207+
$setParams = @{
208+
Namespace = 'root\cimv2'
209+
Query = "Select * from Win32_PageFileSetting where SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'"
210+
Property = @{
211+
InitialSize = 0
212+
MaximumSize = 0
213+
}
214+
}
215+
216+
Write-Verbose -Message "Enabling system-managed page file on $pageFileName"
217+
218+
Set-CimInstance @setParams
219+
$global:DSCMachineStatus = 1
220+
break
221+
}
222+
"NoPagingFile"
223+
{
224+
if ($SystemInfo.AutomaticManagedPageFile)
225+
{
226+
$setParams = @{
227+
Namespace = 'root\cimv2'
228+
Query = 'Select * from Win32_ComputerSystem'
229+
Property = @{AutomaticManagedPageFile = $false}
230+
}
231+
232+
Set-CimInstance @setParams
233+
}
234+
235+
$driveInfo = [System.IO.DriveInfo] $Drive
236+
if (-not $driveInfo.IsReady)
237+
{
238+
throw "Drive $($driveInfo.Name) is not ready. Please ensure that the drive exists and is available"
239+
}
240+
241+
$PageFile = Get-CimInstance -Class Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'"
242+
243+
$existingPageFileSetting = Get-CimInstance -Namespace root\cimv2 -Query "Select * from Win32_PageFileSetting where SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'"
244+
if ($existingPageFileSetting)
245+
{
246+
Write-Verbose -Message "Removing existing page file $($existingPageFileSetting.Name)"
247+
Remove-CimInstance -InputObject $existingPageFileSetting
248+
$global:DSCMachineStatus = 1
249+
}
250+
251+
Write-Verbose -Message "Disabled page file for drive $Drive"
252+
253+
break
254+
}
255+
default
256+
{
257+
throw "A wrong type '$Type' has been selected."
258+
}
259+
}
260+
}
261+
262+
<#
263+
.SYNOPSIS
264+
Tests if virtual memory settings need to be applied based on the parameters supplied
265+
.PARAMETER Drive
266+
The drive letter that should be tested
267+
.PARAMETER Type
268+
The type of the virtual memory configuration
269+
.PARAMETER InitialSize
270+
The initial page file size in megabyte
271+
.PARAMETER MaximumSize
272+
The maximum page file size in megabyte
273+
#>
274+
function Test-TargetResource
275+
{
276+
[CmdletBinding()]
277+
[OutputType([System.Boolean])]
278+
param
279+
(
280+
[parameter(Mandatory = $true)]
281+
[System.String]
282+
$Drive,
283+
284+
[ValidateSet("AutoManagePagingFile", "CustomSize", "SystemManagedSize", "NoPagingFile")]
285+
[parameter(Mandatory = $true)]
286+
[System.String]
287+
$Type,
288+
289+
[System.Int64]
290+
$InitialSize,
291+
292+
[System.Int64]
293+
$MaximumSize
294+
)
295+
296+
$SystemInfo = Get-CimInstance -Class Win32_ComputerSystem
297+
$result = $false
298+
299+
switch ($Type)
300+
{
301+
"AutoManagePagingFile"
302+
{
303+
$result = $SystemInfo.AutomaticManagedPagefile
304+
break
305+
}
306+
"CustomSize"
307+
{
308+
if ($SystemInfo.AutomaticManagedPageFile)
309+
{
310+
$result = $false
311+
break
312+
}
313+
314+
$driveInfo = [System.IO.DriveInfo] $Drive
315+
$PageFile = Get-CimInstance -Class Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'"
316+
if (-not $PageFile)
317+
{
318+
$result = $false
319+
break
320+
}
321+
322+
if (-not ($PageFile.InitialSize -eq $InitialSize -and $PageFile.MaximumSize -eq $MaximumSize))
323+
{
324+
$result = $false
325+
break
326+
}
327+
328+
$result = $true
329+
break
330+
}
331+
"SystemManagedSize"
332+
{
333+
if ($SystemInfo.AutomaticManagedPageFile)
334+
{
335+
$result = $false
336+
break
337+
}
338+
339+
$driveInfo = [System.IO.DriveInfo] $Drive
340+
$PageFile = Get-CimInstance -Class Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'"
341+
if (-not $PageFile)
342+
{
343+
$result = $false
344+
break
345+
}
346+
347+
if (-not ($PageFile.InitialSize -eq 0 -and $PageFile.MaximumSize -eq 0))
348+
{
349+
$result = $false
350+
break
351+
}
352+
353+
$result = $true
354+
break
355+
}
356+
"NoPagingFile"
357+
{
358+
if ($SystemInfo.AutomaticManagedPageFile)
359+
{
360+
$result = $false
361+
break
362+
}
363+
364+
$driveInfo = [System.IO.DriveInfo] $Drive
365+
$PageFile = Get-CimInstance -Class Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'"
366+
367+
if ($PageFile)
368+
{
369+
$result = $false
370+
break
371+
}
372+
373+
$result = $true
374+
break
375+
}
376+
default
377+
{
378+
break
379+
}
380+
}
381+
382+
$result
383+
}
384+
385+
Export-ModuleMember -Function *-TargetResource
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
[ClassVersion("1.0.0.0"), FriendlyName("xVirtualMemory")]
3+
class MSFT_xVirtualMemory : OMI_BaseResource
4+
{
5+
[Key, Description("The drive letter for which paging settings should be set. Can be letter only, letter and colon or letter with colon and trailing slash.")] String Drive;
6+
[Key, Description("The type of the paging setting to use. If set to AutoManagePagingFile, the drive letter will be ignored. If set to SystemManagedSize, the values for InitialSize and MaximumSize will be ignored"), ValueMap{"AutoManagePagingFile","CustomSize","SystemManagedSize","NoPagingFile"}, Values{"AutoManagePagingFile","CustomSize","SystemManagedSize","NoPagingFile"}] String Type;
7+
[Write, Description("The initial size of the page file in Megabyte")] Sint64 InitialSize;
8+
[Write, Description("The maximum size of the page file in Megabyte")] Sint64 MaximumSize;
9+
};
10+

0 commit comments

Comments
 (0)