PSDevOpsAddonsByWiku.ps1


<#PSScriptInfo
 
.VERSION 2.0
 
.GUID dfd5369d-a676-44bb-89dd-a5e29142686a
 
.AUTHOR wzerebecki@plottwist.games
 
.COMPANYNAME Plot Twist
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
.PRIVATEDATA
 
#>


<#
 
.DESCRIPTION
 Some additional functions for our DevOps, working on top of VSTeam
 
#>
 
Param()

function Set-BenFoxAzureDevOpsPAT
{
    $securePassword = Read-host -AsSecureString | ConvertFrom-SecureString
    $securePassword | Out-File -FilePath $env:USERPROFILE"\benFoxDevOpsPat.txt" 
}

function Get-Builds{
    param(
        [Parameter (Mandatory = $true)] [String]$projectName,
        [Parameter (Mandatory = $false)] [Int]$top=10
    )
    Write-Host '--------' $projectName -ForegroundColor Blue
    Get-VSTeamBuild -ProjectName $projectName -Top $top | Format-Table @{
        Label = "Build Definition"
        Expression = 
        {
            switch ($_.Result)
            {
                'failed' { $color = "31"; break }
                'succeeded' { $color = '32'; break }
                'canceled' { $color = "33"; break }
               default { $color = "0" }
            }
            $text = $_.BuildDefinition
            if('Microsoft.VisualStudio.Services.TFS' -eq $text)
            { 
                $text =  '(DevOps)'
            }

            $e = [char]27
            "$e[${color}m$($text)${e}[0m"
        }
    }, @{
        Label = "Build Number"
        Expression = 
        {
            switch ($_.Result)
            {
                'failed' { $color = "31"; break }
                'succeeded' { $color = '32'; break }
                'canceled' { $color = "33"; break }
               default { $color = "0" }
            }
            $e = [char]27
            "$e[${color}m$($_.BuildNumber)${e}[0m"
        }
    }, 'Status', @{
        Label = "Result"
        Expression = 
        {
            switch ($_.Result)
            {
                'failed' { $color = "31"; break }
                'succeeded' { $color = '32'; break }
                'canceled' { $color = "33"; break }
               default { $color = "0" }
            }
            $e = [char]27
            "$e[${color}m$($_.Result)${e}[0m"
        } 
    }, @{
            Label = "Requested by"
            Expression = 
            {
                switch ($_.Result)
                {
                    'failed' { $color = "31"; break }
                    'succeeded' { $color = '32'; break }
                    'canceled' { $color = "33"; break }
                   default { $color = "0" }
                }
                $text = $_.RequestedBy
                if('Microsoft.VisualStudio.Services.TFS' -eq $text)
                { 
                    $text =  '(DevOps)'
                }
    
                $e = [char]27
                "$e[${color}m$($text)${e}[0m"
            }
    }#, 'StartTime'
}

function Get-AllBuilds{
    param(
        [Parameter (Mandatory = $false)] [Int]$top=10
    )
    Get-Builds -ProjectName 'Ben Fox' -Top $top
    Get-Builds -ProjectName 'Unity Packages' -Top $top
}


function Get-Feeds
{
    $pat = Get-Content $env:USERPROFILE"\benFoxDevOpsPat.txt" | ConvertTo-SecureString

    $Organization = 'awesomeind'
    $AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$([System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pat)))")) }
    $UriRootFeeds = "https://feeds.dev.azure.com/$($Organization)/"

    $UriFeeds = $UriRootFeeds + "_apis/packaging/feeds?api-version=6.0-preview.1"
    $FeedResult = Invoke-RestMethod -Uri $UriFeeds -Method get -Headers $AzureDevOpsAuthenicationHeader
     
    Foreach ($feed in $FeedResult.value)
    {
        # get packages for this feed
        $UriFeedPackages = $UriRootFeeds + "$($feed.project.name)/_apis/packaging/Feeds/$($feed.id)/packages?api-version=6.0-preview.1&includeDescription=true"   
        $FeedPackageResult = Invoke-RestMethod -Uri $UriFeedPackages -Method get -Headers $AzureDevOpsAuthenicationHeader  

        #if there are packages in feed, print them out
        if(0 -ne $FeedPackageResult.value.Count)
        {
            Write-Host "-------- $($feed.name) ($($FeedPackageResult.value.Count)) packages" -ForegroundColor Blue 
            $FeedPackageResult.value | ForEach-Object{
                $pkg = $_
                $ver = $pkg.versions | Select-Object -First 1

                New-Object -TypeName psobject -Property @{
                    PublishDate   = $ver.publishDate
                    LatestVersion = $ver.version
                    Listed        = $ver.isListed
                    Name          = $pkg.name
                    Description   = $ver.packageDescription
                    ProtocolType  = $pkg.protocolType
                }
            } 
            | Sort-Object 
            | Format-Table 'Name', 'LatestVersion', 'PublishDate'
        }
    }
}

function Get-FeedsWithDownloadCountPerVersion {
    $pat = Get-Content $env:USERPROFILE"\benFoxDevOpsPat.txt" | ConvertTo-SecureString

    $Organization = 'awesomeind'
    $AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$([System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pat)))")) }
    $UriRootFeeds = "https://feeds.dev.azure.com/$($Organization)/"



    $UriFeeds = $UriRootFeeds + "_apis/packaging/feeds?api-version=6.0-preview.1"
    $FeedResult = Invoke-RestMethod -Uri $UriFeeds -Method get -Headers $AzureDevOpsAuthenicationHeader
    Write-Output $unsecurePat  
    
    Foreach ($feed in $FeedResult.value) {
        Write-Host $feed.name


        $UriFeedPackages = $UriRootFeeds + "$($feed.project.name)/_apis/packaging/Feeds/$($feed.id)/packages?api-version=6.0-preview.1"   

        $FeedPackageResult = Invoke-RestMethod -Uri $UriFeedPackages -Method get -Headers $AzureDevOpsAuthenicationHeader  
        Foreach ($feedpackage in $FeedPackageResult.value) {
            Write-Host $feedpackage.name

            $UriFeedPackageVersion = $UriRootFeeds + "$($feed.project.name)/_apis/packaging/Feeds/$($feed.id)/Packages/$($feedpackage.id)/versions?api-version=6.0-preview.1"
            $FeedPackageVersionResult = Invoke-RestMethod -Uri $UriFeedPackageVersion -Method get -Headers $AzureDevOpsAuthenicationHeader
                
            Foreach ($feedpackageversion in $FeedPackageVersionResult.value) {
                Write-Host " " $feedpackageversion.version -NoNewline


                $BodyPackageVersionIds = @{ packageVersionIds = @($feedpackageversion.id) } | ConvertTo-Json     
                $UriFeedPackageVersionUsage = $UriRootFeeds + "$($feed.project.name)/_apis/packaging/Feeds/$($feed.id)/Packages/$($feedpackage.id)/versionmetricsbatch?api-version=6.0-preview.1"   
                $FeedPackageVersionUsageResult = Invoke-RestMethod -Uri $UriFeedPackageVersionUsage -ContentType "application/json" -Method Post -Body $BodyPackageVersionIds -Headers $AzureDevOpsAuthenicationHeader
                foreach ($feedpackageversionusage in $FeedPackageVersionUsageResult.value) 
                {                    
                    Write-Host " - " $feedpackageversionusage.downloadCount                                       
                }
                if(0 -eq $FeedPackageVersionUsageResult.value.Count)
                {
                    Write-Host ""   
                }
            }

        }

    }
}

# function List-Npm {
# param(
# [Parameter (Mandatory = $true)] [String]$feed
# )
    
# $pkgs = Get-VSTeamPackage -feedName $feed

# if(0 -ne $pkgs.Count)
# {
# Write-Host '--------' $feed -ForegroundColor Blue

# $pkgs | ForEach-Object {
# $pkg = $_
# $ver = Get-VSTeamPackageVersion -feedName $feed -packageId $pkg.Id | Select-Object -First 1
# New-Object -TypeName psobject -Property @{
# PublishDate = $ver.PublishDate
# Version = $ver.Version
# Listed = $ver.IsListed
# Name = $pkg.Name
# Description = $ver.Description
# }
# } | Format-Table 'Name', 'Version', 'PublishDate', 'Description'
# }
# }

# function List-AllNpm {
# List-Npm 'plottwist.all.core'
# List-Npm 'plottwist.all.code'
# List-Npm 'plottwist.all.art'
# List-Npm 'plottwist.all.ld'

# List-Npm 'plottwist.benfox.core'
# List-Npm 'plottwist.benfox.code'
# List-Npm 'plottwist.benfox.art'
# List-Npm 'plottwist.benfox.ld'
# }