Skip to content

Instantly share code, notes, and snippets.

@ryanvgates
Last active May 11, 2016 04:37
Show Gist options
  • Save ryanvgates/85a882a45afc5ae69510c6dead0008bd to your computer and use it in GitHub Desktop.
Save ryanvgates/85a882a45afc5ae69510c6dead0008bd to your computer and use it in GitHub Desktop.
Powershell and TFS Deployment Email
$From = $env:username + "@domain.com"
$TFSServer = "http://tfsServer:8080/tfs/Collection"
$body = ""
$Subject = "IMPORTANT: QA DEPLOYMENT for Your Site Announcement COMPLETE"
$recipients = $From
#Prevent e-mails from going out before you are ready
#$recipients = "Deployment QA Distribution List"
$cc = ""
$bcc = $From
$Pbis = "10899", "10950" #enter PBI's here
$QAAssignee = 'QA Rockstar'
Function Get-WorkItems
{
$query = "SELECT [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.IterationPath] " +
"FROM WorkItems WHERE [System.Id] In (" + [System.String]::Join(",", $Pbis) + ")"
Write-Host $query
tfpt query /format:tsv /collection:$TFSServer /wiql:$query
foreach ($PBI in $Pbis)
{
Write-Host "Setting Assigned To = $QAAssignee for PBI # $PBI"
$fields = "Assigned To=$QAAssignee"
# This just captures the output so it doesn't get included in the e-mail below
$test = tfpt workitem /collection:$TFSServer /update $PBI /fields:$fields
}
$query = ""
}
Function TsvToHtmlTable()
{
$results = Get-WorkItems
$results = $results -replace "(.*)Query results:.*", "$1"
$count = 0;
$table = "<table>"
$class = "gray"
foreach ($line in $results)
{
if (![string]::IsNullOrWhiteSpace($line))
{
$table += "<tr class='" + $class + "' id='" + $count + "'>"
$class = if($count % 2 -eq 0) { "even" } else { "odd" }
foreach($chunk in $line.Split("`t"))
{
if (![string]::IsNullOrWhiteSpace($chunk))
{
$table += "<td>" + $chunk.Trim() + "</td>"
}
}
$table += "</tr>`r`n"
$count += 1
}
}
return $table + "</td></tr></table>"
}
$body += @"
<style type='text/css'>
table { width:100%; border-collapse:collapse}
table td{ padding:7px; border:black 1px solid}
.gray {background: #808080}
.even {background: #b8d1f3}
tr#0 {font-weight: bold}
</style>
"@
$body += "<b>Hello,</b><br><br>`r`n"
$body += "<b>Summary</b><br><br>We just completed a QA deployment for the product (" + [System.DateTime]::Now.ToString() + ")<br><br>`r`n"
$body += "<b>Environment</b><br>QA<br>The servers involved can be accessed at: "
$body += "<br>http://url1<br>`r`n"
$body += "<br>http://url2<br><br>`r`n"
$body += "<b>Actions</b><br>The following items will have to be tested in the QA environment after deployment.<br><br>`r`n"
$body += TsvToHtmlTable
Send-MailMessage `
-Subject $Subject `
-BodyAsHtml $body `
-From $From `
-To $recipients `
-Bcc $From `
-SmtpServer "smtpServer" `
-port 25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment