Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions templates/DscClassBasedResource/PSMDInvoke.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
param (
$Path
)

New-PSMDTemplate -ReferencePath "$PSScriptRoot" -OutPath $Path
17 changes: 17 additions & 0 deletions templates/DscClassBasedResource/PSMDTemplate.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@{
TemplateName = 'dscclass'
Version = "1.0.0.0"
AutoIncrementVersion = $true
Tags = 'dscresource'
Author = 'Jan-Hendrik Peters'
Description = 'Basic class-based DSC resource template with support for Azure Guest Configuration'
Exclusions = @("PSMDInvoke.ps1") # Contains list of files - relative path to root - to ignore when building the template
Scripts = @{
guid = {
[System.Guid]::NewGuid().ToString()
}
year = {
Get-Date -Format "yyyy"
}
}
}
85 changes: 85 additions & 0 deletions templates/DscClassBasedResource/þnameþ.psd1
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
@{
# Script module or binary module file associated with this manifest
RootModule = 'þnameþ.psm1'

# Version number of this module.
ModuleVersion = '1.0.0'

# ID used to uniquely identify this module
GUID = 'þ!guid!þ'

# Author of this module
Author = 'þauthorþ'

# Company or vendor of this module
CompanyName = 'þcompanyþ'

# Copyright statement for this module
Copyright = 'Copyright (c) þ!year!þ þauthorþ'

# Description of the functionality provided by this module
Description = 'þdescriptionþ'

# Minimum version of the Windows PowerShell engine required by this module
PowerShellVersion = '5.0'

# Modules that must be imported into the global environment prior to importing
# this module
# RequiredModules = @()

# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @('bin\þnameþ.dll')

# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @('xml\þnameþ.Types.ps1xml')

# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @('xml\þnameþ.Format.ps1xml')

# Functions to export from this module
FunctionsToExport = ''

# Cmdlets to export from this module
# CmdletsToExport = ''

# Variables to export from this module
# VariablesToExport = ''

# Aliases to export from this module
# AliasesToExport = ''

DscResourcesToExport = @(
'þnameþ'
)

# List of all modules packaged with this module
ModuleList = @()

# List of all files packaged with this module
FileList = @()

# Private data to pass to the module specified in ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{

#Support for PowerShellGet galleries.
PSData = @{

# Tags applied to this module. These help with module discovery in online galleries.
# Tags = @()

# A URL to the license for this module.
# LicenseUri = ''

# A URL to the main website for this project.
# ProjectUri = ''

# A URL to an icon representing this module.
# IconUri = ''

# ReleaseNotes of this module
# ReleaseNotes = ''

} # End of PSData hashtable

} # End of PrivateData hashtable
}
124 changes: 124 additions & 0 deletions templates/DscClassBasedResource/þnameþ.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
enum Ensure
{
Present
Absent
}

# Support for DSC v3, or Azure Guest Configuration
class Reason
{
[DscProperty()]
[string] $Code

[DscProperty()]
[string] $Phrase
}

function Get-Resource
{
[OutputType([hashtable])]
[CmdletBinding()]
param
(
)

$reasonList = @()

if ($Ensure -eq 'Present' -and -not $successfulTests)
{
$reasonList += @{
Code = 'þnameþ:þnameþ:SomethingAwfulHappened'
Phrase = "Extend the reasonList with all errors"
}
}

return @{
Reasons = $reasonList
}
}

function Set-Resource
{
[CmdletBinding()]
param
(
)

}

function Test-Resource
{
[OutputType([bool])]
[CmdletBinding()]
param
(

)

return $true
}

[DscResource()]
class þnameþ
{
[DscProperty(Key)] [string] $YourKeyProperty
[DscProperty(Mandatory)] [string] $YourRequiredProperty
[DscProperty()] [string[]] $YourStandardProperty
[DscProperty()] [Ensure] $Ensure # Usually a good idea to include
[DscProperty(NotConfigurable)] [Reason[]] $Reasons # Reserved for Azure Guest Configuration

þnameþ ()
{
$this.Ensure = 'Present' # Set up defaults
}

[þnameþ] Get()
{
$parameter = $this.GetConfigurableDscProperties()
return (Get-Resource @parameter)
}

[void] Set()
{
$parameter = $this.GetConfigurableDscProperties()
Set-Resource @parameter
}

[bool] Test()
{
$parameter = $this.GetConfigurableDscProperties()
return (Test-Resource @parameter)
}

[Hashtable] GetConfigurableDscProperties()
{
# This method returns a hashtable of properties with two special workarounds
# The hashtable will not include any properties marked as "NotConfigurable"
# Any properties with a ValidateSet of "True","False" will beconverted to Boolean type
# The intent is to simplify splatting to functions
# Source: https://gist.github.com/mgreenegit/e3a9b4e136fc2d510cf87e20390daa44
$DscProperties = @{}
foreach ($property in [þnameþ].GetProperties().Name)
{
# Checks if "NotConfigurable" attribute is set
$notConfigurable = [þnameþ].GetProperty($property).GetCustomAttributes($false).Where({ $_ -is [System.Management.Automation.DscPropertyAttribute] }).NotConfigurable
if (!$notConfigurable)
{
$value = $this.$property
# Gets the list of valid values from the ValidateSet attribute
$validateSet = [þnameþ].GetProperty($property).GetCustomAttributes($false).Where({ $_ -is [System.Management.Automation.ValidateSetAttribute] }).ValidValues
if ($validateSet)
{
# Workaround for boolean types
if ($null -eq (Compare-Object @('True', 'False') $validateSet))
{
$value = [System.Convert]::ToBoolean($this.$property)
}
}
# Add property to new
$DscProperties.add($property, $value)
}
}
return $DscProperties
}
}