Last active
September 2, 2021 01:29
-
-
Save SteveL-MSFT/0b39a56728190a57c6ce04ee3ed240ec to your computer and use it in GitHub Desktop.
Example DSC class-based resource for file content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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