Poznámka:
Přístup k této stránce vyžaduje autorizaci. Můžete se zkusit přihlásit nebo změnit adresáře.
Přístup k této stránce vyžaduje autorizaci. Můžete zkusit změnit adresáře.
Description
Tento příklad ukazuje, jak můžete prostředek použít Script k vytvoření souboru.
Pokud chcete prostředek použít Script , musíte zadat tři bloky kódu.
GetScript, TestScript a SetScript.
Tento příklad používá dva parametry vstupu uživatele.
FilePath nastaví cestu, na které má soubor existovat, a FileContent nastaví obsah souboru. Tyto hodnoty jsou odkazovány ve skriptblocks s direktivou using .
GetScript
$fileContent = $null
if (Test-Path -Path $using:FilePath) {
$fileContent = Get-Content -Path $using:filePath -Raw
}
return @{
Result = $fileContent
}
V objektu GetScript scriptblock zkontroluje kód, jestli soubor určený filePath existuje. Pokud ano, skriptblock vrátí aktuální obsah tohoto souboru pro výsledek. Pokud ne, skriptblock se vrátí $null pro výsledek.
TestScript
if (Test-Path -Path $using:FilePath) {
$fileContent = Get-Content -Path $using:filePath -Raw
return ($fileContent -eq $using:FileContent)
} else {
return $false
}
V bloku skriptu TestScript kód zkontroluje, jestli soubor určený filePath existuje. Pokud ne, vrátí $falsese skriptblock . Pokud existuje, kód porovná aktuální obsah souboru s obsahem určeným FileContent. Pokud se obsah shoduje, vrátí $trueskriptblock . Pokud ne, vrátí $falsese skriptblock .
SetScript
$streamWriter = New-Object -TypeName 'System.IO.StreamWriter' -ArgumentList @(
$using:FilePath
)
$streamWriter.WriteLine($using:FileContent)
$streamWriter.Close()
V objektu SetScript scriptblock vytvoří kód objekt System.IO.StreamWriter pro zápis do souboru určeného filePathem. Zapíše obsah určený FileContent a pak zavře objekt StreamWriter .
S Invoke-DscResource
Tento skript ukazuje, jak pomocí Script prostředku s rutinou Invoke-DscResource zajistit, aby soubor existoval s konkrétním obsahem.
[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
}
}
S konfigurací
Tento fragment kódu ukazuje, jak můžete definovat Configuration blok Script prostředků, abyste zajistili, že soubor existuje s konkrétním obsahem.
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
}
}
}
}
}