Skip to content

Instantly share code, notes, and snippets.

@jlucaspains
Created September 23, 2021 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlucaspains/92d71fd346106e657138f796ab3a708c to your computer and use it in GitHub Desktop.
Save jlucaspains/92d71fd346106e657138f796ab3a708c to your computer and use it in GitHub Desktop.
# remember to run this script as an Admin
Import-Module SQLServer
# get open PRs from your repository
$B64Pat = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("your pat token goes here"))
$header = @{Authorization = "Basic $B64Pat"}
$url = "https://dev.azure.com/your-org/your-project/_apis/git/pullrequests?repositoryId=d26dc76c-6593-4612-acde-4ac37af9cf2f&includeCommits=false&includeWorkItemRefs=false"
$prArray = (Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers $header).value
# find the existing environments in the VM
$envs = Get-ChildItem -filter "lpainsPR*" -Directory | Select-Object Fullname, Name
foreach($env in $envs){
$prId = $env.Name -replace "lpainsPR", ""
# find the PR by number
$pr = $prArray | Where-Object { $_.pullRequestId -eq $prId }
# if a PR is not found for the environment, tear it down!
if ($null -eq $pr ) {
Write-Host "Removing PR$prId"
# delete web sites
appcmd.exe delete site "$($env.Name)_UI"
appcmd.exe delete site "$($env.Name)_API"
# delete app pools
appcmd.exe delete apppool "$($env.Name)_UI"
appcmd.exe delete apppool "$($env.Name)_API"
# delete files
Remove-Item -Recurse -Force $env.FullName
# drop database
Invoke-Sqlcmd -ServerInstance LPainsDB -database master -query "DROP DATABASE $($env.Name)"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment