VhdFile: Test-TargetResource is always $false for directories
Details of the scenario you tried and the problem that is occurring
Try to include a 'Directory' resource like MSFT_xFileDirectory { SourcePath = 'C:\Program Files\WindowsPowerShell\Modules\xActiveDirectory' DestinationPath = "\Program Files\WindowsPowerShell\Modules" type = 'Directory' Recurse = $True Ensure='Present' } The subtree copies correctly. Then run the Test-DscConfiguration or wait for the Monitor action on complete. There's a false result. The root cause is that LastWriteTime for the directory is the time of last file operation within the directory, meaning it will never match the timestamp of original directory. SHA-* algorithm also not applicable.
Verbose logs showing the problem
VERBOSE: [DESKTOP-1NHIAMI]: [[xVhdFile]Inject DC1] SourcePath => C:\Program
Files\WindowsPowerShell\Modules\xActiveDirectory
VERBOSE: [DESKTOP-1NHIAMI]: [[xVhdFile]Inject DC1] Type => Directory
VERBOSE: [DESKTOP-1NHIAMI]: [[xVhdFile]Inject DC1] Ensure => True
VERBOSE: [DESKTOP-1NHIAMI]: [[xVhdFile]Inject DC1] Content =>
VERBOSE: [DESKTOP-1NHIAMI]: [[xVhdFile]Inject DC1] DestinationPath => \Program
Files\WindowsPowerShell\Modules
VERBOSE: [DESKTOP-1NHIAMI]: [[xVhdFile]Inject DC1] Force => False
VERBOSE: [DESKTOP-1NHIAMI]: [[xVhdFile]Inject DC1] Attributes =>
VERBOSE: [DESKTOP-1NHIAMI]: [[xVhdFile]Inject DC1] Recurse => True
VERBOSE: [DESKTOP-1NHIAMI]: [[xVhdFile]Inject DC1] Testing the file with relative VHD
destination \Program Files\WindowsPowerShell\Modules
VERBOSE: [DESKTOP-1NHIAMI]: [[xVhdFile]Inject DC1] Test returned False
VERBOSE: [DESKTOP-1NHIAMI]: LCM: [ End Test ] [[xVhdFile]Inject DC1] False in 1.2560 seconds.
Suggested solution to the issue
Rewrite ItemHasChanged to separately process directories and files.
The DSC configuration that is used to reproduce the issue (as detailed as possible)
Configuration LabDCs
{
Import-DscResource –ModuleName 'PSDesiredStateConfiguration'
Import-DscResource -module xHyper-V
Import-DscResource -ModuleName 'xPSDesiredStateConfiguration'
xVhdFile "Inject"
{
VhdPath = "C:\temp\OSDisk.vhdx"
FileDirectory = @(
# xActiveDirectory
MSFT_xFileDirectory {
SourcePath = 'C:\Program Files\WindowsPowerShell\Modules\xActiveDirectory\'
DestinationPath = "\Program Files\WindowsPowerShell\Modules\"
type = 'Directory'
Recurse = $True
Ensure='Present'
}
)
DependsOn = "[xVhd]$($DCName[0])"
}
}
}
The operating system the target node is running
OsName : Microsoft Windows 10 Enterprise OsOperatingSystemSKU : EnterpriseEdition OsArchitecture : 64-bit WindowsVersion : 2004 WindowsBuildLabEx : 19037.1.amd64fre.vb_release.191202-1231 OsLanguage : en-US OsMuiLanguages : {en-US, ru-RU}
Version and build of PowerShell the target node is running
Name Value
PSVersion 5.1.19037.1 PSEdition Desktop PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} BuildVersion 10.0.19037.1 CLRVersion 4.0.30319.42000 WSManStackVersion 3.0 PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1
Version of the DSC module that was used ('dev' if using current dev branch)
3.17.0.0
@DmitryMashkov-DM thanks for opening this issue. I have confirmed the issue really stems from using Copy-Item since it doesn't copy the metadata associated for the item. The way I'd like to proceed is replace the use of Copy-Item with robocopy.
@johlju I see that SqlServerDsc already has a helper function that implements robocopy: https://github.com/dsccommunity/SqlServerDsc/blob/master/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1. Would we be able to put this helper function in a more central location so that any DSC module could benefit from its use? Maybe here https://github.com/dsccommunity/DscResource.Common/tree/master/source/Public?
Absolutely! That would be great to have it there. Then we can reuse it in SqlServerDsc from there.