File resorce not working as expected

I hope this does not doublepost. Firefox is not likely the site today :frowning:

It was my understanding that if no Checksum is used the fact that DestinationPath exists stops the copying of the file. But that is not what it’s doing. it’s recopying the file every time the destination file is changed.

file BranchSrvBootDrive
Ensure = ‘Present’
SourcePath = “$VMHdPath$VHDFile”
DestinationPath = “$VMHdPath\BranchServer_Boot.vhdx”
Force = $false
Type = ‘file’
DependsOn = ‘[file]VMImage’

My workaround for now is this.

script BranchServerBootDrive
setScript = {
copy-item -Path “$VMHdPath$VHDFile” -Destination “$VMHdPath\BranchServer_Boot.vhdx” -ErrorAction SilentlyContinue
TestScript = {
Test-Path -Path “$VMHdPath\BranchServer_Boot.vhdx” -ErrorAction SilentlyContinue
GetScript = {
$Ensure = (Test-Path -Path “$VMHdPath\BranchServer_Boot.vhdx” -ErrorAction SilentlyContinue)
$output = @{Ensure = $Ensure
Path = “$VMHdPath\BranchServer_Boot.vhdx”}
DependsOn = ‘[file]VMImage’

Anyone know what I’m doing wrong?

It’s hard to say, because File is one of the few resources that’s implemented with binary code instead of PowerShell code. If I have time later, I’ll try to track down which binary implements this resource and see how it works.

Based on the TechNet documentation: if you don’t specify a value for the Checksum parameter, the File resource should be just using the file name to determine whether two files are the same. Is your $VHDFile variable set to ‘BranchServer_Boot.vhdx’, or something else? Maybe if you use different filenames for the source / destination, it causes the resource to see them as “different” every time it’s applied.

I think your workaround is on the right track. You don’t need to be downloading this file over and over again, except in situations where the whole VHD / VM needs to be rebuilt.

Edit: As a short-term workaround, assuming that $VHDFile is set to some name other than ‘BranchServer_Boot.vhdx’, you could possibly avoid this problem by setting up a folder structure where the files all have that name. This might make the File resource behave (which you can check.) You could also try setting the Checksum parameter to ‘CreatedDate’.