From 02901aad433c6fe6bae8cb5b4077f0132767ca37 Mon Sep 17 00:00:00 2001 From: Dmitry Mashkov Date: Tue, 7 Jan 2020 22:32:32 +0400 Subject: [PATCH] 1. Added ContentEncoding property to properly support Content files (otherwise they are always written as Unicode) which corrupts some content types like UTF-8 XMLs. 2. Content files are not tested with ItemHasChanged (this didn't work anyway). 3. Fixed GetItemToCopy where an undefined DefaultValues array was used instead of DesiredProperties, resulting in a dead code, impacting properties like Force. 4. After #3 is fixed and the code is finally execuded, it would crash if a file is a Content (without SourcePath). 5. In SetVHDFile , Copy-Item's ErrorAction is set to Stop. Otherwise, the resource would simply ignore all errors and produce invalid configuration. --- .gitignore | 1 + .../MSFT_xVhdFileDirectory.psm1 | 23 +++++++++++-------- .../MSFT_xVhdFileDirectory.schema.mof | 1 + 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index b03606a..d9cc951 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ DSCResource.Tests +/.vs diff --git a/DSCResources/MSFT_xVhdFileDirectory/MSFT_xVhdFileDirectory.psm1 b/DSCResources/MSFT_xVhdFileDirectory/MSFT_xVhdFileDirectory.psm1 index 218d243..c3dbd7b 100644 --- a/DSCResources/MSFT_xVhdFileDirectory/MSFT_xVhdFileDirectory.psm1 +++ b/DSCResources/MSFT_xVhdFileDirectory/MSFT_xVhdFileDirectory.psm1 @@ -135,7 +135,7 @@ function Set-TargetResource } # Create file/folder scenario - SetVHDFile -destinationPath $finalDestinationPath -type $itemToCopy.Type -force:($itemToCopy.Force) -content $itemToCopy.Content + SetVHDFile -destinationPath $finalDestinationPath -type $itemToCopy.Type -force:($itemToCopy.Force) -content $itemToCopy.Content -contentEncoding $itemToCopy.ContentEncoding } # Set Attribute scenario @@ -213,6 +213,8 @@ function Test-TargetResource } else { + # Do not test for Content + if($null -eq $itemToCopy.SourcePath) {$result = $false; break;} $itemToCopyIsFile = Test-Path $itemToCopy.SourcePath -PathType Leaf $destinationIsFolder = Test-Path $finalDestinationPath -PathType Container @@ -332,6 +334,7 @@ function GetItemToCopy 'Recurse' = 'True' 'Force' = 'True' 'Content' = $null + 'ContentEncoding' = $null 'Attributes' = $null 'Type' = 'Directory' } @@ -340,20 +343,20 @@ function GetItemToCopy #Get Property Value $thisItem = $item.CimInstanceProperties[$_].Value - if (-not $thisItem -and $_ -in $DefaultValues.Keys) + if (-not $thisItem -and $_ -in $DesiredProperties.Keys) { #If unset and a default value is defined enter here if ($_ -eq 'Type') { #Special behavior for the Type property based on SourcePath #This relies on SourcePath preceeding Type in the list of keys (the reason for using OrderedDictionary) - if (Test-Path $returnValue.SourcePath -PathType Leaf ) + if (($null -eq $returnValue.SourcePath) -or (Test-Path $returnValue.SourcePath -PathType Leaf )) { #If the sourcepath resolves to a file, set the default to File instad of Directory - $DefaultValues.Type = 'File' + $DesiredProperties.Type = 'File' } } - $returnValue[$_] = $DefaultValues[$_] + $returnValue[$_] = $DesiredProperties[$_] } else { @@ -398,7 +401,9 @@ function SetVHDFile $type, [Parameter(ParameterSetName = "New")] $content, - [Parameter(Mandatory=$true)] + [Parameter(ParameterSetName = "New")] + $contentEncoding, + [Parameter(Mandatory=$true)] $destinationPath, [Parameter(Mandatory=$true,ParameterSetName = "Set")] $attribute, @@ -407,10 +412,11 @@ function SetVHDFile ) Write-Verbose "Setting the VHD file $($PSCmdlet.ParameterSetName)" + if ($PSCmdlet.ParameterSetName -eq 'Copy') { New-Item -Path (Split-Path $destinationPath) -ItemType Directory -ErrorAction SilentlyContinue - Copy-Item -Path $sourcePath -Destination $destinationPath -Force:$force -Recurse:$recurse -ErrorAction SilentlyContinue + Copy-Item -Path $sourcePath -Destination $destinationPath -Force:$force -Recurse:$recurse -ErrorAction Stop } elseif ($PSCmdlet.ParameterSetName -eq 'New') { @@ -420,8 +426,7 @@ function SetVHDFile } else { - New-Item -Path $destinationPath -ItemType $type - $content | Out-File $destinationPath + $content | Out-File $destinationPath -Encoding $contentEncoding } } diff --git a/DSCResources/MSFT_xVhdFileDirectory/MSFT_xVhdFileDirectory.schema.mof b/DSCResources/MSFT_xVhdFileDirectory/MSFT_xVhdFileDirectory.schema.mof index b221b4b..6e93294 100644 --- a/DSCResources/MSFT_xVhdFileDirectory/MSFT_xVhdFileDirectory.schema.mof +++ b/DSCResources/MSFT_xVhdFileDirectory/MSFT_xVhdFileDirectory.schema.mof @@ -9,6 +9,7 @@ Class MSFT_xFileDirectory [Write] boolean Recurse; [Write] boolean Force ; [write] string Content; + [write] string ContentEncoding; [Write,ValueMap{"ReadOnly", "Hidden", "System", "Archive"},Values{"ReadOnly", "Hidden", "System", "Archive"}] string Attributes[]; };