Expand an archive with default file validation and file overwrite allowed

Description

This example shows how you can use the Archive resource to ensure a .zip file is expanded to a specific directory and the expanded contents match the contents in the .zip file.

With Ensure set to Present, the Path set to C:\ExampleArchivePath\Archive.zip, and the Destination set to C:\ExampleDestinationPath\Destination, the resource expands the contents of Archive.zip to the Destination folder if they're not already there.

With Validate set to $true and Checksum not set, the resource compares the LastWriteTime property of every expanded file against the LastWriteTime property of the relevant file in Archive.zip. If the values for any content in the Destination folder don't match the value in Archive.zip, the resource is out of the desired state.

With Force set to $true, the resource overwrites any expanded files with an incorrect LastWriteTime. If Force was set to $false, the resource would throw an exception instead of overwriting the files.

With Invoke-DscResource

This script shows how you can use the Archive resource with the Invoke-DscResource cmdlet to ensure Archive.zip is expanded to the Destination folder with default validation.

[CmdletBinding()]
param()

begin {
    $SharedParameters = @{
        Name       = 'Archive'
        ModuleName = 'PSDscResource'
        Properties = @{
            Path        = 'C:\ExampleArchivePath\Archive.zip'
            Destination = 'C:\ExampleDestinationPath\Destination'
            Validate    = $true
            Force       = $true
            Ensure      = 'Present'
        }
    }

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

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 Archive.zip is expanded to the Destination folder with default validation.

Configuration ExpandArchiveDefaultValidationAndForce {
    Import-DscResource -ModuleName 'PSDscResources'

    Node localhost {
        Archive ExampleArchive {
            Path        = 'C:\ExampleArchivePath\Archive.zip'
            Destination = 'C:\ExampleDestinationPath\Destination'
            Validate    = $true
            Force       = $true
            Ensure      = 'Present'
        }
    }
}