Private/Get-TeamAndChannel.ps1
# Module: Orbit # Function: Assertion # Author: David Eberhardt # Updated: 15-MAY-2021 # Status: Live function Get-TeamAndChannel { <# .SYNOPSIS Queries Team and Channel based on input .DESCRIPTION Used by Get-TeamsCallableEntity .PARAMETER String String in on of the formats: TeamId\ChannelId, TeamId\ChannelDisplayName, TeamDisplayName,ChannelId or TeamDisplayName\ChannelDisplayName .EXAMPLE Get-TeamAndChannel -String "00000000-0000-0000-0000-000000000000\19:abcdef1234567890abcdef1234567890@thread.tacv2" String is expected in the format "<Team Guid or DN>\<Channel Guid or DN>" .NOTES This helper function is targeted by Get-TeamsCallQueue as well as Get-TeamsCallableEntity Avoids having to wait for Get-TeamsCallableEntity #> [CmdletBinding()] [OutputType([PSCustomObject[]])] param( [ValidateScript( { If ($_ -match '\\') { $True } else { throw [System.Management.Automation.ValidationMetadataException] "String must contain one '\'-character" } })] [string]$String ) #param #Show-OrbitFunctionStatus -Level Live # Setting Preference Variables according to Upstream settings if (-not $PSBoundParameters['Verbose']) { $VerbosePreference = $PSCmdlet.SessionState.PSVariable.GetValue('VerbosePreference') } $DebugPreference = if (-not $PSBoundParameters['Debug']) { $PSCmdlet.SessionState.PSVariable.GetValue('DebugPreference') } else { 'Continue' } $InformationPreference = if ( $PSBoundParameters['InformationAction']) { $PSCmdlet.SessionState.PSVariable.GetValue('InformationAction') } else { 'Continue' } Write-Verbose -Message "[PROCESS] $($MyInvocation.MyCommand) - Processing Team & Channel for: '$String'" $TeamId, $ChannelId = $String.split('\') if ($PSBoundParameters['Debug'] -or $DebugPreference -eq 'Continue') { " Function: $($MyInvocation.MyCommand.Name) - Team:", ($TeamId | Format-Table -AutoSize | Out-String).Trim() | Write-Debug " Function: $($MyInvocation.MyCommand.Name) - Channel:", ($ChannelId | Format-Table -AutoSize | Out-String).Trim() | Write-Debug } # Finding and returning Team try { $Team = $null if ( $script:OrbitRegexGuid.isMatch($TeamId) ) { Write-Verbose -Message "$($MyInvocation.MyCommand) - Querying Team with GroupId: '$TeamId'" $Team = Get-Team -GroupId $TeamId -ErrorAction Stop } else { Write-Verbose -Message "$($MyInvocation.MyCommand) - Querying Team with DisplayName: '$TeamId'" $Team = Get-Team -DisplayName "$TeamId" -ErrorAction Stop } if ( $Team ) { if ($PSBoundParameters['Debug'] -or $DebugPreference -eq 'Continue') { " Function: $($MyInvocation.MyCommand.Name) - Team:", ($Team | Format-Table -AutoSize | Out-String).Trim() | Write-Debug } # dealing with potential duplicates if ( $Team.Count -gt 1 ) { Write-Verbose "$($MyInvocation.MyCommand) - Target is a Team\Channel, but multiple Teams found" $Team = $Team | Where-Object DisplayName -EQ "$($Team.DisplayName)" } if ( $Team.Count -gt 1 ) { Write-Verbose "$($MyInvocation.MyCommand) - Target is a Team\Channel, but not unique!" throw [System.Reflection.AmbiguousMatchException]::New('Multiple Targets found - Result not unique (Team)') } Write-Verbose -Message "Returning Team with DisplayName: '$($Team.DisplayName)'" Write-Output $Team } else { throw "$($MyInvocation.MyCommand) - Lookup for Team & Channel - No Team found for '$TeamId'." } } catch { throw "$($MyInvocation.MyCommand) - Lookup for Team & Channel - Team not found. Exception: $($_.Exception.Message)" } # Finding and returning Channel try { $Channel = $null if ( $script:OrbitRegexChannelGuid.isMatch($ChannelId) ) { Write-Verbose -Message "$($MyInvocation.MyCommand) - Querying Channel with Id: '$ChannelId'" $Channel = Get-TeamChannel -GroupId $Team.GroupId | Where-Object Id -EQ $ChannelId -ErrorAction Stop } else { Write-Verbose -Message "$($MyInvocation.MyCommand) - Querying Channel with DisplayName: '$ChannelId'" $Channel = Get-TeamChannel -GroupId $Team.GroupId | Where-Object DisplayName -EQ "$ChannelId" -ErrorAction Stop } if ( $Channel ) { if ($PSBoundParameters['Debug'] -or $DebugPreference -eq 'Continue') { " Function: $($MyInvocation.MyCommand.Name) - Channel:", ($Channel | Format-Table -AutoSize | Out-String).Trim() | Write-Debug } # dealing with potential duplicates if ( $Team.Count -gt 1 ) { Write-Verbose "$($MyInvocation.MyCommand) - Target is a Team\Channel, but multiple Channels found" $Team = $Team | Where-Object DisplayName -EQ "$String" } if ( $Team.Count -gt 1 ) { Write-Verbose "$($MyInvocation.MyCommand) - Target is a Team\Channel, but not unique!" throw [System.Reflection.AmbiguousMatchException]::New('Multiple Targets found - Result not unique (Channel)') } Write-Verbose -Message "$($MyInvocation.MyCommand) - Returning Channel with DisplayName: '$($Channel.DisplayName)'" Write-Output $Channel } else { throw "$($MyInvocation.MyCommand) - Lookup for Team & Channel - Team found, but no Channel found with this Name/Guid: '$ChannelId'" } } catch { throw "$($MyInvocation.MyCommand) - Lookup for Team & Channel - Channel not found. Exception: $($_.Exception.Message)" } } #Get-TeamAndChannel |