Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example DSC class-based resource for file content
# this is an example of what a converted script resource would look like as a class keeping
# the private functions
[DscResource()]
class FileContentResource {
[DscProperty(Key)]
[string] $filePath
[DscProperty(Mandatory)]
[string] $content
[FileContentResource] Get()
{
return (Get-FileContent -filePath $this.filePath)
}
[void] Set()
{
Set-FileContent -filePath $this.filePath -content $this.Content
}
[bool] Test()
{
return (Test-FileContent -filePath $this.filePath -content $this.content)
}
FileContentResource() {
# default constructor
}
FileContentResource(
[string] $filePath,
[string] $content
){
$this.filePath = $filePath
$this.content = $content
}
}
function Get-FileContent {
param($filePath)
$content = Get-Content -Path $filePath
[FileContentResource]::new($filePath, $content)
}
function Set-FileContent {
param($filePath, $content)
Set-Content -Path $filePath -Value $content -Force
}
function Test-FileContent {
param($filePath, $content)
(Get-Content -Path $filePath) -eq $content
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment