Skip to content

Instantly share code, notes, and snippets.

@adbertram
Created June 13, 2016 18:15
Show Gist options
  • Save adbertram/8d1ede9387cb6c6875d8e6973b1af306 to your computer and use it in GitHub Desktop.
Save adbertram/8d1ede9387cb6c6875d8e6973b1af306 to your computer and use it in GitHub Desktop.
function New-TfsWorkItem
{
[OutputType([void])]
[CmdletBinding()]
param
(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$Title,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[ValidateSet('Adam Bertram','Steve Evans','Paul Shamus')]
[string]$AssignedTo,
[Parameter()]
[ValidateNotNullOrEmpty()]
[ValidateSet(1,2,3,4,5)]
[int]$Priority = 2,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$ProjectName = 'IS-DevOps',
[Parameter()]
[ValidateNotNullOrEmpty()]
[Microsoft.TeamFoundation.Client.TfsTeamProjectCollection]$ProjectCollection = (Get-TfsProjectCollection -Uri 'http://tfs:8080/tfs')
)
DynamicParam
{
$RuntimeParamDic = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$workItemStore = $ProjectCollection.GetService([Microsoft.TeamFoundation.WorkItemTracking.Client.workitemstore])
$project = $workItemStore.Projects[$ProjectName]
$dynParams = @(
@{
'Name' = 'IterationPath'
'Mandatory' = $true
'ValidateSetOptions' = $project.IterationRootNodes.foreach({ $_.Path; if ($_.HasChildNodes) { $_.ChildNodes.Path } })
},
@{
'Name' = 'Type'
'Mandatory' = $true
'ValidateSetOptions' = $project.WorkItemTypes.Name
},
@{
'Name' = 'AreaPath'
'Mandatory' = $true
'ValidateSetOptions' = $project.AreaRootNodes.foreach({ $_.Path; if ($_.HasChildNodes) { $_.ChildNodes.Path } })
}
)
@($dynParams).foreach({
$RuntimeParam = New-DynamicParameter @_
$RuntimeParamDic.Add($_.Name, $RuntimeParam)
})
$RuntimeParamDic
}
begin {
Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState
Write-Log -Source $MyInvocation.MyCommand -Message "$($MyInvocation.MyCommand) : Entering"
$ErrorActionPreference = 'Stop'
$PsBoundParameters.GetEnumerator() | foreach { New-Variable -Name $_.Key -Value $_.Value -ea 'SilentlyContinue' }
}
process {
try
{
$workItemType = $project.WorkItemTypes[$PSBoundParameters.Type]
$workItem = New-Object -TypeName 'Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem' -ArgumentList $workItemType
$workItem['Priority'] = $Priority
$workItem['Assigned To'] = $AssignedTo
$workItem['Area Path'] = $AreaPath
$workItem['Iteration Path'] = $IterationPath
$workItem['Title'] = $Title
$workItem.Save()
}
catch
{
Write-Log -Source $MyInvocation.MyCommand -EventId 1003 -EntryType 'Error' -Message ($_ | Resolve-Error)
}
}
end {
Write-Log -Source $MyInvocation.MyCommand -Message ('{0}: Exiting' -f $MyInvocation.MyCommand)
$ErrorActionPreference = 'Continue'
}
}
function New-DynamicParameter
{
[CmdletBinding()]
[OutputType('System.Management.Automation.RuntimeDefinedParameter')]
param (
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$Name,
[ValidateNotNullOrEmpty()]
[Parameter()]
[array]$ValidateSetOptions,
[Parameter()]
[switch]$Mandatory = $false,
[Parameter()]
[string]$ParameterSetName = '__AllParameterSets',
[Parameter()]
[switch]$ValueFromPipeline = $false,
[Parameter()]
[switch]$ValueFromPipelineByPropertyName = $false
)
$AttribColl = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
$ParamAttrib = New-Object System.Management.Automation.ParameterAttribute
$ParamAttrib.Mandatory = $Mandatory.IsPresent
$ParamAttrib.ParameterSetName = $ParameterSetName
$ParamAttrib.ValueFromPipeline = $ValueFromPipeline.IsPresent
$ParamAttrib.ValueFromPipelineByPropertyName = $ValueFromPipelineByPropertyName.IsPresent
$AttribColl.Add($ParamAttrib)
if ($PSBoundParameters.ContainsKey('ValidateSetOptions'))
{
$AttribColl.Add((New-Object System.Management.Automation.ValidateSetAttribute($ValidateSetOptions)))
}
$RuntimeParam = New-Object System.Management.Automation.RuntimeDefinedParameter($Name, [string], $AttribColl)
$RuntimeParam
}
#endregion function New-TfsWorkItem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment