Skip to content

Instantly share code, notes, and snippets.

@iainbrighton
Created February 4, 2017 16:06
Show Gist options
  • Save iainbrighton/9d3dd03630225ee44126769c5d9c50a9 to your computer and use it in GitHub Desktop.
Save iainbrighton/9d3dd03630225ee44126769c5d9c50a9 to your computer and use it in GitHub Desktop.
Scans a Desired State Configuration .mof file and returns the declared/required modules.
function Get-MofModule {
<#
.SYNOPSIS
Scans a Desired State Configuration .mof file and returns the declared/
required modules.
#>
[CmdletBinding()]
param (
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[System.String] $Path
)
process {
$modules = @{ };
$moduleName = $null;
$moduleVersion = $null;
Get-Content -Path $Path -Encoding Unicode | ForEach-Object {
$line = $_;
if ($line -match '^\s?Instance of') {
## We have a new instance so write the existing one
if (($null -ne $moduleName) -and ($null -ne $moduleVersion)) {
$modules[$moduleName] = $moduleVersion;
$moduleName = $null;
$moduleVersion = $null;
}
}
elseif ($line -match '(?<=^\s?ModuleName\s?=\s?")\S+(?=";)') {
## Ignore the default PSDesiredStateConfiguration module
if ($Matches[0] -notmatch 'PSDesiredStateConfiguration') {
$moduleName = $Matches[0];
}
}
elseif ($line -match '(?<=^\s?ModuleVersion\s?=\s?")\S+(?=";)') {
$moduleVersion = $Matches[0] -as [System.Version];
}
}
Write-Output -InputObject $modules;
} #end process
} #end function Get-MofModule
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment