Delen via


Een archief uitbreiden met standaardbestandvalidatie en toegestaan bestand overschrijven

Description

In dit voorbeeld ziet u hoe u de Archive resource kunt gebruiken om ervoor te zorgen dat een .zip bestand wordt uitgebreid naar een specifieke map en dat de uitgebreide inhoud overeenkomt met de inhoud in het .zip bestand.

Met Ensure ingesteld op Present, het pad ingesteld op C:\ExampleArchivePath\Archive.zipen de doel ingesteld C:\ExampleDestinationPath\Destinationop , wordt de inhoud van Archive.zip de resource uitgebreid naar de Destination map als ze er nog niet zijn.

Als Validate is ingesteld op $true en Checksum niet is ingesteld, vergelijkt de resource de eigenschap LastWriteTime van elk uitgevouwen bestand met de eigenschap LastWriteTime van het relevante bestand in Archive.zip. Als de waarden voor inhoud in de Destination map niet overeenkomen met de waarde in Archive.zip, heeft de resource de gewenste status.

Als Force is ingesteld $trueop, overschrijft de resource alle uitgebreide bestanden met een onjuiste LastWriteTime. Als Force is ingesteld $falseop, genereert de resource een uitzondering in plaats van de bestanden te overschrijven.

Met Invoke-DscResource

Dit script laat zien hoe u de Archive resource met de Invoke-DscResource cmdlet kunt gebruiken om ervoor te zorgen dat Archive.zip deze wordt uitgebreid naar de Destination map met standaardvalidatie.

[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
    }
}

Met een configuratie

Dit fragment laat zien hoe u een Configuration met een Archive resourceblok kunt definiƫren om ervoor te zorgen dat Archive.zip deze wordt uitgebreid naar de Destination map met standaardvalidatie.

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'
        }
    }
}