Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
3e2c012
Initial check in
nyanhp Feb 19, 2017
b19cb5b
Added functionality to Set, Get and Test
nyanhp Feb 21, 2017
72d3359
File conversion (UTF8 and ASCII)
nyanhp Feb 21, 2017
9d8e236
Set Type as mandatory (because it should be) and removed irrelevant p…
nyanhp Feb 21, 2017
4166b86
Started adding tests
nyanhp Feb 21, 2017
5a44ae3
Added drive to return values in Get-TargetResource
nyanhp Feb 21, 2017
ef43e61
Module modified to squash a few bugs
nyanhp Feb 27, 2017
5400034
Updated friendly name in schema
nyanhp Feb 27, 2017
8874086
Updated all unit tests
nyanhp Feb 27, 2017
76d1806
Fixed set-targetresource for SystemManagedPageFile
nyanhp Feb 27, 2017
37a9131
Added integration tests for all scenarios
nyanhp Feb 27, 2017
d843364
Updated README.md
nyanhp Feb 27, 2017
65d12e5
Updated version in xComputerManagement.psd1
nyanhp Feb 27, 2017
a0b1085
Version comments put into wrong section
nyanhp Feb 27, 2017
f58e79e
Resource module style
nyanhp Feb 27, 2017
60348ba
Further styling, added comments where necessary
nyanhp Feb 27, 2017
203e1bc
Issue in string format (could not count to 2) fixed
nyanhp Feb 27, 2017
9410687
Removed stray comment from unit test
nyanhp Feb 27, 2017
7ceda00
Added sample script
nyanhp Feb 27, 2017
c3d335a
Missing newlines added to sample and config data
nyanhp Feb 28, 2017
2459fdd
Test case for virtual memory unit test modified
nyanhp Feb 28, 2017
2fcdc65
Had to modify MSFT_xComputer.Tests.ps1 because my tests were failing.…
nyanhp Feb 28, 2017
b4ed149
MSFT_xComputer.Tests -->last commit did not honor the alphabet...
nyanhp Feb 28, 2017
368c340
xVirtualMemory.psm1 modified to incorporate style issues (curly brace…
nyanhp Mar 1, 2017
15c45ac
Added descriptions to all resource properties
nyanhp Mar 1, 2017
23e7c48
Styling changes in README.md implemented
nyanhp Mar 1, 2017
e96f633
Styling issues in MSFT_xVirtualMemory.Integration.Tests fixed
nyanhp Mar 1, 2017
4edceda
Styling issues in MSFT_xVirtualMemory.Tests.ps1 fixed
nyanhp Mar 1, 2017
67b96b3
AppVeyor test for Exceptions was failing. Moved curly braces one line up
nyanhp Mar 1, 2017
46f7b68
Formatting
nyanhp Mar 22, 2017
4ea3cb5
Formatting
nyanhp Mar 22, 2017
b14817a
Added comment-based help entries for Get/Set/Test
nyanhp Apr 21, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
385 changes: 385 additions & 0 deletions DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,385 @@
<#
.SYNOPSIS
Returns the current state of the virtual memory configuration
.PARAMETER Drive
The drive for which the virtual memory configuration needs to be returned
.PARAMETER Type
The type of the virtual memory configuration
#>
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[parameter(Mandatory = $true)]
[System.String]
$Drive,

[ValidateSet("AutoManagePagingFile", "CustomSize", "SystemManagedSize", "NoPagingFile")]
[parameter(Mandatory = $true)]
[System.String]
$Type
)

Write-Verbose -Message 'Getting current page file settings'

$returnValue = @{
Drive = [string]::Empty
Type = [string]::Empty
InitialSize = 0
MaximumSize = 0
}

[bool] $isSystemManaged = (Get-CimInstance -ClassName Win32_ComputerSystem).AutomaticManagedPagefile

if ($isSystemManaged)
{
$returnValue.Type = 'AutoManagePagingFile'
return $returnValue
}

$driveItem = [System.IO.DriveInfo] $Drive

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))'"

# Find existing page file settings by drive letter
$virtualMemoryInstance = Get-CimInstance -Namespace root\cimv2 -Query "Select * from Win32_PageFileSetting where SettingID='pagefile.sys @ $($driveItem.Name.Substring(0,2))'"

if (-not $virtualMemoryInstance)
{
$returnValue.Type = 'NoPagingFile'
return $returnValue
}

if ($virtualMemoryInstance.InitialSize -eq 0 -and $virtualMemoryInstance.MaximumSize -eq 0)
{
$returnValue.Type = 'SystemManagedSize'
}
else
{
$returnValue.Type = "CustomSize"
}

$returnValue.Drive = $virtualMemoryInstance.Name.Substring(0, 3)
$returnValue.InitialSize = $virtualMemoryInstance.InitialSize
$returnValue.MaximumSize = $virtualMemoryInstance.MaximumSize

$returnValue
}

<#
.SYNOPSIS
Sets the virtual memory settings based on the parameters supplied
.PARAMETER Drive
The drive for which the virtual memory configuration should be set.
.PARAMETER Type
The paging type. When set to AutoManagePagingFile, drive letters are ignored
.PARAMETER InitialSize
The initial page file size in megabyte
.PARAMETER MaximumSize
The maximum page file size in megabyte. May not be smaller than InitialSize
#>
function Set-TargetResource
{
[CmdletBinding()]
param
(
[parameter(Mandatory = $true)]
[System.String]
$Drive,

[ValidateSet("AutoManagePagingFile", "CustomSize", "SystemManagedSize", "NoPagingFile")]
[parameter(Mandatory = $true)]
[System.String]
$Type,

[System.Int64]
$InitialSize,

[System.Int64]
$MaximumSize
)

Write-Verbose -Message 'Setting page file'

$SystemInfo = Get-CimInstance -Class Win32_ComputerSystem

switch ($Type)
{
"AutoManagePagingFile"
{
$setParams = @{
Namespace = 'root\cimv2'
Query = 'Select * from Win32_ComputerSystem'
Property = @{AutomaticManagedPageFile = $true}
}

Write-Verbose -Message 'Enabling AutoManagePagingFile'

Set-CimInstance @setParams
$global:DSCMachineStatus = 1
break
}
"CustomSize"
{
if ($SystemInfo.AutomaticManagedPageFile)
{
# First set AutomaticManagedPageFile to $false to be able to set a custom one later

$setParams = @{
Namespace = 'root\cimv2'
Query = 'Select * from Win32_ComputerSystem'
Property = @{AutomaticManagedPageFile = $false}
}

Write-Verbose -Message 'Disabling AutoManagePagingFile'

Set-CimInstance @setParams
}

$driveInfo = [System.IO.DriveInfo] $Drive
if (-not $driveInfo.IsReady)
{
throw "Drive $($driveInfo.Name) is not ready. Please ensure that the drive exists and is available"
}

$pageFileName = Join-Path -Path $driveInfo.Name -ChildPath 'pagefile.sys'

Write-Verbose -Message ('Checking if a paging file already exists at {0}' -f $pageFileName)
$existingPageFileSetting = Get-CimInstance -Namespace root\cimv2 -Query "Select * from Win32_PageFileSetting where SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'"
if (-not $existingPageFileSetting)
{
[void] (New-CimInstance -Namespace 'root\cimv2' -ClassName 'Win32_PageFileSetting' -Property @{Name = $pageFileName})
}

<#
New-CimInstance does not support properties InitialSize and MaximumSize. Therefore, create
a New-CimInstance with the page file name only if it does not exist and Set-CimInstance on the instance
#>
$setParams = @{
Namespace = 'root\cimv2'
Query = "Select * from Win32_PageFileSetting where SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'"
Property = @{
InitialSize = $InitialSize
MaximumSize = $MaximumSize
}
}

Write-Verbose -Message ("Setting page file to {0}. Initial size {1}MB, maximum size {2}MB" -f $pageFileName, $InitialSize, $MaximumSize)

Set-CimInstance @setParams
$global:DSCMachineStatus = 1
break
}
"SystemManagedSize"
{
if ($SystemInfo.AutomaticManagedPageFile)
{
$setParams = @{
Namespace = 'root\cimv2'
Query = 'Select * from Win32_ComputerSystem'
Property = @{AutomaticManagedPageFile = $false}
}

Write-Verbose -Message 'Disabling AutoManagePagingFile'

Set-CimInstance @setParams
}

$driveInfo = [System.IO.DriveInfo] $Drive
if (-not $driveInfo.IsReady)
{
throw "Drive $($driveInfo.Name) is not ready. Please ensure that the drive exists and is available"
}

$pageFileName = Join-Path -Path $driveInfo.Name -ChildPath 'pagefile.sys'

Write-Verbose -Message ('Checking if a paging file already exists at {0}' -f $pageFileName)

$existingPageFileSetting = Get-CimInstance -Namespace root\cimv2 -Query "Select * from Win32_PageFileSetting where SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'"
if (-not $existingPageFileSetting)
{
[void] (New-CimInstance -Namespace 'root\cimv2' -ClassName 'Win32_PageFileSetting' -Property @{Name = $pageFileName})
}


$setParams = @{
Namespace = 'root\cimv2'
Query = "Select * from Win32_PageFileSetting where SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'"
Property = @{
InitialSize = 0
MaximumSize = 0
}
}

Write-Verbose -Message "Enabling system-managed page file on $pageFileName"

Set-CimInstance @setParams
$global:DSCMachineStatus = 1
break
}
"NoPagingFile"
{
if ($SystemInfo.AutomaticManagedPageFile)
{
$setParams = @{
Namespace = 'root\cimv2'
Query = 'Select * from Win32_ComputerSystem'
Property = @{AutomaticManagedPageFile = $false}
}

Set-CimInstance @setParams
}

$driveInfo = [System.IO.DriveInfo] $Drive
if (-not $driveInfo.IsReady)
{
throw "Drive $($driveInfo.Name) is not ready. Please ensure that the drive exists and is available"
}

$PageFile = Get-CimInstance -Class Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'"

$existingPageFileSetting = Get-CimInstance -Namespace root\cimv2 -Query "Select * from Win32_PageFileSetting where SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'"
if ($existingPageFileSetting)
{
Write-Verbose -Message "Removing existing page file $($existingPageFileSetting.Name)"
Remove-CimInstance -InputObject $existingPageFileSetting
$global:DSCMachineStatus = 1
}

Write-Verbose -Message "Disabled page file for drive $Drive"

break
}
default
{
throw "A wrong type '$Type' has been selected."
}
}
}

<#
.SYNOPSIS
Tests if virtual memory settings need to be applied based on the parameters supplied
.PARAMETER Drive
The drive letter that should be tested
.PARAMETER Type
The type of the virtual memory configuration
.PARAMETER InitialSize
The initial page file size in megabyte
.PARAMETER MaximumSize
The maximum page file size in megabyte
#>
function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[parameter(Mandatory = $true)]
[System.String]
$Drive,

[ValidateSet("AutoManagePagingFile", "CustomSize", "SystemManagedSize", "NoPagingFile")]
[parameter(Mandatory = $true)]
[System.String]
$Type,

[System.Int64]
$InitialSize,

[System.Int64]
$MaximumSize
)

$SystemInfo = Get-CimInstance -Class Win32_ComputerSystem
$result = $false

switch ($Type)
{
"AutoManagePagingFile"
{
$result = $SystemInfo.AutomaticManagedPagefile
break
}
"CustomSize"
{
if ($SystemInfo.AutomaticManagedPageFile)
{
$result = $false
break
}

$driveInfo = [System.IO.DriveInfo] $Drive
$PageFile = Get-CimInstance -Class Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'"
if (-not $PageFile)
{
$result = $false
break
}

if (-not ($PageFile.InitialSize -eq $InitialSize -and $PageFile.MaximumSize -eq $MaximumSize))
{
$result = $false
break
}

$result = $true
break
}
"SystemManagedSize"
{
if ($SystemInfo.AutomaticManagedPageFile)
{
$result = $false
break
}

$driveInfo = [System.IO.DriveInfo] $Drive
$PageFile = Get-CimInstance -Class Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'"
if (-not $PageFile)
{
$result = $false
break
}

if (-not ($PageFile.InitialSize -eq 0 -and $PageFile.MaximumSize -eq 0))
{
$result = $false
break
}

$result = $true
break
}
"NoPagingFile"
{
if ($SystemInfo.AutomaticManagedPageFile)
{
$result = $false
break
}

$driveInfo = [System.IO.DriveInfo] $Drive
$PageFile = Get-CimInstance -Class Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'"

if ($PageFile)
{
$result = $false
break
}

$result = $true
break
}
default
{
break
}
}

$result
}

Export-ModuleMember -Function *-TargetResource
10 changes: 10 additions & 0 deletions DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.schema.mof
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

[ClassVersion("1.0.0.0"), FriendlyName("xVirtualMemory")]
class MSFT_xVirtualMemory : OMI_BaseResource
{
[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;
[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;
[Write, Description("The initial size of the page file in Megabyte")] Sint64 InitialSize;
[Write, Description("The maximum size of the page file in Megabyte")] Sint64 MaximumSize;
};

Loading