Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A PowerShell script which removes all the unnecessary elements and attributes from a Team Build Process Template XAML file that have been added by the Workflow Designer. http://blog.stangroome.com/2014/02/12/effectively-comparing-team-build-process-templates/
#requires -version 3
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[ValidateScript({ $_ | Test-Path -PathType Leaf })]
[string]
$Path,
[string]
$Destination = $Path
)
# ensure the path is fully qualified for use with $Doc.Save()
$Destination = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($Destination)
$Namespaces = @{
xmlns = 'http://www.w3.org/2000/xmlns/'
mc = 'http://schemas.openxmlformats.org/markup-compatibility/2006'
}
$Doc = (Select-Xml -Path $Path -XPath / ).Node
$IgnorableValue = $Doc.DocumentElement.GetAttribute('Ignorable', $Namespaces.mc)
$IgnorablePrefixes = @()
if ($IgnorableValue) {
$IgnorablePrefixes = @($IgnorableValue -split '\s+')
}
foreach ($Prefix in $IgnorablePrefixes) {
$NamespaceUri = $Doc.DocumentElement.GetAttribute($Prefix, $Namespaces.xmlns)
$PrefixNamespaces = @{ $Prefix = $NamespaceUri }
# elements
Select-Xml -Xml $Doc -XPath "//$Prefix`:*" -Namespace $PrefixNamespaces |
Select-Object -ExpandProperty Node |
ForEach-Object {
$_.ParentNode.RemoveChild($_) | Out-Null
}
# attributes
Select-Xml -Xml $Doc -XPath "//*/@$Prefix`:*" -Namespace $PrefixNamespaces |
Select-Object -ExpandProperty Node |
ForEach-Object {
$_.OwnerElement.RemoveAttributeNode($_) | Out-Null
}
}
$Doc.Save($Destination)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment