Build/Get-AppFromLastSuccessfulBuild.ps1

function Get-AppFromLastSuccessfulBuild {
    Param(
        [Parameter(Mandatory=$true)]
        [string]$ProjectName,
        [Parameter(Mandatory=$false)]
        [string]$RepositoryName = '',
        [Parameter(Mandatory=$false)]
        [switch]$OpenExplorer,
        [Parameter(Mandatory=$false)]
        [switch]$IncludeTests
    )

    $VSTSProjectName = (Get-VSTSProjects | where name -like ('*{0}' -f $ProjectName)).name

    if (($VSTSProjectName -eq $null) -and ($ProjectName.StartsWith('Clever'))) {
        $VSTSProjectName = (Get-VSTSProjects | where name -like ('*{0}' -f $ProjectName.Substring(7))).name
    }

    if ($RepositoryName -ne '') {
        $Build = Invoke-TFSAPI ('{0}{1}/_apis/build/builds?queryOrder=finishTimeDescending&resultFilter=succeeded&$top=1&repositoryId={2}&repositoryType=TfsGit' -f (Get-TFSCollectionURL), $VSTSProjectName, (Get-RepositoryId -ProjectName $VSTSProjectName -RepositoryName $RepositoryName))
    }
    else {
        $Build = Invoke-TFSAPI ('{0}{1}/_apis/build/builds?queryOrder=finishTimeDescending&resultFilter=succeeded&$top=1' -f (Get-TFSCollectionURL), $VSTSProjectName)
    }

    $Artifacts = Invoke-TFSAPI ('{0}{1}/_apis/build/builds/{2}/artifacts' -f (Get-TFSCollectionURL), $VSTSProjectName, $Build.value.id)
    $ArtifactPath = Join-Path (Create-TempDirectory) ('{0}.zip' -f (Get-URLParameterValue -Url $Artifacts.value.resource.downloadUrl -ParameterName 'artifactName'))
    Invoke-TFSAPI ($Artifacts.value.resource.downloadUrl) -OutFile -OutFilePath $ArtifactPath
    $ExpandPath = (Create-TempDirectory)
    Expand-Archive -Path $ArtifactPath -DestinationPath $ExpandPath

    if ($IncludeTests.IsPresent) {
        Get-ChildItem -Path $ExpandPath -Filter '*.app' -Recurse
    }
    else {
        Get-ChildItem -Path $ExpandPath -Filter '*.app' -Recurse | ? FullName -NotLike '*Tests*'
    }

    if ($OpenExplorer.IsPresent) {
        explorer (Split-Path (Get-ChildItem -Path $ExpandPath -Filter '*app' -Recurse).Item(0).FullName -Parent)
    }
    Remove-Item $ArtifactPath -Recurse
}

function Get-URLParameterValue {
    Param(
        [Parameter(Mandatory=$true)]
        [string]$Url,
        [Parameter(Mandatory=$true)]
        [string]$ParameterName
    )

    $ParameterString = $Url.Substring($Url.IndexOf('?') + 1)
    $Parameters = $ParameterString.Split('&')
    $Parameter = $Parameters | where {$_ -like ('{0}*' -f $ParameterName)}
    $Parameter.Substring($Parameter.IndexOf('=') + 1)
}

Export-ModuleMember -Function Get-AppFromLastSuccessfulBuild