Skip to content

Instantly share code, notes, and snippets.

@SteveL-MSFT
Last active September 2, 2021 01:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SteveL-MSFT/0b39a56728190a57c6ce04ee3ed240ec to your computer and use it in GitHub Desktop.
Save SteveL-MSFT/0b39a56728190a57c6ce04ee3ed240ec to your computer and use it in GitHub Desktop.
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