移除封存而不進行檔案驗證
描述
此範例示範如何使用 Archive
資源來確保檔案的內容 .zip
不會擴充至特定目錄。
將 [確定 ] 設定為 Absent
時, [路徑] 會設定為 C:\ExampleArchivePath\Archive.zip
。 和[目的地] 設定為 C:\ExampleDestinationPath\Destination
,如果資源存在,則會從 Destination
資料夾中移除 的內容 Archive.zip
。
若未設定Validate或Checksum,資源就會移除 資料夾中存在 Archive.zip
的任何檔案 Destination
。
使用 Invoke-DscResource
此腳本示範如何搭配 Invoke-DscResource
Cmdlet 使用 Archive
資源,以確保資料夾中沒有任何內容 Archive.zip
存在 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
}
}
使用組態
此程式碼片段示範如何使用資源區塊來定義 , Archive
Configuration
以確保資料夾中沒有任何內容 Archive.zip
存在 Destination
。
Configuration RemoveArchiveNoValidation {
Import-DscResource -ModuleName 'PSDscResources'
Node localhost {
Archive ExampleArchive {
Path = 'C:\ExampleArchivePath\Archive.zip'
Destination = 'C:\ExampleDestinationPath\Destination'
Ensure = 'Absent'
}
}
}