PoshCode Logo PowerShell Code Repository

MIFParser.ps1 by Chad Miller 27 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1447"></script>download | new post

Parses Management Information Format (MIF) files. See http://chadwickmiller.spaces.live.com/blog/cns!EA42395138308430!522.entry

  1. param ($fileName, $computerName=$env:ComputerName)
  2.  
  3. #######################
  4. function ConvertTo-MIFXml
  5. {
  6.     param ($mifFile)
  7.  
  8.     $mifText = gc $mifFile |
  9.     #Remove illegal XML characters
  10.     % { $_ -replace "&", "&amp;" } |
  11.     % { $_ -replace"'", "&apos;" } |
  12.     % { $_ -replace "<", "&lt;" } |
  13.     % { $_ -replace ">", "&gt;" } |
  14.     #Create Component attribute
  15.     % { $_ -replace 'Start Component','<Component' } |
  16.     #Create Group attribute
  17.     % { $_ -replace 'Start Group','><Group' } |
  18.     #Create Attribute attribute
  19.     % { $_ -replace 'Start Attribute','><Attribute' } |
  20.     #Create closing tags
  21.     % { $_ -replace 'End Attribute','></Attribute>' } |
  22.     % { $_ -replace 'End Group','</Group>' } |
  23.     % { $_ -replace 'End Component','</Component>'} |
  24.     #Remove all quotes
  25.     % { $_ -replace '"' } |
  26.     #Remove MIF comments. MIF Comments start with //
  27.     % { $_ -replace "(\s*//\s*.*)" } |
  28.     #Extract name/value and quote value
  29.     % { $_ -replace "\s*([^\s]+)\s*=\s*(.+$)",'$1="$2"' } |
  30.     #Replace tabs with spaces
  31.     % { $_ -replace "\t", " " } |
  32.     #Replace 2 spaces with 1
  33.     % { $_ -replace "\s{2,}", " " }
  34.  
  35.     #Join the array, cleanup some spacing and extra > signs
  36.     [xml]$mifXml = [string]::Join(" ", $mifText) -replace ">\s*>",">" -replace "\s+>",">"
  37.  
  38.     return $mifXml
  39.  
  40. } #ConvertTo-MIFXml
  41.  
  42. #######################
  43. ConvertTo-MIFXml $fileName | foreach {$_.component} | foreach {$_.Group} | foreach {$_.Attribute} | select @{n='SystemName';e={$computerName}}, `
  44. @{n='Component';e={$($_.psbase.ParentNode).psbase.ParentNode.name}}, @{n='Group';e={$_.psbase.ParentNode.name}}, `
  45. @{n='FileName';e={[System.IO.Path]::GetFileName($FileName)}}, ID, Name, Value

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me