From 3e2c012de78782193f789d563343a2bd50a1886a Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Sun, 19 Feb 2017 10:57:23 +0100 Subject: [PATCH 01/33] Initial check in --- .../MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 | Bin 0 -> 6008 bytes .../MSFT_xVirtualMemory.schema.mof | Bin 0 -> 802 bytes 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 create mode 100644 DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.schema.mof diff --git a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 new file mode 100644 index 0000000000000000000000000000000000000000..71344a2a0565a72637158f03d5d6cd03be3dadbc GIT binary patch literal 6008 zcmeHLS#J|D5T0iw{==$WsR<%cImAN+h+aS*Xb~y&A*xW0Z3#-UX^xh1{B_{_JaN|E zqXz_mP*io}wP(lUZ^qw@{p0(wOl2T#nMhY+v;uh{5#B0tAWa#`iFtd2=NRKt^t92w zEw|*0c?S4`e+}7|zI3E#TAtuP#QO-AN0~Vjef~P?<_kr`N5Xy)$_{jP|>Ao_h`HlbIKurGmof@2DthkPm&+`%2az#LOby9MBz$9OP z+S`fsuI?)I5y#6>v9WO(Ccf4hd^V9s@-P*1){0Ol(S=OcU};ys$Qg*+C+P13icH`7MJ&1N%8PyZ#9Ktcz8--u2^AHJ0X#wQUfmon#R1U2;_bF55=rlQG7!hhOgT zKn@{2kOy*K-W#4FR^%k^eRs|>_{Q$7KE7t$w7p%{neBr}jxokP=6c#~cOg-_a$Oo5 zHeP+6wY^2rQUz}-n<_eMbK70)^7ieTXSJQjjY%?}`>tHX7_^Z#g^zmh4>O3eb~Yl&;x^CydekV2kyvI` zE@pf%>jm-^|E`S|lS5=KMp=Z&+(*2BM2qS8Eb>u3r;S}HI`6!Vq_N_*C1uKuuN-uA{@m`dd78~ELU+u2G z{)(#wIf%YfKdc)+YW+d4+(dT~|0cQ@i@3{{F3ThpgO9x*MG+!+j8(gc{vPKQRX6So z+00YKZNGAroaQ57(}=6`MeZ3|loBCtFh}%P%56XvJlgkX6%MPx%B%zGcr7 z%8GyXJTV04YxVA~U|H)gYyJEk_qS{PRaBp>gqW2psP$P5ua!FhUROWLC#o zxl~~R_mR7oe2aG=--1R~IoGxJ95d;P)pGlNk=5sB?X0G`URpWDoP6iWpJxPHY318X z&$kU{5hv=fSQ4{=>0{>B5lXNkb8k7c=M?j@lHphYTm$?jcN8k4XO;8qASxV-2m~DVW~kd0g52-W+{uyNX{AjAj4Ijm?(v486qKWqB)OPmXR~ zly~}CGBx*Wwl-QawLJs;Z%pkni*8-w~vl#?|MxeO>OzsysEb s`V6~^o4X-cjCTnYaCMA4Njsw1ok9yOX4?wR{pq{&JLtXY-kcWv1hegZ=l}o! literal 0 HcmV?d00001 diff --git a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.schema.mof b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.schema.mof new file mode 100644 index 0000000000000000000000000000000000000000..1506b0a32905b4dc70676a2d771ce95c71f443a5 GIT binary patch literal 802 zcmd6lPfNo<5XIkF@H;FyStz0)9`vLYp-8LPQV&H$+7K3!w3tL1rC(kB%~qmB1-;3# z*_nNB-u&6TzHDntQ}vYUUIlBSSPz_D3+38TqdNUHmWP{bPHe3q@fp5F z3^W2C>rN~5Jj_xRcnT83T5wat>*!XNf7WDozl?P{qJ~S@eX^W}+GAa#UZ7r~Kf^Zd z=kRr~ZB2E-eNA3Rjb-Q|SAusI>_^b!R>Vy}N)YRN68fm{23Sk;&{G zoE|@67M>1cm|b=cb`kg)HF!NW$cV}eu^D~VnzwjEa(%DAZq-=)yZXLPeoWU(dj5It pn;4sAsY4y`3Vm#k=b3WaOy@5OHs2NT8L#p24jYB_doA|=yaBUOh3EhP literal 0 HcmV?d00001 From b19cb5b1eb2fafd873802b2851e6dc86a5320db4 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Tue, 21 Feb 2017 14:34:01 +0100 Subject: [PATCH 02/33] Added functionality to Set, Get and Test --- .../MSFT_xVirtualMemory.psm1 | Bin 6008 -> 6844 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 index 71344a2a0565a72637158f03d5d6cd03be3dadbc..76ac3c5020575232a1483262e4c59274fd66d088 100644 GIT binary patch literal 6844 zcmeHLOK;mo5WXAme;@<`rV5jA)1n6h2GYn$tAk=AG@6586f0>YZOeyBE}f`p|9fZl zK`trMldS+lS-uu*7Qwxd*szSnO4w5`sa_6) zfXth)WWhw5ZJjH}6Q5A$Byupu!NKrLg z!7W>%>^mO8;CwJDa*@U%|C9bGmoL5MT0=u$NfFCKfj0T9jTUE>fl9=&ovS{gk)%`ox(g!T z7|BmwG@E@BNXL(pWqPmZsb@d%OcX4dK#VLeeQ+hC?@_Oi)x=vQv_J|wf9dr$271ksZtTqm%_Vh%8JUv;O@_@X1T6s7od*w>W3yY}FPxexi zVk9LN%s1`1v^9^Kz0N;zF<*NwCBYj1+pDCVL>ue3CT2=e2uv35%KnBVCNH9smGA0P zXBn{Ht05h1q{I%UCNxSDm3KPP)r=;c^z0)@W-S>;e1%L2*Tnm5D}%q3RRu(CXm0^2 zrA$5OV}Y4x>V}UygvHev=z;O|)!9Z@)F9vH&TGf@bQwTQvJ3=aF;o46cX9q|I6NEd zx}ZMdnq`}5%?gAr2`fR7%dNj15`8Xzgibs8eQ;!z4&g_0i`m|tNsAp#mcHqB;K8D7O-SR zgiU&VdDXyGV_Khnsd0A|r85>?UWcOzo3D8Sp1~I`rRSKpN zJQJJ9qKnA-#|EFQ63}vw*i85@I;T{qj#QVIQHDT=<4Aq$`>C!kp?dgzaX7$+(Dbf@ ze=eyH*XADiTTAtuP#QO-AN0~Vjef~P?<_kr`N5Xy)$_{jP|>Ao_h`HlbIKurGmof@2DthkPm&+`%2az#LOby9MBz$9OP z+S`fsuI?)I5y#6>v9WO(Ccf4hd^V9s@-P*1){0Ol(S=OcU};ys$Qg*+C+P13icH`7MJ&1N%8PyZ#9Ktcz8--u2^AHJ0X#wQUfmon#R1U2;_bF55=rlQG7!hhOgT zKn@{2kOy*K-W#4FR^%k^eRs|>_{Q$7KE7t$w7p%{neBr}jxokP=6c#~cOg-_a$Oo5 zHeP+6wY^2rQUz}-n<_eMbK70)^7ieTXSJQjjY%?}`>tHX7_^Z#g^zmh4>O3eb~Yl&;x^CydekV2kyvI` zE@pf%>jm-^|E`S|lS5=KMp=Z&+(*2BM2qS8Eb>u3r;S}HI`6!Vq_N_*C1uKuuN-uA{@m`dd78~ELU+u2G z{)(#wIf%YfKdc)+YW+d4+(dT~|0cQ@i@3{{F3ThpgO9x*MG+!+j8(gc{vPKQRX6So z+00YKZNGAroaQ57(}=6`MeZ3|loBCtFh}%P%56XvJlgkX6%MPx%B%zGcr7 z%8GyXJTV04YxVA~U|H)gYyJEk_qS{PRaBp>gqW2psP$P5ua!FhUROWLC#o zxl~~R_mR7oe2aG=--1R~IoGxJ95d;P)pGlNk=5sB?X0G`URpWDoP6iWpJxPHY318X z&$kU{5hv=fSQ4{=>0{>B5lXNkb8k7c=M?j@lHphYTm$?jcN8k4XO;8qASxV-2m~DVW~kd0g52-W+{uyNX{AjAj4Ijm?(v486qKWqB)OPmXR~ zly~}CGBx*Wwl-QawLJs;Z%pkni*8-w~vl#?|MxeO>OzsysEb s`V6~^o4X-cjCTnYaCMA4Njsw1ok9yOX4?wR{pq{&JLtXY-kcWv1hegZ=l}o! From 72d335933e6cbe3923edb6e9d5543817c72da33c Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Tue, 21 Feb 2017 14:46:42 +0100 Subject: [PATCH 03/33] File conversion (UTF8 and ASCII) --- .../MSFT_xVirtualMemory.psm1 | 18 ++++++------------ .../MSFT_xVirtualMemory.schema.mof | Bin 802 -> 400 bytes 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 index 76ac3c50..5f02ffbb 100644 --- a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 +++ b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 @@ -119,8 +119,7 @@ function Set-TargetResource { $driveInfo = [System.IO.DriveInfo]$Drive $PageFile = Get-CimInstance -Class Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'" - if($PageFile) - { + if($PageFile) { $PageFile | Remove-CimInstance } break @@ -169,14 +168,12 @@ function Test-TargetResource { $driveInfo = [System.IO.DriveInfo]$Drive $PageFile = Get-CimInstance -Class Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'" - if(-not $PageFile) - { + if(-not $PageFile) { $result = $false break } - if(-not $PageFile.InitialSize -eq $InitialSize -and -not $PageFile.MaximumSize -eq $MaximumSize) - { + if(-not $PageFile.InitialSize -eq $InitialSize -and -not $PageFile.MaximumSize -eq $MaximumSize) { $result = $false break } @@ -192,14 +189,12 @@ function Test-TargetResource { $driveInfo = [System.IO.DriveInfo]$Drive $PageFile = Get-CimInstance -Class Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'" - if(-not $PageFile) - { + if(-not $PageFile) { $result = $false break } - if(-not $PageFile.InitialSize -eq 0 -and -not $PageFile.MaximumSize -eq 0) - { + if(-not $PageFile.InitialSize -eq 0 -and -not $PageFile.MaximumSize -eq 0) { $result = $false break } @@ -216,8 +211,7 @@ function Test-TargetResource { $driveInfo = [System.IO.DriveInfo]$Drive $PageFile = Get-CimInstance -Class Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'" - if($PageFile) - { + if($PageFile) { $result = $false break } diff --git a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.schema.mof b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.schema.mof index 1506b0a32905b4dc70676a2d771ce95c71f443a5..d5aec0c0436d008f2dd88601928b706ce7979513 100644 GIT binary patch literal 400 zcmb`CO-lno42JIm^B;yewXlc<@t`NIER-@;+_D}@DWMHZU_Q!xNXOFu?yN4wg2yK0 zki1XA8_rf!4I#AX135dAlbhQ<<+u{HC$u}A7hrHCRZ}lFhn9RypcNYDvmCEE+xF|? zA5}bvS2f!_L%??oF8M8TcH~TySj{o7MH4;QPP};f#K%GS@D#BUE$D;__DA`YqEld@ z!#C&(UXw;y$!QAFnTEdlrpY0q`K5NlY~lX!r{Q9_*1fa&W!_(UFACXsedNUHmWP{bPHe3q@fp5F z3^W2C>rN~5Jj_xRcnT83T5wat>*!XNf7WDozl?P{qJ~S@eX^W}+GAa#UZ7r~Kf^Zd z=kRr~ZB2E-eNA3Rjb-Q|SAusI>_^b!R>Vy}N)YRN68fm{23Sk;&{G zoE|@67M>1cm|b=cb`kg)HF!NW$cV}eu^D~VnzwjEa(%DAZq-=)yZXLPeoWU(dj5It pn;4sAsY4y`3Vm#k=b3WaOy@5OHs2NT8L#p24jYB_doA|=yaBUOh3EhP From 9d8e2366b2a589e89b0b1d700e9003ad829708fe Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Tue, 21 Feb 2017 14:53:02 +0100 Subject: [PATCH 04/33] Set Type as mandatory (because it should be) and removed irrelevant parameters from Get --- .../MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 | 11 ++++------- .../MSFT_xVirtualMemory.schema.mof | 2 +- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 index 5f02ffbb..dc3aa61b 100644 --- a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 +++ b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 @@ -8,14 +8,9 @@ function Get-TargetResource { $Drive, [ValidateSet("AutoManagePagingFile","CustomSize","SystemManagedSize","NoPagingFile")] + [parameter(Mandatory = $true)] [System.String] - $Type, - - [System.Int64] - $InitialSize, - - [System.Int64] - $MaximumSize + $Type ) $returnValue = @{ @@ -65,6 +60,7 @@ function Set-TargetResource { $Drive, [ValidateSet("AutoManagePagingFile","CustomSize","SystemManagedSize","NoPagingFile")] + [parameter(Mandatory = $true)] [System.String] $Type, @@ -143,6 +139,7 @@ function Test-TargetResource { $Drive, [ValidateSet("AutoManagePagingFile","CustomSize","SystemManagedSize","NoPagingFile")] + [parameter(Mandatory = $true)] [System.String] $Type, diff --git a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.schema.mof b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.schema.mof index d5aec0c0..431383a3 100644 --- a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.schema.mof +++ b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.schema.mof @@ -3,7 +3,7 @@ class MSFT_xVirtualMemory : OMI_BaseResource { [Key] String Drive; - [Write, ValueMap{"AutoManagePagingFile","CustomSize","SystemManagedSize","NoPagingFile"}, Values{"AutoManagePagingFile","CustomSize","SystemManagedSize","NoPagingFile"}] String Type; + [Key, ValueMap{"AutoManagePagingFile","CustomSize","SystemManagedSize","NoPagingFile"}, Values{"AutoManagePagingFile","CustomSize","SystemManagedSize","NoPagingFile"}] String Type; [Write] Sint64 InitialSize; [Write] Sint64 MaximumSize; }; From 4166b8637b106969bc7a204e654cdad88c27ea6c Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Tue, 21 Feb 2017 15:40:46 +0100 Subject: [PATCH 05/33] Started adding tests --- Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 | 102 +++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 diff --git a/Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 b/Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 new file mode 100644 index 00000000..8381ebe8 --- /dev/null +++ b/Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 @@ -0,0 +1,102 @@ +#region HEADER +$script:DSCModuleName = 'xComputerManagement' +$script:DSCResourceName = 'MSFT_xVirtualMemory' + +# Unit Test Template Version: 1.2.0 +$script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) +if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or ` + (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) ) +{ + & git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $script:moduleRoot -ChildPath '\DSCResource.Tests\')) +} + +Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'DSCResource.Tests' -ChildPath 'TestHelper.psm1')) -Force + +$TestEnvironment = Initialize-TestEnvironment ` + -DSCModuleName 'XComputerManagement' ` + -DSCResourceName 'MSFT_xVirtualMemory' ` + -TestType Unit + +#endregion HEADER + +function Invoke-TestSetup { +} + +function Invoke-TestCleanup { + Restore-TestEnvironment -TestEnvironment $TestEnvironment +} + +# Begin Testing +try +{ + Invoke-TestSetup + + InModuleScope 'MSFT_xVirtualMemory' { + + Describe "$($script:DSCResourceName)\Get-TargetResource" { + + BeforeEach { + $testParameters = @{ + Drive = 'C:' + Type = 'CustomSize' + InitialSize = 128 + MaximumSize = 2048 + } + } + + Context 'When the system is in the desired present state' { + BeforeEach { + Mock -CommandName Get-CimInstance -MockWith { + return New-Object Object | + Add-Member -MemberType NoteProperty -Name AutomaticManagedPagefile -Value $false -PassThru -Force | + Add-MemberType NoteProperty -Name Name -Value "C:\pagefile.sys" -PassThru -Force + } -ModuleName $script:DSCResourceName -Verifiable + } + + It 'Should return the same values as passed as parameters' { + $result = Get-TargetResource @testParameters + $result.Type | Should Be $testParameters.Type + $result.InitialSize | Should Be $testParameters.InitialSize + $result.MaximumSize | Should Be $testParameters.MaximumSize + $result.Drive | Should Be $testParameters.Drive + } + } + + <#Context 'When the system is not in the desired present state' { + BeforeEach { + Mock -CommandName Get-CimInstance -MockWith { + return New-Object Object | + Add-Member -MemberType NoteProperty -Name IsActive -Value $false -PassThru -Force + } -ModuleName $script:DSCResourceName -Verifiable + } + + It 'Should not return any plan name' { + $result = Get-TargetResource @testParameters + $result.IsSingleInstance | Should Be 'Yes' + $result.Name | Should Be $null + } + }#> + } + <# + Describe "$($script:DSCResourceName)\Set-TargetResource" { + Context '' { + It 'Should ...test-description' { + # test-code + } + } + } + + Describe "$($script:DSCResourceName)\Test-TargetResource" { + Context '' { + It 'Should ...test-description' { + # test-code + } + } + }#> + } +} +finally +{ + Invoke-TestCleanup +} + From 5a44ae3a7cfd468c76a76f2c4f54aad565b13f15 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Tue, 21 Feb 2017 15:41:07 +0100 Subject: [PATCH 06/33] Added drive to return values in Get-TargetResource --- DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 | 1 + 1 file changed, 1 insertion(+) diff --git a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 index dc3aa61b..fa49fcce 100644 --- a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 +++ b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 @@ -43,6 +43,7 @@ function Get-TargetResource { $returnValue.Type = "CustomSize" } + $returnValue.Drive = $driveItem.Name $returnValue.InitialSize = $virtualMemoryInstance.InitialSize $returnValue.MaximumSize = $virtualMemoryInstance.MaximumSize From ef43e614b8f13b856cd1ce980ed408941cff792a Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Mon, 27 Feb 2017 14:48:16 +0100 Subject: [PATCH 07/33] Module modified to squash a few bugs --- .../MSFT_xVirtualMemory.psm1 | 145 ++++++++++++++---- 1 file changed, 114 insertions(+), 31 deletions(-) diff --git a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 index fa49fcce..fc46de7a 100644 --- a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 +++ b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 @@ -13,6 +13,8 @@ function Get-TargetResource { $Type ) + Write-Verbose 'Getting current page file settings' + $returnValue = @{ Drive = [string]::Empty Type = [string]::Empty @@ -25,11 +27,13 @@ function Get-TargetResource { if($isSystemManaged) { $returnValue.Type = 'AutoManagePagingFile' return $returnValue - } - + } + $driveItem = [System.IO.DriveInfo]$Drive - $virtualMemoryInstance = Get-CimInstance -ClassName Win32_PageFileSetting | - Where-Object {([System.IO.DriveInfo](Split-Path -Name $PSItem.Name)).Name -eq $driveItem.Name} + + Write-Verbose "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))'" + + $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' @@ -43,12 +47,11 @@ function Get-TargetResource { $returnValue.Type = "CustomSize" } - $returnValue.Drive = $driveItem.Name + $returnValue.Drive = $virtualMemoryInstance.Name.Substring(0,3) $returnValue.InitialSize = $virtualMemoryInstance.InitialSize $returnValue.MaximumSize = $virtualMemoryInstance.MaximumSize $returnValue - } @@ -71,62 +74,142 @@ function Set-TargetResource { [System.Int64] $MaximumSize ) + + Write-Verbose 'Setting page file' + $SystemInfo = Get-CimInstance -Class Win32_ComputerSystem switch($Type) { "AutoManagePagingFile" { - $SystemInfo.AutomaticManagedPageFile = $true - $SystemInfo | Set-CimInstance + $setParams = @{ + Namespace = 'root\cimv2' + Query = 'Select * from Win32_ComputerSystem' + Property = @{AutomaticManagedPageFile = $true} + } + + Write-Verbose 'Enabling AutoManagePagingFile' + + Set-CimInstance @setParams + $global:DSCMachineStatus = 1 break } "CustomSize" { if($SystemInfo.AutomaticManagedPageFile) { - $SystemInfo.AutomaticManagedPageFile = $false - $SystemInfo | Set-CimInstance + + $setParams = @{ + Namespace = 'root\cimv2' + Query = 'Select * from Win32_ComputerSystem' + Property = @{AutomaticManagedPageFile = $false} + } + + Write-Verbose 'Disabling AutoManagePagingFile' + + Set-CimInstance @setParams } $driveInfo = [System.IO.DriveInfo]$Drive - $PageFile = Get-CimInstance -Class Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'" - - $PageFile.InitialSize = $InitialSize - $PageFile.MaximumSize = $MaximumSize - $PageFile | Set-CimInstance + if(-not $driveInfo.IsReady) { + throw "Drive $($driveInfo.Name) is not ready. Please ensure that the drive exists and is available" + } + + $pageFileName = Join-Path $driveInfo.Name 'pagefile.sys' + + Write-Verbose ('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 = $InitialSize + MaximumSize = $MaximumSize + } + } + + Write-Verbose "Setting page file to $pageFileName. Initial size $InitialSize MB, maximum size $MaximumSize MB" + Set-CimInstance @setParams + $global:DSCMachineStatus = 1 break } "SystemManagedSize" { if($SystemInfo.AutomaticManagedPageFile) { - $SystemInfo.AutomaticManagedPageFile = $false - $SystemInfo | Set-CimInstance + $setParams = @{ + Namespace = 'root\cimv2' + Query = 'Select * from Win32_ComputerSystem' + Property = @{AutomaticManagedPageFile = $false} + } + + Write-Verbose 'Disabling AutoManagePagingFile' + + Set-CimInstance @setParams } $driveInfo = [System.IO.DriveInfo]$Drive - $PageFile = Get-CimInstance -Class Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'" - - $PageFile.InitialSize = 0 - $PageFile.MaximumSize = 0 - $PageFile | Set-CimInstance + if(-not $driveInfo.IsReady) { + Write-Error "Drive $($driveInfo.Name) is not ready. Please ensure that the drive exists and is available" -TargetObject $driveInfo + } + + $pageFileName = Join-Path $Drive.Name 'pagefile.sys' + + Write-Verbose ('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' + ClassName = 'Win32_PageFileSetting' + Property = @{ + InitialSize = 0 + MaximumSize = 0 + } + } + + Write-Verbose "Enabling system-managed page file on $pageFileName" + + New-CimInstance @setParams + $global:DSCMachineStatus = 1 break } "NoPagingFile" { if($SystemInfo.AutomaticManagedPageFile) { - $SystemInfo.AutomaticManagedPageFile = $false - $SystemInfo | Set-CimInstance + $setParams = @{ + Namespace = 'root\cimv2' + Query = 'Select * from Win32_ComputerSystem' + Property = @{AutomaticManagedPageFile = $false} + } + + Set-CimInstance @setParams } $driveInfo = [System.IO.DriveInfo]$Drive + if(-not $driveInfo.IsReady) { + Write-Error "Drive $($driveInfo.Name) is not ready. Please ensure that the drive exists and is available" -TargetObject $driveInfo + } + $PageFile = Get-CimInstance -Class Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'" - if($PageFile) { - $PageFile | Remove-CimInstance + $existingPageFileSetting = Get-CimInstance -Namespace root\cimv2 -Query "Select * from Win32_PageFileSetting where SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'" + if($existingPageFileSetting) { + Write-Verbose "Removing existing page file $($existingPageFileSetting.Name)" + Remove-CimInstance -InputObject $existingPageFileSetting + $global:DSCMachineStatus = 1 } + + Write-Verbose "Disabled page file for drive $Drive" + break } default { - return + throw "A wrong type '$Type' has been selected." } } - - $global:DSCMachineStatus = 1 } @@ -171,7 +254,7 @@ function Test-TargetResource { break } - if(-not $PageFile.InitialSize -eq $InitialSize -and -not $PageFile.MaximumSize -eq $MaximumSize) { + if(-not ($PageFile.InitialSize -eq $InitialSize -and $PageFile.MaximumSize -eq $MaximumSize)) { $result = $false break } @@ -192,7 +275,7 @@ function Test-TargetResource { break } - if(-not $PageFile.InitialSize -eq 0 -and -not $PageFile.MaximumSize -eq 0) { + if(-not ($PageFile.InitialSize -eq 0 -and $PageFile.MaximumSize -eq 0)) { $result = $false break } From 54000346cf6904a536bdf3aadda2c68969745342 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Mon, 27 Feb 2017 14:48:26 +0100 Subject: [PATCH 08/33] Updated friendly name in schema --- DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.schema.mof | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.schema.mof b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.schema.mof index 431383a3..f5992962 100644 --- a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.schema.mof +++ b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.schema.mof @@ -1,5 +1,5 @@ -[ClassVersion("1.0.0.0"), FriendlyName("MSFT_xVirtualMemory")] +[ClassVersion("1.0.0.0"), FriendlyName("xVirtualMemory")] class MSFT_xVirtualMemory : OMI_BaseResource { [Key] String Drive; From 8874086e7515f00bc3e13ed3e11604d78e2cac4b Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Mon, 27 Feb 2017 14:48:51 +0100 Subject: [PATCH 09/33] Updated all unit tests --- Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 | 169 ++++++++++++++++++----- 1 file changed, 132 insertions(+), 37 deletions(-) diff --git a/Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 b/Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 index 8381ebe8..46e55824 100644 --- a/Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 +++ b/Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 @@ -5,16 +5,15 @@ $script:DSCResourceName = 'MSFT_xVirtualMemory' # Unit Test Template Version: 1.2.0 $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or ` - (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) ) -{ + (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) ) { & git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $script:moduleRoot -ChildPath '\DSCResource.Tests\')) } Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'DSCResource.Tests' -ChildPath 'TestHelper.psm1')) -Force $TestEnvironment = Initialize-TestEnvironment ` - -DSCModuleName 'XComputerManagement' ` - -DSCResourceName 'MSFT_xVirtualMemory' ` + -DSCModuleName $script:DSCModuleName ` + -DSCResourceName $script:DSCResourceName ` -TestType Unit #endregion HEADER @@ -27,76 +26,172 @@ function Invoke-TestCleanup { } # Begin Testing -try -{ +try { Invoke-TestSetup - InModuleScope 'MSFT_xVirtualMemory' { + InModuleScope 'MSFT_xVirtualMemory' { Describe "$($script:DSCResourceName)\Get-TargetResource" { BeforeEach { - $testParameters = @{ - Drive = 'C:' - Type = 'CustomSize' - InitialSize = 128 - MaximumSize = 2048 - } + $testParameters = @{ + Drive = 'D:' + Type = 'CustomSize' + } } Context 'When the system is in the desired present state' { BeforeEach { Mock -CommandName Get-CimInstance -MockWith { return New-Object Object | - Add-Member -MemberType NoteProperty -Name AutomaticManagedPagefile -Value $false -PassThru -Force | - Add-MemberType NoteProperty -Name Name -Value "C:\pagefile.sys" -PassThru -Force + Add-Member -MemberType NoteProperty -Name AutomaticManagedPagefile -Value $false -PassThru -Force | + Add-Member -MemberType NoteProperty -Name Name -Value "D:\pagefile.sys" -PassThru -Force } -ModuleName $script:DSCResourceName -Verifiable } - It 'Should return the same values as passed as parameters' { + It 'It should return the same values as passed as parameters' { $result = Get-TargetResource @testParameters $result.Type | Should Be $testParameters.Type - $result.InitialSize | Should Be $testParameters.InitialSize - $result.MaximumSize | Should Be $testParameters.MaximumSize - $result.Drive | Should Be $testParameters.Drive + $result.Drive | Should Be ([System.IO.DriveInfo]$testParameters.Drive).Name } } - <#Context 'When the system is not in the desired present state' { + Context 'When the system is not in the desired present state' { BeforeEach { Mock -CommandName Get-CimInstance -MockWith { return New-Object Object | - Add-Member -MemberType NoteProperty -Name IsActive -Value $false -PassThru -Force + Add-Member -MemberType NoteProperty -Name InitialSize -Value 0 -PassThru -Force | + Add-Member -MemberType NoteProperty -Name MaximumSize -Value 0 -PassThru -Force | + Add-Member -MemberType NoteProperty -Name Name -Value "C:\pagefile.sys" -PassThru -Force } -ModuleName $script:DSCResourceName -Verifiable } - It 'Should not return any plan name' { + It 'It should not return a valid type' { + $result = Get-TargetResource @testParameters + $result.Type | Should Not Be $testParameters.Type + } + + It 'It should not return a valid drive letter' { $result = Get-TargetResource @testParameters - $result.IsSingleInstance | Should Be 'Yes' - $result.Name | Should Be $null + $result.Drive | Should Not Be ([System.IO.DriveInfo]$testParameters.Drive).Name } - }#> + } + + Assert-VerifiableMocks } - <# - Describe "$($script:DSCResourceName)\Set-TargetResource" { - Context '' { - It 'Should ...test-description' { - # test-code + + Describe "$($script:DSCResourceName)\Set-TargetResource" { + + Context 'When the system is not in the desired state' { + BeforeEach { + $testParameters = @{ + Drive = 'D:' + Type = 'CustomSize' + InitialSize = 0 + MaximumSize = 1337 + } + } + Mock -CommandName Set-CimInstance -MockWith {} -ModuleName $script:DSCResourceName -Verifiable + Mock -CommandName New-CimInstance -MockWith {} -ModuleName $script:DSCResourceName -Verifiable + Mock -CommandName Remove-CimInstance -MockWith {} -ModuleName $script:DSCResourceName -Verifiable + Mock -CommandName Get-CimInstance -MockWith {} -ModuleName $script:DSCResourceName -Verifiable + + It 'Should call the mocked function Set-CimInstance exactly once' { + Set-TargetResource @testParameters + + Assert-MockCalled -CommandName Set-CimInstance -Exactly -Times 1 -Scope It #-ModuleName $script:DSCResourceName -Verbose + } + } + + + context 'When an exception is expected' { + Mock -CommandName Set-CimInstance -MockWith {} -ModuleName $script:DSCResourceName -Verifiable + Mock -CommandName New-CimInstance -MockWith {} -ModuleName $script:DSCResourceName -Verifiable + Mock -CommandName Remove-CimInstance -MockWith {} -ModuleName $script:DSCResourceName -Verifiable + Mock -CommandName Get-CimInstance -MockWith { + New-Object psobject | + Add-Member -MemberType NoteProperty -Name InitialSize -Value 0 -PassThru -Force | + Add-Member -MemberType NoteProperty -Name MaximumSize -Value 1338 -PassThru -Force | + Add-Member -MemberType NoteProperty -Name Name -Value "D:\pagefile.sys" -PassThru -Force | + Add-Member -MemberType NoteProperty -Name AutomaticManagedPageFile -Value $false -PassThru -Force + } + + $testParameters = @{ + Drive = 'abc' + Type = 'CustomSize' + InitialSize = 0 + MaximumSize = 1337 + } + It 'Should throw if no valid drive letter has been used' { { Set-TargetResource @testParameters } | Should Throw + } + + $testParameters = @{ + Drive = 'N:' + Type = 'CustomSize' + InitialSize = 0 + MaximumSize = 1337 + } + It 'Should throw if the drive is not ready' { { Set-TargetResource @testParameters } | Should Throw } } + + Assert-VerifiableMocks } - Describe "$($script:DSCResourceName)\Test-TargetResource" { - Context '' { - It 'Should ...test-description' { - # test-code + Describe "$($script:DSCResourceName)\Test-TargetResource" { + Context 'When a True or False is expected' { + BeforeEach { + $testParameters = @{ + Drive = 'D:' + Type = 'CustomSize' + InitialSize = 0 + MaximumSize = 1337 + } + } + Mock -CommandName Get-CimInstance -MockWith { + New-Object psobject | + Add-Member -MemberType NoteProperty -Name InitialSize -Value 0 -PassThru -Force | + Add-Member -MemberType NoteProperty -Name MaximumSize -Value 1337 -PassThru -Force | + Add-Member -MemberType NoteProperty -Name Name -Value "D:\pagefile.sys" -PassThru -Force | + Add-Member -MemberType NoteProperty -Name AutomaticManagedPageFile -Value $false -PassThru -Force + } + It 'Should return True if the input matches the actual values' { + Test-TargetResource @testParameters | Should Be $true + } + + Mock -CommandName Get-CimInstance -MockWith { + New-Object psobject | + Add-Member -MemberType NoteProperty -Name InitialSize -Value 0 -PassThru -Force | + Add-Member -MemberType NoteProperty -Name MaximumSize -Value 1337 -PassThru -Force | + Add-Member -MemberType NoteProperty -Name Name -Value "D:\pagefile.sys" -PassThru -Force | + Add-Member -MemberType NoteProperty -Name AutomaticManagedPageFile -Value $true -PassThru -Force + } + It 'Should return False if the type is wrong' { + Test-TargetResource @testParameters | Should Be $false + } + + Mock -CommandName Get-CimInstance -MockWith { + New-Object psobject | + Add-Member -MemberType NoteProperty -Name InitialSize -Value 0 -PassThru -Force | + Add-Member -MemberType NoteProperty -Name MaximumSize -Value 1338 -PassThru -Force | + Add-Member -MemberType NoteProperty -Name Name -Value "D:\pagefile.sys" -PassThru -Force | + Add-Member -MemberType NoteProperty -Name AutomaticManagedPageFile -Value $false -PassThru -Force + } + It 'Should return False if InitialSize and/or MaximumSize do not match' { + Test-TargetResource @testParameters | Should Be $false + } + + Mock -CommandName Get-CimInstance -MockWith { # In this case Get-CimInstance returns an empty object + } + It 'Should return False if Name does not match' { + Test-TargetResource @testParameters | Should Be $false } + } - }#> + } } } -finally -{ +finally { Invoke-TestCleanup } From 76d18067bfc4b2e2ca6fa676ce65e006b2effdb4 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Mon, 27 Feb 2017 15:09:13 +0100 Subject: [PATCH 10/33] Fixed set-targetresource for SystemManagedPageFile --- DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 index fc46de7a..a96fee6f 100644 --- a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 +++ b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 @@ -152,7 +152,7 @@ function Set-TargetResource { Write-Error "Drive $($driveInfo.Name) is not ready. Please ensure that the drive exists and is available" -TargetObject $driveInfo } - $pageFileName = Join-Path $Drive.Name 'pagefile.sys' + $pageFileName = Join-Path $driveInfo.Name 'pagefile.sys' Write-Verbose ('Checking if a paging file already exists at {0}' -f $pageFileName) @@ -164,7 +164,7 @@ function Set-TargetResource { $setParams = @{ Namespace = 'root\cimv2' - ClassName = 'Win32_PageFileSetting' + Query = "Select * from Win32_PageFileSetting where SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'" Property = @{ InitialSize = 0 MaximumSize = 0 @@ -173,7 +173,7 @@ function Set-TargetResource { Write-Verbose "Enabling system-managed page file on $pageFileName" - New-CimInstance @setParams + Set-CimInstance @setParams $global:DSCMachineStatus = 1 break } From 37a91319c74494f77010c6a950867a4aa8710915 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Mon, 27 Feb 2017 15:35:24 +0100 Subject: [PATCH 11/33] Added integration tests for all scenarios --- .../MSFT_xVirtualMemory.Config.ps1 | 49 ++++++ .../MSFT_xVirtualMemory.Integration.Tests.ps1 | 143 ++++++++++++++++++ 2 files changed, 192 insertions(+) create mode 100644 Tests/Integration/MSFT_xVirtualMemory.Config.ps1 create mode 100644 Tests/Integration/MSFT_xVirtualMemory.Integration.Tests.ps1 diff --git a/Tests/Integration/MSFT_xVirtualMemory.Config.ps1 b/Tests/Integration/MSFT_xVirtualMemory.Config.ps1 new file mode 100644 index 00000000..74248787 --- /dev/null +++ b/Tests/Integration/MSFT_xVirtualMemory.Config.ps1 @@ -0,0 +1,49 @@ +Configuration setToAuto +{ + Import-DscResource -ModuleName xComputerManagement + node "localhost" { + xVirtualMemory vMem + { + Type = 'AutoManagePagingFile' + Drive = 'C' + } + } +} + +Configuration setToCustom +{ + Import-DscResource -ModuleName xComputerManagement + node "localhost" { + xVirtualMemory vMem + { + Type = 'CustomSize' + Drive = 'C' + InitialSize = 128 + MaximumSize = 1024 + } + } +} + +Configuration setToSystemManaged +{ + Import-DscResource -ModuleName xComputerManagement + node "localhost" { + xVirtualMemory vMem + { + Type = 'SystemManagedSize' + Drive = 'C' + } + } +} + +Configuration setToNone +{ + Import-DscResource -ModuleName xComputerManagement + node "localhost" { + xVirtualMemory vMem + { + Type = 'NoPagingFile' + Drive = 'C' + } + } +} \ No newline at end of file diff --git a/Tests/Integration/MSFT_xVirtualMemory.Integration.Tests.ps1 b/Tests/Integration/MSFT_xVirtualMemory.Integration.Tests.ps1 new file mode 100644 index 00000000..672df139 --- /dev/null +++ b/Tests/Integration/MSFT_xVirtualMemory.Integration.Tests.ps1 @@ -0,0 +1,143 @@ +<# +.Synopsis + Template for creating DSC Resource Integration Tests +.DESCRIPTION + To Use: + 1. Copy to \Tests\Integration\ folder and rename .Integration.tests.ps1 (e.g. MSFT_xNeworking.Integration.tests.ps1) + 2. Customize TODO sections. + 3. Create test DSC Configurtion file .config.ps1 (e.g. MSFT_xNeworking.config.ps1) from integration_config_template.ps1 file. + +.NOTES + Code in HEADER, FOOTER and DEFAULT TEST regions are standard and may be moved into + DSCResource.Tools in Future and therefore should not be altered if possible. +#> + +$script:DSCModuleName = 'xComputerManagement' +$script:DSCResourceName = 'MSFT_xVirtualMemory' + +#region HEADER +# Integration Test Template Version: 1.1.1 +[String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) +if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or ` + (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) ) +{ + & git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $script:moduleRoot -ChildPath '\DSCResource.Tests\')) +} + +Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'DSCResource.Tests' -ChildPath 'TestHelper.psm1')) -Force +$TestEnvironment = Initialize-TestEnvironment ` + -DSCModuleName $script:DSCModuleName ` + -DSCResourceName $script:DSCResourceName ` + -TestType Integration + +#endregion + +# Using try/finally to always cleanup. +try +{ + #region Integration Tests + $configFile = Join-Path -Path $PSScriptRoot -ChildPath "$($script:DSCResourceName).config.ps1" + . $configFile + + Describe "$($script:DSCResourceName)_Integration" { + + Context "Set page file to automatically managed" { + $CurrentConfig = "setToAuto" + $ConfigDir = (Join-Path $TestDrive $CurrentConfig) + $ConfigMof = (Join-Path $ConfigDir "localhost.mof") + + It "should compile a MOF file without error" { + { + . $CurrentConfig -OutputPath $ConfigDir + } | Should Not Throw + } + + It "should apply the MOF correctly" { + { + Start-DscConfiguration -Path $ConfigDir -Wait -Verbose -Force + } | Should Not Throw + } + + It "should return a compliant state after being applied" { + (Test-DscConfiguration -ReferenceConfiguration $ConfigMof -Verbose).InDesiredState | Should be $true + } + } + + Context "Set page file to custom size" { + $CurrentConfig = "setToCustom" + $ConfigDir = (Join-Path $TestDrive $CurrentConfig) + $ConfigMof = (Join-Path $ConfigDir "localhost.mof") + + It "should compile a MOF file without error" { + { + . $CurrentConfig -OutputPath $ConfigDir + } | Should Not Throw + } + + It "should apply the MOF correctly" { + { + Start-DscConfiguration -Path $ConfigDir -Wait -Verbose -Force + } | Should Not Throw + } + + It "should return a compliant state after being applied" { + (Test-DscConfiguration -ReferenceConfiguration $ConfigMof -Verbose).InDesiredState | Should be $true + } + } + + Context "Set page file to system managed" { + $CurrentConfig = "setToSystemManaged" + $ConfigDir = (Join-Path $TestDrive $CurrentConfig) + $ConfigMof = (Join-Path $ConfigDir "localhost.mof") + + It "should compile a MOF file without error" { + { + . $CurrentConfig -OutputPath $ConfigDir + } | Should Not Throw + } + + It "should apply the MOF correctly" { + { + Start-DscConfiguration -Path $ConfigDir -Wait -Verbose -Force + } | Should Not Throw + } + + It "should return a compliant state after being applied" { + (Test-DscConfiguration -ReferenceConfiguration $ConfigMof -Verbose).InDesiredState | Should be $true + } + } + + Context "Set page file to none" { + $CurrentConfig = "setToNone" + $ConfigDir = (Join-Path $TestDrive $CurrentConfig) + $ConfigMof = (Join-Path $ConfigDir "localhost.mof") + + It "should compile a MOF file without error" { + { + . $CurrentConfig -OutputPath $ConfigDir + } | Should Not Throw + } + + It "should apply the MOF correctly" { + { + Start-DscConfiguration -Path $ConfigDir -Wait -Verbose -Force + } | Should Not Throw + } + + It "should return a compliant state after being applied" { + (Test-DscConfiguration -ReferenceConfiguration $ConfigMof -Verbose).InDesiredState | Should be $true + } + } + } +} +finally +{ + #region FOOTER + + Restore-TestEnvironment -TestEnvironment $TestEnvironment + + #endregion + + # TODO: Other Optional Cleanup Code Goes Here... +} + From d843364f4c991ae0154475fe31643b0fbd8f1dff Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Mon, 27 Feb 2017 15:47:08 +0100 Subject: [PATCH 12/33] Updated README.md --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index e0028faa..40a13ba0 100644 --- a/README.md +++ b/README.md @@ -76,10 +76,21 @@ xPowerPlan resource has following properties: * IsSingleInstance: Specifies the resource is a single instance, the value must be 'Yes'. * Name: The name of the power plan to activate. + +## xVirtualMemory +xVirtualMemory resource is used to set the properties of the paging file on the local computer. +xVirtualMemory has the following properties: +* Type: The type of the paging settings, mandatory, out of "AutoManagePagingFile","CustomSize","SystemManagedSize","NoPagingFile" +* Drive: The drive to enable paging on, mandatory. Ignored for "AutoManagePagingFile" +* InitialSize: The initial size in MB of the paging file. Ignored for Type "AutoManagePagingFile" and "SystemManagedSize" +* MaximumSize: The maximum size in MB of the paging file. Ignored for Type "AutoManagePagingFile" and "SystemManagedSize" ## Versions ### Unreleased +### 2.0.0.0 +* Added resources + - xVirtualMemory ### 1.9.0.0 * Added resources From 65d12e5e10aa8fa696ade7cb0af3ac2fd38c7629 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Mon, 27 Feb 2017 15:47:28 +0100 Subject: [PATCH 13/33] Updated version in xComputerManagement.psd1 --- xComputerManagement.psd1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xComputerManagement.psd1 b/xComputerManagement.psd1 index ea995901..fbe998e1 100644 --- a/xComputerManagement.psd1 +++ b/xComputerManagement.psd1 @@ -1,6 +1,6 @@ @{ # Version number of this module. -ModuleVersion = '1.9.0.0' +ModuleVersion = '2.0.0.0' # ID used to uniquely identify this module GUID = 'B5004952-489E-43EA-999C-F16A25355B89' From a0b10850a883b2c955cc5e3723082ac88ea5ee5e Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Mon, 27 Feb 2017 15:49:29 +0100 Subject: [PATCH 14/33] Version comments put into wrong section --- README.md | 1 - xComputerManagement.psd1 | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 40a13ba0..f57aeb66 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,6 @@ xVirtualMemory has the following properties: ## Versions ### Unreleased -### 2.0.0.0 * Added resources - xVirtualMemory diff --git a/xComputerManagement.psd1 b/xComputerManagement.psd1 index fbe998e1..ea995901 100644 --- a/xComputerManagement.psd1 +++ b/xComputerManagement.psd1 @@ -1,6 +1,6 @@ @{ # Version number of this module. -ModuleVersion = '2.0.0.0' +ModuleVersion = '1.9.0.0' # ID used to uniquely identify this module GUID = 'B5004952-489E-43EA-999C-F16A25355B89' From f58e79e45fce82613fd62274057d3105fc395485 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Mon, 27 Feb 2017 16:02:15 +0100 Subject: [PATCH 15/33] Resource module style --- .../MSFT_xVirtualMemory.psm1 | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 index a96fee6f..f970164d 100644 --- a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 +++ b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 @@ -22,25 +22,25 @@ function Get-TargetResource { MaximumSize = 0 } - [bool]$isSystemManaged = (Get-CimInstance -ClassName Win32_ComputerSystem).AutomaticManagedPagefile + [bool] $isSystemManaged = (Get-CimInstance -ClassName Win32_ComputerSystem).AutomaticManagedPagefile - if($isSystemManaged) { + if ($isSystemManaged) { $returnValue.Type = 'AutoManagePagingFile' return $returnValue } - $driveItem = [System.IO.DriveInfo]$Drive + $driveItem = [System.IO.DriveInfo] $Drive Write-Verbose "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))'" $virtualMemoryInstance = Get-CimInstance -Namespace root\cimv2 -Query "Select * from Win32_PageFileSetting where SettingID='pagefile.sys @ $($driveItem.Name.Substring(0,2))'" - if(-not $virtualMemoryInstance) { + if (-not $virtualMemoryInstance) { $returnValue.Type = 'NoPagingFile' return $returnValue } - if($virtualMemoryInstance.InitialSize -eq 0 -and $virtualMemoryInstance.MaximumSize -eq 0) { + if ($virtualMemoryInstance.InitialSize -eq 0 -and $virtualMemoryInstance.MaximumSize -eq 0) { $returnValue.Type = 'SystemManagedSize' } else { @@ -94,7 +94,7 @@ function Set-TargetResource { break } "CustomSize" { - if($SystemInfo.AutomaticManagedPageFile) { + if ($SystemInfo.AutomaticManagedPageFile) { $setParams = @{ Namespace = 'root\cimv2' @@ -107,8 +107,8 @@ function Set-TargetResource { Set-CimInstance @setParams } - $driveInfo = [System.IO.DriveInfo]$Drive - if(-not $driveInfo.IsReady) { + $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" } @@ -116,7 +116,7 @@ function Set-TargetResource { Write-Verbose ('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) { + if (-not $existingPageFileSetting) { [void] (New-CimInstance -Namespace 'root\cimv2' -ClassName 'Win32_PageFileSetting' -Property @{Name = $pageFileName}) } @@ -135,7 +135,7 @@ function Set-TargetResource { break } "SystemManagedSize" { - if($SystemInfo.AutomaticManagedPageFile) { + if ($SystemInfo.AutomaticManagedPageFile) { $setParams = @{ Namespace = 'root\cimv2' Query = 'Select * from Win32_ComputerSystem' @@ -147,8 +147,8 @@ function Set-TargetResource { Set-CimInstance @setParams } - $driveInfo = [System.IO.DriveInfo]$Drive - if(-not $driveInfo.IsReady) { + $driveInfo = [System.IO.DriveInfo] $Drive + if (-not $driveInfo.IsReady) { Write-Error "Drive $($driveInfo.Name) is not ready. Please ensure that the drive exists and is available" -TargetObject $driveInfo } @@ -157,7 +157,7 @@ function Set-TargetResource { Write-Verbose ('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) { + if (-not $existingPageFileSetting) { [void] (New-CimInstance -Namespace 'root\cimv2' -ClassName 'Win32_PageFileSetting' -Property @{Name = $pageFileName}) } @@ -178,7 +178,7 @@ function Set-TargetResource { break } "NoPagingFile" { - if($SystemInfo.AutomaticManagedPageFile) { + if ($SystemInfo.AutomaticManagedPageFile) { $setParams = @{ Namespace = 'root\cimv2' Query = 'Select * from Win32_ComputerSystem' @@ -188,15 +188,15 @@ function Set-TargetResource { Set-CimInstance @setParams } - $driveInfo = [System.IO.DriveInfo]$Drive - if(-not $driveInfo.IsReady) { + $driveInfo = [System.IO.DriveInfo] $Drive + if (-not $driveInfo.IsReady) { Write-Error "Drive $($driveInfo.Name) is not ready. Please ensure that the drive exists and is available" -TargetObject $driveInfo } $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) { + if ($existingPageFileSetting) { Write-Verbose "Removing existing page file $($existingPageFileSetting.Name)" Remove-CimInstance -InputObject $existingPageFileSetting $global:DSCMachineStatus = 1 @@ -242,19 +242,19 @@ function Test-TargetResource { break } "CustomSize" { - if($SystemInfo.AutomaticManagedPageFile) { + if ($SystemInfo.AutomaticManagedPageFile) { $result = $false break } - $driveInfo = [System.IO.DriveInfo]$Drive + $driveInfo = [System.IO.DriveInfo] $Drive $PageFile = Get-CimInstance -Class Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'" - if(-not $PageFile) { + if (-not $PageFile) { $result = $false break } - if(-not ($PageFile.InitialSize -eq $InitialSize -and $PageFile.MaximumSize -eq $MaximumSize)) { + if (-not ($PageFile.InitialSize -eq $InitialSize -and $PageFile.MaximumSize -eq $MaximumSize)) { $result = $false break } @@ -263,19 +263,19 @@ function Test-TargetResource { break } "SystemManagedSize" { - if($SystemInfo.AutomaticManagedPageFile) { + if ($SystemInfo.AutomaticManagedPageFile) { $result = $false break } - $driveInfo = [System.IO.DriveInfo]$Drive + $driveInfo = [System.IO.DriveInfo] $Drive $PageFile = Get-CimInstance -Class Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'" - if(-not $PageFile) { + if (-not $PageFile) { $result = $false break } - if(-not ($PageFile.InitialSize -eq 0 -and $PageFile.MaximumSize -eq 0)) { + if (-not ($PageFile.InitialSize -eq 0 -and $PageFile.MaximumSize -eq 0)) { $result = $false break } @@ -284,15 +284,15 @@ function Test-TargetResource { break } "NoPagingFile" { - if($SystemInfo.AutomaticManagedPageFile) { + if ($SystemInfo.AutomaticManagedPageFile) { $result = $false break } - $driveInfo = [System.IO.DriveInfo]$Drive + $driveInfo = [System.IO.DriveInfo] $Drive $PageFile = Get-CimInstance -Class Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'" - if($PageFile) { + if ($PageFile) { $result = $false break } From 60348ba161829f4b5f6a6985b03cbc09d3d75ae9 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Mon, 27 Feb 2017 16:09:46 +0100 Subject: [PATCH 16/33] Further styling, added comments where necessary --- .../MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 index f970164d..331be3f2 100644 --- a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 +++ b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 @@ -33,6 +33,7 @@ function Get-TargetResource { Write-Verbose "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) { @@ -95,6 +96,7 @@ function Set-TargetResource { } "CustomSize" { if ($SystemInfo.AutomaticManagedPageFile) { + # First set AutomaticManagedPageFile to $false to be able to set a custom one later $setParams = @{ Namespace = 'root\cimv2' @@ -120,6 +122,10 @@ function Set-TargetResource { [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))'" @@ -129,7 +135,8 @@ function Set-TargetResource { } } - Write-Verbose "Setting page file to $pageFileName. Initial size $InitialSize MB, maximum size $MaximumSize MB" + Write-Verbose ("Setting page file to {0}. Initial size {1}MB, maximum size {3}MB" -f $pageFileName, $InitialSize, $MaximumSize) + Set-CimInstance @setParams $global:DSCMachineStatus = 1 break @@ -149,7 +156,7 @@ function Set-TargetResource { $driveInfo = [System.IO.DriveInfo] $Drive if (-not $driveInfo.IsReady) { - Write-Error "Drive $($driveInfo.Name) is not ready. Please ensure that the drive exists and is available" -TargetObject $driveInfo + throw "Drive $($driveInfo.Name) is not ready. Please ensure that the drive exists and is available" } $pageFileName = Join-Path $driveInfo.Name 'pagefile.sys' @@ -190,7 +197,7 @@ function Set-TargetResource { $driveInfo = [System.IO.DriveInfo] $Drive if (-not $driveInfo.IsReady) { - Write-Error "Drive $($driveInfo.Name) is not ready. Please ensure that the drive exists and is available" -TargetObject $driveInfo + 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))'" @@ -233,6 +240,7 @@ function Test-TargetResource { [System.Int64] $MaximumSize ) + $SystemInfo = Get-CimInstance -Class Win32_ComputerSystem $result = $false @@ -308,6 +316,4 @@ function Test-TargetResource { $result } - Export-ModuleMember -Function *-TargetResource - From 203e1bc4641277c270e34d64c559928f8d542d30 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Mon, 27 Feb 2017 16:18:34 +0100 Subject: [PATCH 17/33] Issue in string format (could not count to 2) fixed --- DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 index 331be3f2..8944b0e2 100644 --- a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 +++ b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 @@ -135,7 +135,7 @@ function Set-TargetResource { } } - Write-Verbose ("Setting page file to {0}. Initial size {1}MB, maximum size {3}MB" -f $pageFileName, $InitialSize, $MaximumSize) + Write-Verbose ("Setting page file to {0}. Initial size {1}MB, maximum size {2}MB" -f $pageFileName, $InitialSize, $MaximumSize) Set-CimInstance @setParams $global:DSCMachineStatus = 1 From 9410687f3983ef98160958bd7a847a7c773581fc Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Mon, 27 Feb 2017 16:19:09 +0100 Subject: [PATCH 18/33] Removed stray comment from unit test --- Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 b/Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 index 46e55824..a37751f0 100644 --- a/Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 +++ b/Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 @@ -99,7 +99,7 @@ try { It 'Should call the mocked function Set-CimInstance exactly once' { Set-TargetResource @testParameters - Assert-MockCalled -CommandName Set-CimInstance -Exactly -Times 1 -Scope It #-ModuleName $script:DSCResourceName -Verbose + Assert-MockCalled -CommandName Set-CimInstance -Exactly -Times 1 -Scope It } } From 7ceda0074d42088dbb8b13bec14a3c748c179e90 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Mon, 27 Feb 2017 16:27:03 +0100 Subject: [PATCH 19/33] Added sample script --- Examples/Sample_xVirtualMemory.ps1 | 31 ++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Examples/Sample_xVirtualMemory.ps1 diff --git a/Examples/Sample_xVirtualMemory.ps1 b/Examples/Sample_xVirtualMemory.ps1 new file mode 100644 index 00000000..de7637f1 --- /dev/null +++ b/Examples/Sample_xVirtualMemory.ps1 @@ -0,0 +1,31 @@ +<# +.SYNOPSIS + Example to set the paging file +.DESCRIPTION + Example script that sets the paging file to reside on drive C with the custom size 2048MB +#> +configuration Sample_xVirtualMemory +{ + param + ( + [Parameter()] + [String[]] + $NodeName = 'localhost' + ) + + Import-DSCResource -ModuleName xComputerManagement + + node $NodeName + { + xVirtualMemory pagingSettings + { + Type = "CustomSize" + Drive = "C" + InitialSize = "2048" + MaximumSize = "2048" + } + } +} + +Sample_xVirtualMemory +Start-DscConfiguration -Path Sample_xVirtualMemory -Wait -verbose -Force \ No newline at end of file From c3d335a94ade71f432268eb1f171f157e0418fa4 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Tue, 28 Feb 2017 14:19:37 +0100 Subject: [PATCH 20/33] Missing newlines added to sample and config data --- Examples/Sample_xVirtualMemory.ps1 | 2 +- Tests/Integration/MSFT_xVirtualMemory.Config.ps1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Examples/Sample_xVirtualMemory.ps1 b/Examples/Sample_xVirtualMemory.ps1 index de7637f1..33d14451 100644 --- a/Examples/Sample_xVirtualMemory.ps1 +++ b/Examples/Sample_xVirtualMemory.ps1 @@ -28,4 +28,4 @@ configuration Sample_xVirtualMemory } Sample_xVirtualMemory -Start-DscConfiguration -Path Sample_xVirtualMemory -Wait -verbose -Force \ No newline at end of file +Start-DscConfiguration -Path Sample_xVirtualMemory -Wait -verbose -Force diff --git a/Tests/Integration/MSFT_xVirtualMemory.Config.ps1 b/Tests/Integration/MSFT_xVirtualMemory.Config.ps1 index 74248787..8edd95d8 100644 --- a/Tests/Integration/MSFT_xVirtualMemory.Config.ps1 +++ b/Tests/Integration/MSFT_xVirtualMemory.Config.ps1 @@ -46,4 +46,4 @@ Configuration setToNone Drive = 'C' } } -} \ No newline at end of file +} From 2459fdd68913303214143f2b7f71817b317e3dd1 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Tue, 28 Feb 2017 14:33:17 +0100 Subject: [PATCH 21/33] Test case for virtual memory unit test modified --- Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 b/Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 index a37751f0..04d2e114 100644 --- a/Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 +++ b/Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 @@ -85,7 +85,7 @@ try { Context 'When the system is not in the desired state' { BeforeEach { $testParameters = @{ - Drive = 'D:' + Drive = 'C:' Type = 'CustomSize' InitialSize = 0 MaximumSize = 1337 From 2fcdc65d27377d2027789d2b746188ea92e1416b Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Tue, 28 Feb 2017 14:41:55 +0100 Subject: [PATCH 22/33] Had to modify MSFT_xComputer.Tests.ps1 because my tests were failing. Testcase checking the hashtable keys will now check them after sorting them properly since the order they were returned in during the automated appveyor tests was different than on a local system. --- Tests/Unit/MSFT_xComputer.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Unit/MSFT_xComputer.Tests.ps1 b/Tests/Unit/MSFT_xComputer.Tests.ps1 index b3258249..7ab4436d 100644 --- a/Tests/Unit/MSFT_xComputer.Tests.ps1 +++ b/Tests/Unit/MSFT_xComputer.Tests.ps1 @@ -130,7 +130,7 @@ try It 'Should return a hashtable containing Name, DomainName, JoinOU, CurrentOU, Credential, UnjoinCredential and WorkGroupName' { $Result = Get-TargetResource -Name $env:COMPUTERNAME $Result.GetType().Fullname | Should Be 'System.Collections.Hashtable' - $Result.Keys | Should Be @('Name', 'DomainName', 'JoinOU', 'CurrentOU', 'Credential', 'UnjoinCredential', 'WorkGroupName') + $Result.Keys | Sort-Object | Should Be @('CurrentOU', 'Credential','DomainName', 'JoinOU', 'Name', 'UnjoinCredential', 'WorkGroupName') } It 'Throws if name is to long' { {Get-TargetResource -Name "ThisNameIsTooLong"} | Should Throw From b4ed149e33b08ffec65f876352ae11137d5c4986 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Tue, 28 Feb 2017 14:49:27 +0100 Subject: [PATCH 23/33] MSFT_xComputer.Tests -->last commit did not honor the alphabet... --- Tests/Unit/MSFT_xComputer.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Unit/MSFT_xComputer.Tests.ps1 b/Tests/Unit/MSFT_xComputer.Tests.ps1 index 7ab4436d..89b7904e 100644 --- a/Tests/Unit/MSFT_xComputer.Tests.ps1 +++ b/Tests/Unit/MSFT_xComputer.Tests.ps1 @@ -130,7 +130,7 @@ try It 'Should return a hashtable containing Name, DomainName, JoinOU, CurrentOU, Credential, UnjoinCredential and WorkGroupName' { $Result = Get-TargetResource -Name $env:COMPUTERNAME $Result.GetType().Fullname | Should Be 'System.Collections.Hashtable' - $Result.Keys | Sort-Object | Should Be @('CurrentOU', 'Credential','DomainName', 'JoinOU', 'Name', 'UnjoinCredential', 'WorkGroupName') + $Result.Keys | Sort-Object | Should Be @('Credential', 'CurrentOU', 'DomainName', 'JoinOU', 'Name', 'UnjoinCredential', 'WorkGroupName') } It 'Throws if name is to long' { {Get-TargetResource -Name "ThisNameIsTooLong"} | Should Throw From 368c3400eb09f330979f64f785522433bada31f1 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Wed, 1 Mar 2017 09:42:01 +0100 Subject: [PATCH 24/33] xVirtualMemory.psm1 modified to incorporate style issues (curly braces on new line, positional parameters changed to named parameters) --- .../MSFT_xVirtualMemory.psm1 | 88 +++++++++++-------- 1 file changed, 53 insertions(+), 35 deletions(-) diff --git a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 index 8944b0e2..b9df1b24 100644 --- a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 +++ b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 @@ -13,7 +13,7 @@ function Get-TargetResource { $Type ) - Write-Verbose 'Getting current page file settings' + Write-Verbose -Message 'Getting current page file settings' $returnValue = @{ Drive = [string]::Empty @@ -24,27 +24,31 @@ function Get-TargetResource { [bool] $isSystemManaged = (Get-CimInstance -ClassName Win32_ComputerSystem).AutomaticManagedPagefile - if ($isSystemManaged) { + if ($isSystemManaged) + { $returnValue.Type = 'AutoManagePagingFile' return $returnValue } $driveItem = [System.IO.DriveInfo] $Drive - Write-Verbose "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))'" + 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))'" + $virtualMemoryInstance = Get-CimInstance -Namespace root\cimv2 -Query "Select * from Win32_PageFileSetting where SettingID='pagefile.sys @ $($driveItem.Name.Substring(0,2))'" - if (-not $virtualMemoryInstance) { + if (-not $virtualMemoryInstance) + { $returnValue.Type = 'NoPagingFile' return $returnValue } - if ($virtualMemoryInstance.InitialSize -eq 0 -and $virtualMemoryInstance.MaximumSize -eq 0) { + if ($virtualMemoryInstance.InitialSize -eq 0 -and $virtualMemoryInstance.MaximumSize -eq 0) + { $returnValue.Type = 'SystemManagedSize' } - else { + else + { $returnValue.Type = "CustomSize" } @@ -76,26 +80,30 @@ function Set-TargetResource { $MaximumSize ) - Write-Verbose 'Setting page file' + Write-Verbose -Message 'Setting page file' $SystemInfo = Get-CimInstance -Class Win32_ComputerSystem - switch($Type) { - "AutoManagePagingFile" { + switch($Type) + { + "AutoManagePagingFile" + { $setParams = @{ Namespace = 'root\cimv2' Query = 'Select * from Win32_ComputerSystem' Property = @{AutomaticManagedPageFile = $true} } - Write-Verbose 'Enabling AutoManagePagingFile' + Write-Verbose -Message 'Enabling AutoManagePagingFile' Set-CimInstance @setParams $global:DSCMachineStatus = 1 break } - "CustomSize" { - if ($SystemInfo.AutomaticManagedPageFile) { + "CustomSize" + { + if ($SystemInfo.AutomaticManagedPageFile) + { # First set AutomaticManagedPageFile to $false to be able to set a custom one later $setParams = @{ @@ -104,27 +112,29 @@ function Set-TargetResource { Property = @{AutomaticManagedPageFile = $false} } - Write-Verbose 'Disabling AutoManagePagingFile' + Write-Verbose -Message 'Disabling AutoManagePagingFile' Set-CimInstance @setParams } $driveInfo = [System.IO.DriveInfo] $Drive - if (-not $driveInfo.IsReady) { + if (-not $driveInfo.IsReady) + { throw "Drive $($driveInfo.Name) is not ready. Please ensure that the drive exists and is available" } - $pageFileName = Join-Path $driveInfo.Name 'pagefile.sys' + $pageFileName = Join-Path -Path $driveInfo.Name -ChildPath 'pagefile.sys' - Write-Verbose ('Checking if a paging file already exists at {0}' -f $pageFileName) + 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) { + 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 + 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' @@ -135,36 +145,40 @@ function Set-TargetResource { } } - Write-Verbose ("Setting page file to {0}. Initial size {1}MB, maximum size {2}MB" -f $pageFileName, $InitialSize, $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) { + "SystemManagedSize" + { + if ($SystemInfo.AutomaticManagedPageFile) + { $setParams = @{ Namespace = 'root\cimv2' Query = 'Select * from Win32_ComputerSystem' Property = @{AutomaticManagedPageFile = $false} } - Write-Verbose 'Disabling AutoManagePagingFile' + Write-Verbose -Message 'Disabling AutoManagePagingFile' Set-CimInstance @setParams } $driveInfo = [System.IO.DriveInfo] $Drive - if (-not $driveInfo.IsReady) { + if (-not $driveInfo.IsReady) + { throw "Drive $($driveInfo.Name) is not ready. Please ensure that the drive exists and is available" } - $pageFileName = Join-Path $driveInfo.Name 'pagefile.sys' + $pageFileName = Join-Path -Path $driveInfo.Name -ChildPath 'pagefile.sys' - Write-Verbose ('Checking if a paging file already exists at {0}' -f $pageFileName) + 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) { + if (-not $existingPageFileSetting) + { [void] (New-CimInstance -Namespace 'root\cimv2' -ClassName 'Win32_PageFileSetting' -Property @{Name = $pageFileName}) } @@ -178,14 +192,15 @@ function Set-TargetResource { } } - Write-Verbose "Enabling system-managed page file on $pageFileName" + Write-Verbose -Message "Enabling system-managed page file on $pageFileName" Set-CimInstance @setParams $global:DSCMachineStatus = 1 break } "NoPagingFile" { - if ($SystemInfo.AutomaticManagedPageFile) { + if ($SystemInfo.AutomaticManagedPageFile) + { $setParams = @{ Namespace = 'root\cimv2' Query = 'Select * from Win32_ComputerSystem' @@ -196,24 +211,27 @@ function Set-TargetResource { } $driveInfo = [System.IO.DriveInfo] $Drive - if (-not $driveInfo.IsReady) { + 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 "Removing existing page file $($existingPageFileSetting.Name)" + if ($existingPageFileSetting) + { + Write-Verbose -Message "Removing existing page file $($existingPageFileSetting.Name)" Remove-CimInstance -InputObject $existingPageFileSetting $global:DSCMachineStatus = 1 } - Write-Verbose "Disabled page file for drive $Drive" + Write-Verbose -Message "Disabled page file for drive $Drive" break } - default { + default + { throw "A wrong type '$Type' has been selected." } } From 15c45acab05b1c2334c27d57e64d3b8ecd825749 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Wed, 1 Mar 2017 09:43:23 +0100 Subject: [PATCH 25/33] Added descriptions to all resource properties --- .../MSFT_xVirtualMemory/MSFT_xVirtualMemory.schema.mof | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.schema.mof b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.schema.mof index f5992962..38651f9d 100644 --- a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.schema.mof +++ b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.schema.mof @@ -2,9 +2,9 @@ [ClassVersion("1.0.0.0"), FriendlyName("xVirtualMemory")] class MSFT_xVirtualMemory : OMI_BaseResource { - [Key] String Drive; - [Key, ValueMap{"AutoManagePagingFile","CustomSize","SystemManagedSize","NoPagingFile"}, Values{"AutoManagePagingFile","CustomSize","SystemManagedSize","NoPagingFile"}] String Type; - [Write] Sint64 InitialSize; - [Write] Sint64 MaximumSize; + [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; }; From 23e7c48b6387e509673335fc30e0d6d055c98f2d Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Wed, 1 Mar 2017 09:43:36 +0100 Subject: [PATCH 26/33] Styling changes in README.md implemented --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index f57aeb66..41170145 100644 --- a/README.md +++ b/README.md @@ -78,8 +78,10 @@ xPowerPlan resource has following properties: * Name: The name of the power plan to activate. ## xVirtualMemory + xVirtualMemory resource is used to set the properties of the paging file on the local computer. xVirtualMemory has the following properties: + * Type: The type of the paging settings, mandatory, out of "AutoManagePagingFile","CustomSize","SystemManagedSize","NoPagingFile" * Drive: The drive to enable paging on, mandatory. Ignored for "AutoManagePagingFile" * InitialSize: The initial size in MB of the paging file. Ignored for Type "AutoManagePagingFile" and "SystemManagedSize" From e96f6338374e4864f2d4e0a3c2a20111fd80d2c8 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Wed, 1 Mar 2017 09:43:57 +0100 Subject: [PATCH 27/33] Styling issues in MSFT_xVirtualMemory.Integration.Tests fixed --- .../MSFT_xVirtualMemory.Integration.Tests.ps1 | 32 +++++-------------- 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/Tests/Integration/MSFT_xVirtualMemory.Integration.Tests.ps1 b/Tests/Integration/MSFT_xVirtualMemory.Integration.Tests.ps1 index 672df139..b03eaa5c 100644 --- a/Tests/Integration/MSFT_xVirtualMemory.Integration.Tests.ps1 +++ b/Tests/Integration/MSFT_xVirtualMemory.Integration.Tests.ps1 @@ -1,17 +1,3 @@ -<# -.Synopsis - Template for creating DSC Resource Integration Tests -.DESCRIPTION - To Use: - 1. Copy to \Tests\Integration\ folder and rename .Integration.tests.ps1 (e.g. MSFT_xNeworking.Integration.tests.ps1) - 2. Customize TODO sections. - 3. Create test DSC Configurtion file .config.ps1 (e.g. MSFT_xNeworking.config.ps1) from integration_config_template.ps1 file. - -.NOTES - Code in HEADER, FOOTER and DEFAULT TEST regions are standard and may be moved into - DSCResource.Tools in Future and therefore should not be altered if possible. -#> - $script:DSCModuleName = 'xComputerManagement' $script:DSCResourceName = 'MSFT_xVirtualMemory' @@ -43,8 +29,8 @@ try Context "Set page file to automatically managed" { $CurrentConfig = "setToAuto" - $ConfigDir = (Join-Path $TestDrive $CurrentConfig) - $ConfigMof = (Join-Path $ConfigDir "localhost.mof") + $ConfigDir = (Join-Path -Path $TestDrive -ChildPath $CurrentConfig) + $ConfigMof = (Join-Path -Path $ConfigDir -ChildPath "localhost.mof") It "should compile a MOF file without error" { { @@ -65,8 +51,8 @@ try Context "Set page file to custom size" { $CurrentConfig = "setToCustom" - $ConfigDir = (Join-Path $TestDrive $CurrentConfig) - $ConfigMof = (Join-Path $ConfigDir "localhost.mof") + $ConfigDir = (Join-Path -Path $TestDrive -ChildPath $CurrentConfig) + $ConfigMof = (Join-Path -Path $ConfigDir -ChildPath "localhost.mof") It "should compile a MOF file without error" { { @@ -87,8 +73,8 @@ try Context "Set page file to system managed" { $CurrentConfig = "setToSystemManaged" - $ConfigDir = (Join-Path $TestDrive $CurrentConfig) - $ConfigMof = (Join-Path $ConfigDir "localhost.mof") + $ConfigDir = (Join-Path -Path $TestDrive -ChildPath $CurrentConfig) + $ConfigMof = (Join-Path -Path $ConfigDir -ChildPath "localhost.mof") It "should compile a MOF file without error" { { @@ -109,8 +95,8 @@ try Context "Set page file to none" { $CurrentConfig = "setToNone" - $ConfigDir = (Join-Path $TestDrive $CurrentConfig) - $ConfigMof = (Join-Path $ConfigDir "localhost.mof") + $ConfigDir = (Join-Path -Path $TestDrive -ChildPath $CurrentConfig) + $ConfigMof = (Join-Path -Path $ConfigDir -ChildPath "localhost.mof") It "should compile a MOF file without error" { { @@ -137,7 +123,5 @@ finally Restore-TestEnvironment -TestEnvironment $TestEnvironment #endregion - - # TODO: Other Optional Cleanup Code Goes Here... } From 4edceda766dcbf9eb174e09d5eac858bd8062279 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Wed, 1 Mar 2017 09:45:23 +0100 Subject: [PATCH 28/33] Styling issues in MSFT_xVirtualMemory.Tests.ps1 fixed --- Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 | 67 +++++++++++++----------- 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 b/Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 index 04d2e114..a512453b 100644 --- a/Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 +++ b/Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 @@ -43,9 +43,7 @@ try { Context 'When the system is in the desired present state' { BeforeEach { Mock -CommandName Get-CimInstance -MockWith { - return New-Object Object | - Add-Member -MemberType NoteProperty -Name AutomaticManagedPagefile -Value $false -PassThru -Force | - Add-Member -MemberType NoteProperty -Name Name -Value "D:\pagefile.sys" -PassThru -Force + [PSObject] @{ AutomaticManagedPageFile = $false; Name = 'D:\pagefile.sys' } } -ModuleName $script:DSCResourceName -Verifiable } @@ -59,10 +57,11 @@ try { Context 'When the system is not in the desired present state' { BeforeEach { Mock -CommandName Get-CimInstance -MockWith { - return New-Object Object | - Add-Member -MemberType NoteProperty -Name InitialSize -Value 0 -PassThru -Force | - Add-Member -MemberType NoteProperty -Name MaximumSize -Value 0 -PassThru -Force | - Add-Member -MemberType NoteProperty -Name Name -Value "C:\pagefile.sys" -PassThru -Force + [PSObject] @{ + InitialSize = 0 + MaximumSize = 0 + Name = "C:\pagefile.sys" + } } -ModuleName $script:DSCResourceName -Verifiable } @@ -109,11 +108,12 @@ try { Mock -CommandName New-CimInstance -MockWith {} -ModuleName $script:DSCResourceName -Verifiable Mock -CommandName Remove-CimInstance -MockWith {} -ModuleName $script:DSCResourceName -Verifiable Mock -CommandName Get-CimInstance -MockWith { - New-Object psobject | - Add-Member -MemberType NoteProperty -Name InitialSize -Value 0 -PassThru -Force | - Add-Member -MemberType NoteProperty -Name MaximumSize -Value 1338 -PassThru -Force | - Add-Member -MemberType NoteProperty -Name Name -Value "D:\pagefile.sys" -PassThru -Force | - Add-Member -MemberType NoteProperty -Name AutomaticManagedPageFile -Value $false -PassThru -Force + [PSObject] @{ + InitialSize = 0 + MaximumSize = 1338 + Name = "D:\pagefile.sys" + AutomaticManagedPageFile = $false + } } $testParameters = @{ @@ -122,7 +122,9 @@ try { InitialSize = 0 MaximumSize = 1337 } - It 'Should throw if no valid drive letter has been used' { { Set-TargetResource @testParameters } | Should Throw + It 'Should throw if no valid drive letter has been used' + { + { Set-TargetResource @testParameters } | Should Throw } $testParameters = @{ @@ -131,7 +133,9 @@ try { InitialSize = 0 MaximumSize = 1337 } - It 'Should throw if the drive is not ready' { { Set-TargetResource @testParameters } | Should Throw + It 'Should throw if the drive is not ready' + { + { Set-TargetResource @testParameters } | Should Throw } } @@ -147,41 +151,42 @@ try { InitialSize = 0 MaximumSize = 1337 } - } + } + + $pageFileObject = [PSObject] @{ + InitialSize = 0 + MaximumSize = 1338 + Name = "D:\pagefile.sys" + AutomaticManagedPageFile = $false + } + Mock -CommandName Get-CimInstance -MockWith { - New-Object psobject | - Add-Member -MemberType NoteProperty -Name InitialSize -Value 0 -PassThru -Force | - Add-Member -MemberType NoteProperty -Name MaximumSize -Value 1337 -PassThru -Force | - Add-Member -MemberType NoteProperty -Name Name -Value "D:\pagefile.sys" -PassThru -Force | - Add-Member -MemberType NoteProperty -Name AutomaticManagedPageFile -Value $false -PassThru -Force + $pageFileObject.MaximumSize = 1337 + $pageFileObject } It 'Should return True if the input matches the actual values' { Test-TargetResource @testParameters | Should Be $true } Mock -CommandName Get-CimInstance -MockWith { - New-Object psobject | - Add-Member -MemberType NoteProperty -Name InitialSize -Value 0 -PassThru -Force | - Add-Member -MemberType NoteProperty -Name MaximumSize -Value 1337 -PassThru -Force | - Add-Member -MemberType NoteProperty -Name Name -Value "D:\pagefile.sys" -PassThru -Force | - Add-Member -MemberType NoteProperty -Name AutomaticManagedPageFile -Value $true -PassThru -Force + $pageFileObject.MaximumSize = 1337 + $pageFileObject.AutomaticManagedPageFile = $true + $pageFileObject } It 'Should return False if the type is wrong' { Test-TargetResource @testParameters | Should Be $false } Mock -CommandName Get-CimInstance -MockWith { - New-Object psobject | - Add-Member -MemberType NoteProperty -Name InitialSize -Value 0 -PassThru -Force | - Add-Member -MemberType NoteProperty -Name MaximumSize -Value 1338 -PassThru -Force | - Add-Member -MemberType NoteProperty -Name Name -Value "D:\pagefile.sys" -PassThru -Force | - Add-Member -MemberType NoteProperty -Name AutomaticManagedPageFile -Value $false -PassThru -Force + $pageFileObject.MaximumSize = 1338 + $pageFileObject } It 'Should return False if InitialSize and/or MaximumSize do not match' { Test-TargetResource @testParameters | Should Be $false } - Mock -CommandName Get-CimInstance -MockWith { # In this case Get-CimInstance returns an empty object + Mock -CommandName Get-CimInstance -MockWith { + # In this case Get-CimInstance returns an empty object } It 'Should return False if Name does not match' { Test-TargetResource @testParameters | Should Be $false From 67b96b3a34abbfaf1fbcbd0b73d805c70494c211 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Wed, 1 Mar 2017 09:52:41 +0100 Subject: [PATCH 29/33] AppVeyor test for Exceptions was failing. Moved curly braces one line up --- Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 b/Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 index a512453b..d4e55081 100644 --- a/Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 +++ b/Tests/Unit/MSFT_xVirtualMemory.Tests.ps1 @@ -122,8 +122,7 @@ try { InitialSize = 0 MaximumSize = 1337 } - It 'Should throw if no valid drive letter has been used' - { + It 'Should throw if no valid drive letter has been used' { { Set-TargetResource @testParameters } | Should Throw } @@ -133,8 +132,7 @@ try { InitialSize = 0 MaximumSize = 1337 } - It 'Should throw if the drive is not ready' - { + It 'Should throw if the drive is not ready' { { Set-TargetResource @testParameters } | Should Throw } } From 46f7b68c98b3d2c160069a7292c4feece45e72ac Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Wed, 22 Mar 2017 08:38:09 +0100 Subject: [PATCH 30/33] Formatting --- .../MSFT_xVirtualMemory.psm1 | 64 ++++++++++++------- 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 index b9df1b24..d6c288d9 100644 --- a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 +++ b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 @@ -1,4 +1,5 @@ -function Get-TargetResource { +function Get-TargetResource +{ [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param @@ -7,7 +8,7 @@ function Get-TargetResource { [System.String] $Drive, - [ValidateSet("AutoManagePagingFile","CustomSize","SystemManagedSize","NoPagingFile")] + [ValidateSet("AutoManagePagingFile", "CustomSize", "SystemManagedSize", "NoPagingFile")] [parameter(Mandatory = $true)] [System.String] $Type @@ -52,7 +53,7 @@ function Get-TargetResource { $returnValue.Type = "CustomSize" } - $returnValue.Drive = $virtualMemoryInstance.Name.Substring(0,3) + $returnValue.Drive = $virtualMemoryInstance.Name.Substring(0, 3) $returnValue.InitialSize = $virtualMemoryInstance.InitialSize $returnValue.MaximumSize = $virtualMemoryInstance.MaximumSize @@ -60,7 +61,8 @@ function Get-TargetResource { } -function Set-TargetResource { +function Set-TargetResource +{ [CmdletBinding()] param ( @@ -68,7 +70,7 @@ function Set-TargetResource { [System.String] $Drive, - [ValidateSet("AutoManagePagingFile","CustomSize","SystemManagedSize","NoPagingFile")] + [ValidateSet("AutoManagePagingFile", "CustomSize", "SystemManagedSize", "NoPagingFile")] [parameter(Mandatory = $true)] [System.String] $Type, @@ -84,7 +86,7 @@ function Set-TargetResource { $SystemInfo = Get-CimInstance -Class Win32_ComputerSystem - switch($Type) + switch ($Type) { "AutoManagePagingFile" { @@ -198,7 +200,8 @@ function Set-TargetResource { $global:DSCMachineStatus = 1 break } - "NoPagingFile" { + "NoPagingFile" + { if ($SystemInfo.AutomaticManagedPageFile) { $setParams = @{ @@ -238,7 +241,8 @@ function Set-TargetResource { } -function Test-TargetResource { +function Test-TargetResource +{ [CmdletBinding()] [OutputType([System.Boolean])] param @@ -247,7 +251,7 @@ function Test-TargetResource { [System.String] $Drive, - [ValidateSet("AutoManagePagingFile","CustomSize","SystemManagedSize","NoPagingFile")] + [ValidateSet("AutoManagePagingFile", "CustomSize", "SystemManagedSize", "NoPagingFile")] [parameter(Mandatory = $true)] [System.String] $Type, @@ -262,25 +266,31 @@ function Test-TargetResource { $SystemInfo = Get-CimInstance -Class Win32_ComputerSystem $result = $false - switch($Type) { - "AutoManagePagingFile" { + switch ($Type) + { + "AutoManagePagingFile" + { $result = $SystemInfo.AutomaticManagedPagefile break } - "CustomSize" { - if ($SystemInfo.AutomaticManagedPageFile) { + "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) { + if (-not $PageFile) + { $result = $false break } - if (-not ($PageFile.InitialSize -eq $InitialSize -and $PageFile.MaximumSize -eq $MaximumSize)) { + if (-not ($PageFile.InitialSize -eq $InitialSize -and $PageFile.MaximumSize -eq $MaximumSize)) + { $result = $false break } @@ -288,20 +298,24 @@ function Test-TargetResource { $result = $true break } - "SystemManagedSize" { - if ($SystemInfo.AutomaticManagedPageFile) { + "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) { + if (-not $PageFile) + { $result = $false break } - if (-not ($PageFile.InitialSize -eq 0 -and $PageFile.MaximumSize -eq 0)) { + if (-not ($PageFile.InitialSize -eq 0 -and $PageFile.MaximumSize -eq 0)) + { $result = $false break } @@ -309,8 +323,10 @@ function Test-TargetResource { $result = $true break } - "NoPagingFile" { - if ($SystemInfo.AutomaticManagedPageFile) { + "NoPagingFile" + { + if ($SystemInfo.AutomaticManagedPageFile) + { $result = $false break } @@ -318,7 +334,8 @@ function Test-TargetResource { $driveInfo = [System.IO.DriveInfo] $Drive $PageFile = Get-CimInstance -Class Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveInfo.Name.Substring(0,2))'" - if ($PageFile) { + if ($PageFile) + { $result = $false break } @@ -326,7 +343,8 @@ function Test-TargetResource { $result = $true break } - default { + default + { break } } From 4ea3cb59d851954b71ed84aa6467320ef6f1aefc Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Wed, 22 Mar 2017 08:41:44 +0100 Subject: [PATCH 31/33] Formatting --- DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 | 2 -- 1 file changed, 2 deletions(-) diff --git a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 index d6c288d9..1053e8d4 100644 --- a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 +++ b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 @@ -60,7 +60,6 @@ function Get-TargetResource $returnValue } - function Set-TargetResource { [CmdletBinding()] @@ -240,7 +239,6 @@ function Set-TargetResource } } - function Test-TargetResource { [CmdletBinding()] From b14817a8e93d065bad1ef8b0edfe144530529873 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Peters Date: Fri, 21 Apr 2017 10:21:09 +0200 Subject: [PATCH 32/33] Added comment-based help entries for Get/Set/Test --- .../MSFT_xVirtualMemory.psm1 | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 index 1053e8d4..e00a1b69 100644 --- a/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 +++ b/DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1 @@ -1,3 +1,11 @@ +<# +.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()] @@ -60,6 +68,18 @@ function Get-TargetResource $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()] @@ -239,6 +259,18 @@ function Set-TargetResource } } +<# +.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()] From 5f9fa567aa5774e96459a4fd64f93b7b2f82de5f Mon Sep 17 00:00:00 2001 From: Katie Keim Date: Wed, 31 May 2017 14:05:41 -0700 Subject: [PATCH 33/33] Releasing version 1.10.0.0 --- README.md | 2 ++ xComputerManagement.psd1 | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 41170145..5bb07b08 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,8 @@ xVirtualMemory has the following properties: ## Versions ### Unreleased + +### 1.10.0.0 * Added resources - xVirtualMemory diff --git a/xComputerManagement.psd1 b/xComputerManagement.psd1 index ea995901..5b4d796a 100644 --- a/xComputerManagement.psd1 +++ b/xComputerManagement.psd1 @@ -1,6 +1,6 @@ @{ # Version number of this module. -ModuleVersion = '1.9.0.0' +ModuleVersion = '1.10.0.0' # ID used to uniquely identify this module GUID = 'B5004952-489E-43EA-999C-F16A25355B89' @@ -50,7 +50,7 @@ PrivateData = @{ # ReleaseNotes of this module ReleaseNotes = '* Added resources - - xPowerPlan + - xVirtualMemory ' @@ -61,3 +61,4 @@ PrivateData = @{ +