diff --git a/JiraPS/Public/Get-JiraUser.ps1 b/JiraPS/Public/Get-JiraUser.ps1 index 807d7587..b8464eb0 100644 --- a/JiraPS/Public/Get-JiraUser.ps1 +++ b/JiraPS/Public/Get-JiraUser.ps1 @@ -8,18 +8,25 @@ function Get-JiraUser { [String[]] $UserName, + [Parameter( Position = 0, Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = 'ByFilter' )] + [ValidateNotNullOrEmpty()] + [Alias('Search')] + [String[]] + $Filter, + [Parameter( Position = 0, Mandatory, ParameterSetName = 'ByInputObject' )] [Object[]] $InputObject, + [Parameter( ParameterSetName = 'ByFilter' )] [Switch] $IncludeInactive, - [Parameter( ParameterSetName = 'ByUserName' )] + [Parameter( ParameterSetName = 'ByFilter' )] [ValidateRange(1, 1000)] [UInt32] $MaxResults = 50, - [Parameter( ParameterSetName = 'ByUserName' )] + [Parameter( ParameterSetName = 'ByFilter' )] [ValidateNotNullOrEmpty()] [UInt64] $Skip = 0, @@ -36,6 +43,7 @@ function Get-JiraUser { $server = Get-JiraConfigServer -ErrorAction Stop $selfResourceUri = "$server/rest/api/latest/myself" + $getResourceUri = "$server/rest/api/latest/user?username={0}&expand=groups" $searchResourceUri = "$server/rest/api/latest/user/search?username={0}" if ($IncludeInactive) { @@ -57,6 +65,7 @@ function Get-JiraUser { switch ($PsCmdlet.ParameterSetName) { 'ByInputObject' { $UserName = $InputObject.Name; $ParameterSetName = 'ByUserName' } 'ByUserName' { $ParameterSetName = 'ByUserName' } + 'ByFilter' { $ParameterSetName = 'ByFilter' } 'Self' { $ParameterSetName = 'Self' } } @@ -80,12 +89,38 @@ function Get-JiraUser { $PsCmdlet.ParameterSetName = "ByUserName" } "ByUserName" { - $resourceURi = $searchResourceUri + $resourceURi = $getResourceUri foreach ($user in $UserName) { Write-Verbose "[$($MyInvocation.MyCommand.Name)] Processing [$user]" Write-Debug "[$($MyInvocation.MyCommand.Name)] Processing `$user [$user]" + $parameter = @{ + URI = $resourceURi -f $user + Method = "GET" + Credential = $Credential + } + Write-Debug "[$($MyInvocation.MyCommand.Name)] Invoking JiraMethod with `$parameter" + if ($result = Invoke-JiraMethod @parameter) { + Write-Output (ConvertTo-JiraUser -InputObject $result) + } + else { + $errorMessage = @{ + Category = "ObjectNotFound" + CategoryActivity = "Getting user" + Message = "No results when getting user $user" + } + Write-Error @errorMessage + } + } + } + "ByFilter" { + $resourceURi = $searchResourceUri + + foreach ($user in $Filter) { + Write-Verbose "[$($MyInvocation.MyCommand.Name)] Processing [$user]" + Write-Debug "[$($MyInvocation.MyCommand.Name)] Processing `$user [$user]" + $parameter = @{ URI = $resourceURi -f $user Method = "GET" diff --git a/Tests/Get-JiraUser.Tests.ps1 b/Tests/Get-JiraUser.Tests.ps1 index 203ea66a..e99d8863 100644 --- a/Tests/Get-JiraUser.Tests.ps1 +++ b/Tests/Get-JiraUser.Tests.ps1 @@ -133,7 +133,7 @@ Describe "Get-JiraUser" { } It "Allow it search for multiple users" { - Get-JiraUser -UserName "%" + Get-JiraUser -Filter "%" Assert-MockCalled -CommandName Invoke-JiraMethod -Exactly 1 -Scope It -ParameterFilter { $URI -like "$jiraServer/rest/api/*/user/search?*username=%25*" @@ -141,7 +141,7 @@ Describe "Get-JiraUser" { } It "Allows to change the max number of users to be returned" { - Get-JiraUser -UserName "%" -MaxResults 100 + Get-JiraUser -Filter "%" -MaxResults 100 Assert-MockCalled -CommandName Invoke-JiraMethod -Exactly 1 -Scope It -ParameterFilter { $URI -like "$jiraServer/rest/api/*/user/search?*maxResults=100*" @@ -149,7 +149,7 @@ Describe "Get-JiraUser" { } It "Can skip a certain amount of results" { - Get-JiraUser -UserName "%" -Skip 10 + Get-JiraUser -Filter "%" -Skip 10 Assert-MockCalled -CommandName Invoke-JiraMethod -Exactly 1 -Scope It -ParameterFilter { $URI -like "$jiraServer/rest/api/*/user/search?*startAt=10*" diff --git a/docs/en-US/commands/Get-JiraUser.md b/docs/en-US/commands/Get-JiraUser.md index 76d9d06c..16f79642 100644 --- a/docs/en-US/commands/Get-JiraUser.md +++ b/docs/en-US/commands/Get-JiraUser.md @@ -24,7 +24,13 @@ Get-JiraUser [-Credential ] [] ### ByUserName ```powershell -Get-JiraUser [-UserName] [-IncludeInactive] [[-MaxResults] ] [[-Skip] ] [-Credential ] [] +Get-JiraUser [-UserName] [-Credential ] [] +``` + +### ByFilter + +```powershell +Get-JiraUser [-Filter] [-IncludeInactive] [[-MaxResults] ] [[-Skip] ] [-Credential ] [] ``` ### ByInputObject @@ -49,13 +55,21 @@ Returns information about the user user1 ### EXAMPLE 2 +```powershell +Get-JiraUser -Filter 'John' +``` + +Returns information about all user(s) whose username, display name or email address matches 'John'. The string is matched to the starting letters of any word in the searched fields. For example, 'and' matches to the username 'Andrei' but not 'Alexander' + +### EXAMPLE 3 + ```powershell Get-ADUser -filter "Name -like 'John*Smith'" | Select-Object -ExpandProperty samAccountName | Get-JiraUser -Credential $cred ``` This example searches Active Directory for "John*Smith", then obtains their JIRA user accounts. -### EXAMPLE 3 +### EXAMPLE 4 ```powershell Get-JiraUser -Credential $cred @@ -67,7 +81,7 @@ This example returns the JIRA user that is executing the command. ### -UserName -Name of the user to search for. +Name of the user to return information for. ```yaml Type: String[] @@ -81,6 +95,22 @@ Accept pipeline input: True (ByPropertyName) Accept wildcard characters: False ``` +### -Filter + +Name of the user to search for. + +```yaml +Type: String[] +Parameter Sets: ByFilter +Aliases: + +Required: True +Position: 1 +Default value: None +Accept pipeline input: True (ByPropertyName) +Accept wildcard characters: False +``` + ### -InputObject User Object of the user. @@ -103,7 +133,7 @@ Include inactive users in the search ```yaml Type: SwitchParameter -Parameter Sets: (All) +Parameter Sets: ByFilter Aliases: Required: False @@ -121,7 +151,7 @@ Maximum number of user to be returned. ```yaml Type: UInt32 -Parameter Sets: ByUserName +Parameter Sets: ByFilter Aliases: Required: False @@ -139,7 +169,7 @@ Defaults to 0. ```yaml Type: UInt64 -Parameter Sets: ByUserName +Parameter Sets: ByFilter Aliases: Required: False