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
- param ($fileName, $computerName=$env:ComputerName)
- #######################
- function ConvertTo-MIFXml
- {
- param ($mifFile)
- $mifText = gc $mifFile |
- #Remove illegal XML characters
- % { $_ -replace "&", "&" } |
- % { $_ -replace"'", "'" } |
- % { $_ -replace "<", "<" } |
- % { $_ -replace ">", ">" } |
- #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
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.
PowerShell Code Repository