TFSPowershell.Tfs.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#Assemblies folder for TFS Object Model Client
$rootPath = $PSScriptRoot
$AssembliesDir = "$rootPath\Assemblies"

Add-Type -Path "$AssembliesDir\Microsoft.TeamFoundation.Client.dll",
               "$AssembliesDir\Microsoft.TeamFoundation.Common.dll",
               "$AssembliesDir\Microsoft.VisualStudio.Services.Common.dll",
               "$AssembliesDir\Microsoft.TeamFoundation.WorkItemTracking.Client.DataStoreLoader.dll",                                        
               "$AssembliesDir\Microsoft.TeamFoundation.WorkItemTracking.Client.dll"

# .ExternalHelp .\MAML\TFSPowershell.Tfs.Help.xml
Function Get-TeamProjectCollection
{
    [CmdletBinding()]
    Param (
        [Parameter(mandatory=$true)]
        [string] $CollectionUrl
    )
    
    # Get TFS
    $tfs = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($CollectionUrl)
    $tfs.EnsureAuthenticated()

    Write-Output $tfs
}

# .ExternalHelp .\MAML\TFSPowershell.Tfs.Help.xml
Function Get-TeamProject
{
    [CmdletBinding()]
    Param (
        [string] $CollectionUrl,

        [Parameter(mandatory=$true)]
        [string] $TeamProjectName,

        [Parameter(ValueFromPipeline=$True)]
        [Microsoft.TeamFoundation.Client.TfsTeamProjectCollection]$Tfs = $null
    )

    if ($Tfs -eq $null)
    {
        $Tfs = Get-TeamProjectCollection -CollectionUrl $CollectionUrl
    }

    # Get Team Project
    $cssService = $Tfs.GetService("Microsoft.TeamFoundation.Server.ICommonStructureService4")
    Write-Output $cssService.GetProjectFromName($TeamProjectName)
}

# .ExternalHelp .\MAML\TFSPowershell.Tfs.Help.xml
Function Get-AllTFSAlerts
{
    [CmdletBinding()]
    Param (
        [string] $CollectionUrl,

        [Parameter(ValueFromPipeline=$True)]
        [Microsoft.TeamFoundation.Client.TfsTeamProjectCollection]$Tfs = $null
    )

    if ($Tfs -eq $null)
    {
        $Tfs = Get-TeamProjectCollection -CollectionUrl $CollectionUrl
    }

    $eventService = $tfs.GetService("Microsoft.TeamFoundation.Framework.Client.IEventService")
    $identityService = $tfs.GetService("Microsoft.TeamFoundation.Framework.Client.IIdentityManagementService")
 
    foreach ($sub in $eventService.GetAllEventSubscriptions())
    {
        #First resolve the subscriber ID
        $tfsId = $identityService.ReadIdentity([Microsoft.TeamFoundation.Framework.Common.IdentitySearchFactor]::Identifier, 
                                               $sub.Subscriber,
                                               [Microsoft.TeamFoundation.Framework.Common.MembershipQuery]::None, 
                                               [Microsoft.TeamFoundation.Framework.Common.ReadIdentityOptions]::None )
        if ($tfsId.UniqueName)
        {
            $subscriberId = $tfsId.UniqueName
        }
        else
        {
            $subscriberId = $tfsId.DisplayName
        }
 
        #then create custom PSObject
        $subPSObj = New-Object PSObject -Property @{
                        ID             = $sub.ID
                        Device         = $sub.Device
                        Condition      = $sub.ConditionString
                        EventType      = $sub.EventType
                        Address        = $sub.DeliveryPreference.Address
                        Schedule       = $sub.DeliveryPreference.Schedule
                        DeliveryType   = $sub.DeliveryPreference.Type
                        SubscriberName = $subscriberId
                        Tag            = $sub.Tag
                        }
 
        #Send object to the pipeline. You could store it on an Arraylist, but that just
        #consumes more memory
        $subPSObj
    }
}

# .ExternalHelp .\MAML\TFSPowershell.Tfs.Help.xml
Function New-TFSAlert
{
    #FilterExpression "CoreFields/StringFields/Field[Name='Area Path']/NewValue" = '\AreaPath' AND
    # "CoreFields/StringFields/Field[Name='Authorized As']/NewValue" <> '@@MyDisplayName@@' AND
    # ("CoreFields/StringFields/Field[Name='Work Item Type']/NewValue" = 'Requirement' OR
    # "CoreFields/StringFields/Field[Name='Work Item Type']/NewValue" = 'Issue' OR
    # "CoreFields/StringFields/Field[Name='Work Item Type']/NewValue" = 'Bug') AND
    # " ""CoreFields/StringFields/Field[Name='State']/NewValue"" <> ""CoreFields/StringFields/Field[Name='State']/OldValue"" "
    [CmdletBinding()]
    Param (
        [string] $CollectionUrl,

        [Parameter(mandatory=$true)]
        [string] $AlertName,

        [Parameter(mandatory=$true)]
        [string] $SubscriberAccountName,

        [Parameter(mandatory=$true)]
        [string] $FilterExpression,

        [Parameter(mandatory=$true)]
        [string] $EventType,

        [Parameter(ValueFromPipeline=$True)]
        [Microsoft.TeamFoundation.Client.TfsTeamProjectCollection]$Tfs = $null
    )

    Write-Verbose "Connecting to TFS..."
    if ($Tfs -eq $null)
    {
        $Tfs = Get-TeamProjectCollection -CollectionUrl $CollectionUrl
    }

    $eventService = $tfs.GetService("Microsoft.TeamFoundation.Framework.Client.IEventService")
    $identityService = $tfs.GetService("Microsoft.TeamFoundation.Framework.Client.IIdentityManagementService")

    Write-Verbose "Retrieving account identification $SubscriberAccountName..."
    $subsIdentity = $identityService.ReadIdentity([Microsoft.TeamFoundation.Framework.Common.IdentitySearchFactor]::AccountName,
                                             $SubscriberAccountName,
                                             [Microsoft.TeamFoundation.Framework.Common.MembershipQuery]::None,
                                             [Microsoft.TeamFoundation.Framework.Common.ReadIdentityOptions]::None)
    $subsID = $subsIdentity.Descriptor.Identifier

    Write-Verbose "Creating alert $AlertName..."
    $deliveryPreference = New-Object Microsoft.TeamFoundation.Framework.Client.DeliveryPreference 
    $deliveryPreference.Schedule = [Microsoft.TeamFoundation.Framework.Client.DeliverySchedule]::Immediate
    $deliveryPreference.Type = [Microsoft.TeamFoundation.Framework.Client.DeliveryType]::EmailHtml
    $eventID = $eventService.SubscribeEvent($subsID,$EventType,$FilterExpression,$deliveryPreference,$AlertName)
    Write-Verbose "Done."
    Write-Output $eventID  
}

# .ExternalHelp .\MAML\TFSPowershell.Tfs.Help.xml
Function Remove-TFSAlert
{
    [CmdletBinding()]
    Param (
        [string] $CollectionUrl,

        [Parameter(mandatory=$true)]
        [string] $AlertName,

        [Parameter(mandatory=$true)]
        [string] $SubscriberAccountName,

        [Parameter(ValueFromPipeline=$True)]
        [Microsoft.TeamFoundation.Client.TfsTeamProjectCollection]$Tfs = $null
    )
 
    Write-Verbose "Connecting to TFS..."
    if ($Tfs -eq $null)
    {
        $Tfs = Get-TeamProjectCollection -CollectionUrl $CollectionUrl
    }

    $eventService = $tfs.GetService("Microsoft.TeamFoundation.Framework.Client.IEventService")
    $identityService = $tfs.GetService("Microsoft.TeamFoundation.Framework.Client.IIdentityManagementService")

    Write-Verbose "Retrieving account identification $SubscriberAccountName..."
    $subsIdentity = $identityService.ReadIdentity([Microsoft.TeamFoundation.Framework.Common.IdentitySearchFactor]::AccountName,
                                             $SubscriberAccountName,
                                             [Microsoft.TeamFoundation.Framework.Common.MembershipQuery]::None,
                                             [Microsoft.TeamFoundation.Framework.Common.ReadIdentityOptions]::None)
    $subscriberID = $subsIdentity.Descriptor.Identifier

    Write-Verbose "Retrieving alert $AlertName information..."
    $subscriptions = $eventService.GetEventSubscriptions($subscriberID, "$AlertName")

    Write-Verbose "Retrieving alert ID..."
    $subscriptionId = $subscriptions[0].ID

    Write-Verbose "Removing alert $AlertName..."
    $eventService.UnsubscribeEvent($subscriptionId)
    Write-Verbose "Done."
}

# .ExternalHelp .\MAML\TFSPowershell.Tfs.Help.xml
Function Remove-TFSAlertById
{
    [CmdletBinding()]
    Param (
        [string] $CollectionUrl,

        [Parameter(mandatory=$true,ValueFromPipeline = $true)]
        [int] $Id,

        [Microsoft.TeamFoundation.Client.TfsTeamProjectCollection]$Tfs = $null
    )
 
    Write-Verbose "Connecting to TFS..."
    if ($Tfs -eq $null)
    {
        $Tfs = Get-TeamProjectCollection -CollectionUrl $CollectionUrl
    }

    $eventService = $Tfs.GetService("Microsoft.TeamFoundation.Framework.Client.IEventService")

    Write-Verbose "Removing alert $AlertName..."
    $eventService.UnsubscribeEvent($Id)
    Write-Verbose "Done."
}

# .ExternalHelp .\MAML\TFSPowershell.Tfs.Help.xml
Function Get-GlobalLists
{
    [CmdletBinding()]
    Param (
        [string] $CollectionUrl,
        [Microsoft.TeamFoundation.Client.TfsTeamProjectCollection]$Tfs = $null
    )
    Write-Verbose "Connecting to TFS..."
    if ($Tfs -eq $null)
    {
        $Tfs = Get-TeamProjectCollection -CollectionUrl $CollectionUrl
    }

    $workItemStore = $tfs.GetService("Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore")
    return $workItemStore.ExportGlobalLists() 
}

# .ExternalHelp .\MAML\TFSPowershell.Tfs.Help.xml
Function Set-GlobalLists
{
    [CmdletBinding()]
    Param (
        [string] $CollectionUrl,
        [Parameter(mandatory=$true)]
        [string] $GlobalLists,
        [Microsoft.TeamFoundation.Client.TfsTeamProjectCollection]$Tfs = $null
    )
    Write-Verbose "Connecting to TFS..."
    if ($Tfs -eq $null)
    {
        $Tfs = Get-TeamProjectCollection -CollectionUrl $CollectionUrl
    }
                      
    $workItemStore = $tfs.GetService("Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore")
    $workItemStore.ImportGlobalLists($GlobalLists)   
}