Wednesday, 6 January 2016

Powershell cruise control build output parser

<#
.Synopsis
   Write-XmlPathOutput processes all files in a directory for a given XPath expression and puts the results to .txt files of the same name
.EXAMPLE
   Write-XmlPathOutput -DirectoryPath DirectoryPath -XPath //*/project
.INPUTS
   DirectoryPath - the path to the directory
   XPath - the xpath you wish to use
.OUTPUTS
   .txt text files in the same directory
#>
function Write-XmlPathOutput
{
    [CmdletBinding(DefaultParameterSetName='Parameter Set 1',
                  SupportsShouldProcess=$false,
                  PositionalBinding=$false,
                  HelpUri = 'http://merrickchaffer.blogspot.com/',
                  ConfirmImpact='Medium')]
    [OutputType([String])]
    Param
    (
        # Param1 help description
        [Parameter(Mandatory=$true,
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true,
                   ValueFromRemainingArguments=$false,
                   Position=0,
                   ParameterSetName='Parameter Set 1')]
        [ValidateNotNull()]
        [Alias("p")]
        [String]
        $DirectoryPath = (Get-Location).Path,

        # xpath help description
        [Parameter(Mandatory=$false, ParameterSetName='Parameter Set 1', Position=1,
                    ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true,
                   ValueFromRemainingArguments=$false )]
        [Alias("x")]
        [String]
        $XPath = "//*/project/@file"

    )

    Begin
    {
    }
    Process
    {
        if ($pscmdlet.ShouldProcess("Target", "Operation"))
        {
            $files = Get-ChildItem (Get-ChildItem $DirectoryPath\*.xml)
            if ($files -ne $null -and $files.Length -gt 0) {
               
                ForEach ($file in $files) {
                 
                  $OutFile = [string]::Concat($file.FullName, ".txt")
                 
                  Select-Xml -Path $file.FullName -XPath $XPath | Select -ExpandProperty Node | Out-File $OutFile

                }
            }

        }
    }
    End
    {
    }
}

Write-XmlPathOutput -DirectoryPath C:\temp -XPath "//*/project/@file"

No comments:

Post a Comment

How to fix blurry images in Google Chrome

Problem When browsing sites with thumbnail images, those images appear blurred in Chrome, but not in other browsers such as Internet Explore...