Skip to content

Instantly share code, notes, and snippets.

@iainbrighton
Created November 21, 2015 23:42
Show Gist options
  • Save iainbrighton/0daeabe4476494a91038 to your computer and use it in GitHub Desktop.
Save iainbrighton/0daeabe4476494a91038 to your computer and use it in GitHub Desktop.
Enumerate local DSC Resources
function TestDscResourceModule {
<#
.SYNOPSIS
Tests whether the specified PowerShell module directory is a DSC resource.
#>
[CmdletBinding()]
[OutputType([System.Boolean])]
param (
[Parameter(Mandatory, ValueFromPipeline)] [System.String] $Path
)
process {
Write-Verbose ('Testing for MOF-based DSC Resource ''{0}\DSCResources'' directory.' -f $Path);
if (Test-Path -Path "$Path\DSCResources" -PathType Container) {
## We have a WMF 4.0/MOF DSC resource module
Write-Verbose ('Found MOF-based DSC resource ''{0}''.' -f $Path);
return $true;
}
Write-Verbose ('Testing for Class-based DSC resource definition ''{0}''.' -f $Path);
if (Test-Path -Path "$($moduleInfo.FullName)\$($moduleInfo.Name).psm1" -PathType Leaf) {
## If there's a .psm1 file, check if it's a class-based DSC resource
$psm1 = Get-Content -Path "$($moduleInfo.FullName)\$($moduleInfo.Name).psm1";
if ($psm1 -imatch '^(\s*)\[DscResource\(\)\](\s*)$') {
## File has a [DscResource()] declaration
Write-Verbose ('Found Class-based DSC resource ''{0}''.' -f $Path);
return $true;
}
}
} #end process
} #end TestDscResourceModule
function GetDscResourceModule {
<#
.SYNOPSIS
Enumerates a directory path and returns list of all valid DSC resources.
#>
[CmdletBinding()]
[OutputType([System.Boolean])]
param (
[Parameter(Mandatory, ValueFromPipeline)] [System.String[]] $Path
)
process {
foreach ($basePath in $Path) {
Get-ChildItem -Path $basePath -Directory | ForEach-Object {
$moduleInfo = $PSItem;
Write-Verbose ('Checking directory ''{0}''.' -f $moduleInfo.FullName);
## Check to see if we have a MOF or class resource in the module
if (TestDscResourceModule -Path $moduleInfo.FullName) {
Write-Verbose ('Discovered DSC resource ''{0}''.' -f $moduleInfo.FullName);
Write-Output ([PSCustomObject] @{
ModuleName = $moduleInfo.Name;
ModuleVersion = New-Object -TypeName System.Version;
Path = $moduleInfo.FullName;
});
}
else {
## Enumerate each module\<number>.<number> subdirectory
Get-ChildItem -Path $moduleInfo.FullName -Directory | Where-Object Name -match '^\d+\.\d+' | ForEach-Object {
Write-Verbose ('Checking module version directory ''{0}''.' -f $PSItem.FullName);
## Test to see if it's a DSC resource module
if (TestDscResourceModule -Path $PSItem.FullName) {
try {
$moduleVersion = [System.Version] $PSItem.Name;
Write-Verbose ('Discovered versioned DSC resource ''{0}''.' -f $PSItem.FullName);
Write-Output ([PSCustomObject] @{
ModuleName = $moduleInfo.Name;
ModuleVersion = $moduleVersion;
Path = "$($moduleInfo.FullName)\$($PSItem.Name)";
});
}
catch { }
}
} | #end foreach module\<number>.<number> sub directory
Sort-Object -Property ModuleVersion -Descending | Select -First 1;
}
} #end foreach module directory
} #end foreach path
} #end process
} #end function GetDscResourceModule
GetDscResourceModule -Path "$env:ProgramFiles\WindowsPowerShell\Modules" -Verbose;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment