Parse MOF with PowerShell

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Adam Weigert Adam Weigert 1 year, 11 months ago.

  • Author
    Posts
  • #25793
    Profile photo of Adam Weigert
    Adam Weigert
    Participant

    I am looking for a way to read a MOF file in a structured way within PowerShell. Specifically I am looking to find all the module references within a given MOF file. I'm assuming there might be something built in since DSC generates and reads/compiles MOFs.

  • #25794
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    I can't speak for how well this works, but a quick web search turned up https://github.com/mikeclayton/MofParser

  • #25882
    Profile photo of Jeffrey Patton
    Jeffrey Patton
    Participant

    I wrote up a pretty simple module for this as well.

  • #25894
    Profile photo of Adam Weigert
    Adam Weigert
    Participant

    I cheated a little, not really parsing but assuming that the instances are all sorta like a hashtable ...

    $Content = Get-Content -Path $Path | Out-String 
    $Content = $Content -replace '/\*','' 
    $Content = $Content -replace '= (NULL|TRUE|FALSE)','= $$$1' 
    $Content = $Content -replace 'instance of .+\n','[PSCustomObject][Ordered]@' 
    $Content = $Content -replace '\\\\','\' 
     
    $Instances = @(Invoke-Expression -Command $Content) 
    

You must be logged in to reply to this topic.