Dela via


Ta bort ett arkiv utan filverifiering

Description

Det här exemplet visar hur du kan använda resursen Archive för att säkerställa att inget innehåll i en .zip fil expanderas till en specifik katalog.

Med Se till inställd på Absentanger du Sökväg till C:\ExampleArchivePath\Archive.zip. och målinställningen är C:\ExampleDestinationPath\Destination, tar resursen bort innehållet i Archive.zip från Destination mappen om de finns.

Utan verifierings - eller kontrollsummauppsättningen tar resursen bort alla filer i Destination mappen som finns i Archive.zip.

Med Invoke-DscResource

Det här skriptet visar hur du kan använda resursen Archive med cmdleten Invoke-DscResource för att säkerställa att det inte finns något innehåll i Archive.zip mappen Destination .

[CmdletBinding()]
param()

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

    $NonGetProperties = @(
        '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
    }
}

Med en konfiguration

Det här kodfragmentet visar hur du kan definiera en Configuration med ett Archive resursblock för att säkerställa att det inte finns något innehåll i Archive.zip mappen Destination .

Configuration RemoveArchiveNoValidation {
    Import-DscResource -ModuleName 'PSDscResources'

    Node localhost {
        Archive ExampleArchive {
            Path        = 'C:\ExampleArchivePath\Archive.zip'
            Destination = 'C:\ExampleDestinationPath\Destination'
            Ensure      = 'Absent'
        }
    }
}