Remove an archive with SHA-256 file validation

Description

This example shows how you can use the Archive resource to ensure no contents of a .zip file are expanded to a specific directory.

With Ensure set to Absent, the Path set to C:\ExampleArchivePath\Archive.zip. and the Destination set to C:\ExampleDestinationPath\Destination, the resource removes the contents of Archive.zip from the Destination folder if they exist.

With Validate set to $true and Checksum set to SHA-256, the resource compares the SHA256 checksum of every that exists in both the Destination folder and Archive.zip. If the checksum for any file in the Destination folder matches the checksum of that file in Archive.zip, the resource is out of the desired state. The resource removes those matching files when its Set method runs. It won't remove any other files.

With Invoke-DscResource

This script shows how you can use the Archive resource with the Invoke-DscResource cmdlet to ensure no contents in Archive.zip exist in the Destination folder with SHA256 checksum validation.

[CmdletBinding()]
param()

begin {
    $SharedParameters = @{
        Name       = 'Archive'
        ModuleName = 'PSDscResource'
        Properties = @{
            Path        = 'C:\ExampleArchivePath\Archive.zip'
            Destination = 'C:\ExampleDestinationPath\Destination'
            Validate    = $true
            Checksum    = 'SHA-256'
            Ensure      = 'Absent'
        }
    }

    $NonGetProperties = @(
        'Ensure'
        'Validate'
        'Checksum'
    )
}

process {
    $TestResult = Invoke-DscResource -Method Test @SharedParameters

    if ($TestResult.InDesiredState) {
        $QueryParameters = $SharedParameters.Clone()

        foreach ($Property in $NonGetProperties) {
            $QueryParameters.Properties.Remove($Property)
        }

        Invoke-DscResource -Method Get @QueryParameters
    } else {
        Invoke-DscResource -Method Set @SharedParameters
    }
}

With a Configuration

This snippet shows how you can define a Configuration with an Archive resource block to ensure no contents in Archive.zip exist in the Destination folder with SHA256 checksum validation.

Configuration RemoveArchiveChecksum {
    Import-DscResource -ModuleName 'PSDscResources'

    Node localhost {
        Archive ExampleArchive {
            Path        = 'C:\ExampleArchivePath\Archive.zip'
            Destination = 'C:\ExampleDestinationPath\Destination'
            Validate    = $true
            Checksum    = 'SHA-256'
            Ensure      = 'Absent'
        }
    }
}