创建具有内容的文件

说明

此示例演示如何使用 Script 资源创建文件。

若要使用 Script 资源,需要指定三个代码块。 GetScript、TestScriptSetScript

此示例使用用户输入的两个参数。 FilePath 设置文件应位于的路径, FileContent 设置文件的内容。 这些值使用 using 指令在 scriptblock 中引用。

GetScript

$fileContent = $null

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

return @{
    Result = $fileContent
}

GetScript 脚本块中,代码会检查 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 脚本块中,代码创建 一个 System.IO.StreamWriter 对象以写入 FilePath 指定的文件。 它写入 FileContent 指定的内容,然后关闭 StreamWriter 对象。

使用 Invoke-DscResource

此脚本演示如何将 Script 资源与 cmdlet 配合使用 Invoke-DscResource ,以确保存在具有特定内容的文件。

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

使用配置

此代码片段演示如何使用Script资源块定义,Configuration以确保文件存在特定内容。

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