PoshCode Archive  Artifact [864132a122]

Artifact 864132a122825ab85094837661aeed97409f3b04682c7b1742b9cf198bbb5433:

  • File MIFParser.ps1 — part of check-in [69ccca5c11] at 2018-06-10 12:57:19 on branch trunk — Parses Management Information Format (MIF) files. See http://chadwickmiller.spaces.live.com/blog/cns!EA42395138308430!522.entry (user: Chad Miller size: 2155)

# encoding: ascii
# api: powershell
# title: MIFParser.ps1
# description: Parses Management Information Format (MIF) files. See http://chadwickmiller.spaces.live.com/blog/cns!EA42395138308430!522.entry
# version: 0.1
# type: function
# author: Chad Miller
# license: CC0
# function: ConvertTo-MIFXml
# x-poshcode-id: 1447
# x-archived: 2011-08-27T18:41:37
# x-published: 2011-11-02T12:42:00
#
#
param ($fileName, $computerName=$env:ComputerName)

#######################
function ConvertTo-MIFXml
{
    param ($mifFile)

    $mifText = gc $mifFile | 
    #Remove illegal XML characters
    % { $_ -replace "&", "&" } |
    % { $_ -replace"'", "'" } |
    % { $_ -replace "<", "&lt;" } |
    % { $_ -replace ">", "&gt;" } |
    #Create Component attribute
    % { $_ -replace 'Start Component','<Component' } | 
    #Create Group attribute
    % { $_ -replace 'Start Group','><Group' } | 
    #Create Attribute attribute
    % { $_ -replace 'Start Attribute','><Attribute' } |
    #Create closing tags
    % { $_ -replace 'End Attribute','></Attribute>' } |
    % { $_ -replace 'End Group','</Group>' } |
    % { $_ -replace 'End Component','</Component>'} |
    #Remove all quotes
    % { $_ -replace '"' } | 
    #Remove MIF comments. MIF Comments start with //
    % { $_ -replace "(\s*//\s*.*)" } |
    #Extract name/value and quote value
    % { $_ -replace "\s*([^\s]+)\s*=\s*(.+$)",'$1="$2"' } |
    #Replace tabs with spaces
    % { $_ -replace "\t", " " } |
    #Replace 2 spaces with 1
    % { $_ -replace "\s{2,}", " " }

    #Join the array, cleanup some spacing and extra > signs
    [xml]$mifXml = [string]::Join(" ", $mifText) -replace ">\s*>",">" -replace "\s+>",">"

    return $mifXml

} #ConvertTo-MIFXml

#######################
ConvertTo-MIFXml $fileName | foreach {$_.component} | foreach {$_.Group} | foreach {$_.Attribute} | select @{n='SystemName';e={$computerName}}, `
@{n='Component';e={$($_.psbase.ParentNode).psbase.ParentNode.name}}, @{n='Group';e={$_.psbase.ParentNode.name}}, `
@{n='FileName';e={[System.IO.Path]::GetFileName($FileName)}}, ID, Name, Value