Requirements, specification, design and implementation
Requirement
We have two NuGet servers. The primary server is locally hosted for the developers. The second server is an Azure DevOps NuGet feed for contractors typically working outside the firewall.
A scheduled task synchronizing the two servers runs hourly, copying any new packages from the local server to the cloud, and any new pre-release packages from the cloud.
A bug caused some proprietary packages to be copied to the cloud, and they had to be deleted.
Specification
A script was written to use the NuGet command line interface (CLI) application to list the packages, and to delete them.
Afterwards I saw that the packages still showed up in the Azure DevOps feed. Looking at the documentation lead to this page.

After deleting a few packages I decided to automate. There were hundreds of packages. Automating actions on the web site is done by using the REST API. Getting started is fairly obvious.

Drilling down, I get to the Artifact Details – Get Packages page.

I have worked with REST APIs before, so I know that I will be using the Invoke-RestMethod cmdlet. It has a rather terrifying list of parameters. Fortunately we only need three: -Uri, -Method and -Headers.
The -Headers parameter provides the credentials. This consists of two parts, a Personal Access Token (PAT) generated by Azure DevOps:




Copy the token and save it somewhere safe. In this case I am embedding it in a PowerShell function:
function Get-AzureDevOpsCredential {
param(
[string]$Token = '0u8kfiagblr2d5gz097uwvp2rl9f7bnfcyjjmk4a9kow9sn4ui2t',
[string]$UserEmail = 'joglethorpe@ecentric.co.za'
)
@{Authorization=("Basic $([Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $UserEmail,$Token))))")}
}
$auth = Get-AzureDevOpsCredential
$org = 'epsdev'
Invoke-RestMethod -Uri "https://feeds.dev.azure.com/$org/_apis/packaging/Feeds/TestFeed/packages?api-version=5.0-preview.1" -Method GET -Headers $auth
count value
----- -----
79 {@{id=2b0c842e-204c-4698-9c90-0c55dfd5eef5; normalizedName=backofficeaudit.logging; name=BackOfficeAudit.Logging; protocolType=NuGet; url=https://feeds.dev.azure.com/epsdev/_apis/Packaging/...
I refine the script to get a list of the packages and versions I want to delete:
$uri = "https://feeds.dev.azure.com/$org/_apis/packaging/Feeds/TestFeed/packages?protocolType=NuGet&packageNameQuery=Ecentric.UPG.Entity.EntityDB&includeAllVersions=True&api-version=5.0-preview.1"
$pkg = Invoke-RestMethod -Uri $uri -Method GET -Headers $auth
$pkg.value
$pkg.value.versions
id : 3f53f107-fe7c-4403-b0c6-708833c48bf9
normalizedName : ecentric.upg.entity.entitydb
name : Ecentric.UPG.Entity.EntityDB
protocolType : NuGet
url : https://feeds.dev.azure.com/epsdev/_apis/Packaging/Feeds/85a7e085-89e4-401b-a758-31fddd1dd507/Packages/3f53f107-fe7c-4403-b0c6-708833c48bf9
versions : {@{id=6eb17396-68a5-4873-b9aa-e36b295ae629; normalizedVersion=0.2.181; version=0.2.181; isLatest=True; isListed=True;
storageId=27C537932E0859EFD657141FC2FDE0E14073502691C28C2297BC95A0EC0262A200; views=System.Object[]; publishDate=2019-04-08T15:46:27.67005Z},
@{id=4660b946-b17a-41c5-89d1-adca9e29a5ea; normalizedVersion=0.2.178; version=0.2.178; isLatest=False; isListed=True;
storageId=F5EBB39EB7FD34EBF5A28DB31ED505C1B0DA59F68CD94668491845E535E171A100; views=System.Object[]; publishDate=2019-04-05T13:55:06.5019964Z},
@{id=99340196-4f93-4cc7-8390-20509886913c; normalizedVersion=0.2.177; version=0.2.177; isLatest=False; isListed=True;
storageId=F5526525AFCFC9495AED24D5421ACBC9CF63BD48F587ACF32E49AF181F0EFC3400; views=System.Object[]; publishDate=2019-04-05T13:54:50.0169655Z},
@{id=37ec02b2-e048-4a68-be71-c76db845e3c5; normalizedVersion=0.2.176; version=0.2.176; isLatest=False; isListed=True;
storageId=5AECA5990B14D2A12631C52D4121D2C8DB2A35BF2C8FF6B7DDB013FC04BC616900; views=System.Object[]; publishDate=2019-04-05T13:54:33.6320533Z}...}
_links : @{self=; feed=; versions=}
id : 6eb17396-68a5-4873-b9aa-e36b295ae629
normalizedVersion : 0.2.181
version : 0.2.181
isLatest : True
isListed : True
storageId : 27C537932E0859EFD657141FC2FDE0E14073502691C28C2297BC95A0EC0262A200
views : {@{id=c69bbc8c-0958-4e69-b91b-e2cc63c1001a; name=Local; url=; type=implicit}}
publishDate : 2019-04-08T15:46:27.67005Z
id : 4660b946-b17a-41c5-89d1-adca9e29a5ea
normalizedVersion : 0.2.178
version : 0.2.178
isLatest : False
isListed : True
storageId : F5EBB39EB7FD34EBF5A28DB31ED505C1B0DA59F68CD94668491845E535E171A100
views : {@{id=c69bbc8c-0958-4e69-b91b-e2cc63c1001a; name=Local; url=; type=implicit}}
publishDate : 2019-04-05T13:55:06.5019964Z
Now it is a simple matter of providing a list of the packages to be deleted, and to do a script to list them. The next matter is to delete each version.
Returning to the REST API, I see nothing to help in the Artifact documentation. Just below is the Artifacts Package Types sidebar, which I expand to NuGet:

Following the link, I find the necessary information to delete every package version.
Now I have all the information I need to do the job. More important, I have a working prototype. This may be sufficient. If I want to pass this task on to an administrator without PowerShell experience, I will need to make this into a utility – a script or command.
I start with the script as a specification of what needs to be done. The big plus, is that it works, and will require no further research by the developer.
Design and Implementation
I have already described how to create a PowerShell Script Module from a script. In summary:
- Create a PowersShell project, including
- This script
- Empty Script and Manifest modules
- A Build script
- Scripts and Tests folders
- Add a reference to the module from the script
- Refactor the script
- Select code to extract to a function
- Write a basic test script for the function
- Extract the code into the function
- Test the extracted code
- Continue refactoring by
- Extracting code
- Eliminating duplications