共用方式為


建立具有內容的檔案

描述

此範例示範如何使用 Script 資源來建立檔案。

若要使用 Script 資源,您必須指定三個程式碼區塊。 GetScriptTestScriptSetScript

此範例使用使用者輸入的兩個參數。 FilePath 會設定檔案應該存在於的路徑, 而 FileContent 會設定檔案的內容。 這些值會以 using 指示詞在 scriptblocks 中參考。

GetScript

$fileContent = $null

if (Test-Path -Path $using:FilePath) {
    $fileContent = Get-Content -Path $using:filePath -Raw
}

return @{
    Result = $fileContent
}

GetScript scriptblock 中,程式碼會檢查 FilePath 指定的檔案是否存在。 如果這樣做,scriptblock 會針對結果傳回該檔案的目前內容。 如果沒有,腳本區塊會 $null 傳回結果。

TestScript

if (Test-Path -Path $using:FilePath) {
    $fileContent = Get-Content -Path $using:filePath -Raw
    return ($fileContent -eq $using:FileContent)
} else {
    return $false
}

TestScript 腳本區塊中,程式碼會檢查 FilePath 指定的檔案是否存在。 如果沒有,scriptblock 會傳 $false 回 。 如果存在,程式碼會將檔案的目前內容與 FileContent所指定的內容進行比較。 如果內容相符,scriptblock 會傳 $true 回 。 如果沒有,scriptblock 會傳 $false 回 。

SetScript

$streamWriter = New-Object -TypeName 'System.IO.StreamWriter' -ArgumentList @(
    $using:FilePath
)
$streamWriter.WriteLine($using:FileContent)
$streamWriter.Close()

SetScript scriptblock 中,程式碼會建立 System.IO.StreamWriter 物件,以寫入 FilePath所指定的檔案。 它會寫入 FileContent 所指定的內容,然後關閉 StreamWriter 物件。

使用 Invoke-DscResource

此腳本示範如何搭配 Invoke-DscResource Cmdlet 使用 Script 資源,以確保檔案具有特定內容。

[CmdletBinding()]
param(
    [Parameter(Mandatory = $true)]
    [ValidateNotNullOrEmpty()]
    [String]
    $FilePath,

    [Parameter(Mandatory = $true)]
    [ValidateNotNullOrEmpty()]
    [String]
    $FileContent
)

begin {
    $SharedParameters = @{
        Name       = 'Script'
        ModuleName = 'PSDscResource'
        Properties = @{
            SetScript = {
                $streamWriter = New-Object -TypeName 'System.IO.StreamWriter' -ArgumentList @(
                    $using:FilePath
                )
                $streamWriter.WriteLine($using:FileContent)
                $streamWriter.Close()
            }
            TestScript = {
                if (Test-Path -Path $using:FilePath) {
                    $fileContent = Get-Content -Path $using:filePath -Raw
                    return ($fileContent -eq $using:FileContent)
                } else {
                    return $false
                }
            }
            GetScript = {
                $fileContent = $null

                if (Test-Path -Path $using:FilePath) {
                    $fileContent = Get-Content -Path $using:filePath -Raw
                }

                return @{
                    Result = $fileContent
                }
            }
        }
    }
}

process {
    $TestResult = Invoke-DscResource -Method Test @SharedParameters

    if ($TestResult.InDesiredState) {
        Invoke-DscResource -Method Get @SharedParameters
    } else {
        Invoke-DscResource -Method Set @SharedParameters
    }
}

使用組態

此程式碼片段示範如何使用資源區塊來定義 , ConfigurationScript 以確保檔案存在特定內容。

Configuration ScriptExample {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]
        $FilePath,

        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]
        $FileContent
    )

    Import-DscResource -ModuleName 'PSDscResources'

    Node localhost {
        Script ScriptExample {
            SetScript = {
                $streamWriter = New-Object -TypeName 'System.IO.StreamWriter' -ArgumentList @(
                    $using:FilePath
                )
                $streamWriter.WriteLine($using:FileContent)
                $streamWriter.Close()
            }
            TestScript = {
                if (Test-Path -Path $using:FilePath) {
                    $fileContent = Get-Content -Path $using:filePath -Raw
                    return ($fileContent -eq $using:FileContent)
                } else {
                    return $false
                }
            }
            GetScript = {
                $fileContent = $null

                if (Test-Path -Path $using:FilePath) {
                    $fileContent = Get-Content -Path $using:filePath -Raw
                }

                return @{
                    Result = $fileContent
                }
            }
        }
    }
}