다음을 통해 공유


파일 유효성 검사 없이 보관 파일 제거

설명

이 예제에서는 리소스를 Archive 사용하여 파일의 .zip 내용이 특정 디렉터리로 확장되지 않도록 하는 방법을 보여 줍니다.

Ensure를 설정하면 Absent경로가 로 설정됩니다C:\ExampleArchivePath\Archive.zip. 대상을 설정하면 C:\ExampleDestinationPath\Destination리소스는 폴더의 Archive.zip 내용이 있는 경우 폴더에서 Destination 해당 내용을 제거합니다.

유효성 검사 또는 체크섬 집합이 없으면 리소스는 폴더에 Destination 있는 Archive.zip모든 파일을 제거합니다.

Invoke-DscResource

이 스크립트는 cmdlet과 함께 리소스를 ArchiveInvoke-DscResource 사용하여 폴더에 콘텐츠가 없도록 하는 방법을 보여 줍니다 Archive.zipDestination .

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

구성을 사용하여

이 코드 조각에서는 리소스 블록을 사용하여 ConfigurationArchive 정의하여 폴더에 Archive.zipDestination 콘텐츠가 없도록 하는 방법을 보여 줍니다.

Configuration RemoveArchiveNoValidation {
    Import-DscResource -ModuleName 'PSDscResources'

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