Skip to content

Instantly share code, notes, and snippets.

@iqwirty
Created April 18, 2014 18:46
Show Gist options
  • Save iqwirty/11058738 to your computer and use it in GitHub Desktop.
Save iqwirty/11058738 to your computer and use it in GitHub Desktop.
Gets a list of recent changes in a few TFS servers and emails as a report
Clear-Host
Set-PSDebug -Trace 0
Set-StrictMode -Version Latest
#
# Function Send()
#
# Sends an email using the specified to/from, subject, body, and
# and SMTP server. Allows an attachment to be included. Also includes
# in the body the source path of the running script to improve
# traceability.
Function Send
{
Param (
[string]$To,
[string]$From,
[string]$Server,
[string]$Subject,
[string]$Body,
[string]$AttachmentFullPath
)
$parentInvocation = (Get-Variable MyInvocation -Scope 1).Value
$formattedBody = "$Body`r`n`r`nSent by a script at:`r`n$($parentInvocation.ScriptName)"
Send-MailMessage -To $To `
-From $From `
-SmtpServer $Server `
-Subject $Subject `
-Body $formattedBody `
-Attachments $AttachmentFullPath
}
#
# Main
#
If ((Get-PSSnapin -Name Microsoft.TeamFoundation.PowerShell -ErrorAction SilentlyContinue) -eq $null)
{
Add-PSSnapin Microsoft.TeamFoundation.PowerShell
}
#
# Global variables
#
# Email configuration
[string] $email = "emailaddress"
[string] $smtp = "smtpserver"
[string] $subject = "TFS recent change report - 14 days"
[string] $body = "See attached log of recent changes in TFS."
# Path to root of TFS
[string] $tfsProjectRoot = "$/"
# Path to output log file
[string] $log = "$($Env:TEMP)\tfs_$(Get-Date -Format yyyy-MM-dd_hh_mm_ss).log"
# Cutoff days / date for the search in TFS
[int] $days = 14
[string] $date = (Get-Date).AddDays(-$days).ToString("Dyyyy-MM-dd 00:00:00Z~")
# Paths to the TFS servers we want to query
[string[]] $pathsToTfs = @("http://tfsserver1:8080/tfs/Name1",
"http://tfsserver2:8080/tfs/Name2")
ForEach ($p in $pathsToTfs) `
{
# Write the current TFS path as a header to the log file
$p | Out-File $log -Append
# Connect to the next TFS server
[Microsoft.TeamFoundation.Client.TfsTeamProjectCollection] `
$tfs = Get-TfsServer $p
# Get the recent history, filtered down to the info we need
Get-TfsItemHistory $tfsProjectRoot -Server $tfs -Version $date `
-Recurse -IncludeItems |
Select -Expand "Changes" |
Where { $_.ChangeType -notlike "*Delete*" } |
Where { $_.ChangeType -notlike "*Rename*" } |
Select -Expand "Item" |
Where { $_.ContentLength -gt 0 } |
Where { $_.ServerItem -notlike "*/buildtargets/*" } |
Where { $_.ServerItem -notlike "*.csproj" } |
Where { $_.ServerItem -notlike "*.vspscc" } |
Select ServerItem, CheckinDate |
Sort CheckinDate |
Format-Table -Property * -AutoSize | Out-String -Width 4096 |
Out-File $log -Append
}
Send -To $email -From $email -Server $smtp -Subject $subject `
-Body $body -AttachmentFullPath $log
@raajheshkannaa
Copy link

Is there a way I could extract owner email addresses of each changed file and send an email to them with this report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment