Class/TeamsFunctions.ArgumentCompleter.psm1

# Module: TeamsFunctions
# Function: ArgumentCompleter
# Author: David Eberhardt
# Updated: 10-JUL 2022
# Status: Live




# ArgumentCompleter
# Defines scripts blocks to be used either through [ArgumentCompleter] in the Function or through Register-ArgumentCompleter
# Naming convention TeamsFunctionsArgumentCompleterScriptBlock (TfAcSb) followed by the logical name.
<# Usage
    [Parameter(ParameterSetName = 'OVP', HelpMessage = 'Filters based on OnlineVoiceRoutingPolicy')]
    [AllowNull()]
    [Alias('OVP')]
    [ValidateScript( {
        if ($_ -in $(&$global:TfAcSbVoiceRoutingPolicy)) { return $true } else {
          throw [System.Management.Automation.ValidationMetadataException] "Value must be a valid Policy in the Tenant. Use Intellisense for options"
        } })]
    [ArgumentCompleter({ &$global:TfAcSbVoiceRoutingPolicy })]
    [String]$OnlineVoiceRoutingPolicy,
#>


# CsOnlinePSTNGateway
$global:TfAcSbTeasmsPSTNGateway = {
  $null = Assert-MicrosoftTeamsConnection
  (Get-CsOnlinePSTNGateway | Select-Object Identity).Identity
}

# CsOnlineVoiceRoute
$global:TfAcSbOnlineVoiceRoute = {
  $null = Assert-MicrosoftTeamsConnection
  (Get-CsOnlineVoiceRoute | Select-Object Identity).Identity
}

# CsOnlinePstnUsage
$global:TfAcSbOnlinePstnUsage = {
  $null = Assert-MicrosoftTeamsConnection
  (Get-CsOnlinePstnUsage Global).Usage
}

# CsOnlineVoiceRoutingPolicy
$global:TfAcSbVoiceRoutingPolicy = {
  $null = Assert-MicrosoftTeamsConnection
  $((Get-CsOnlineVoiceRoutingPolicy | Where-Object Identity -NE Global | Sort-Object -Property Identity).Identity -replace 'Tag:', '')
}

# CsTenantDialPlan
$global:TfAcSbTenantDialPlan = {
  $null = Assert-MicrosoftTeamsConnection
  if (-not $global:TeamsFunctionsTenantDialPlan) {
    $global:TeamsFunctionsTenantDialPlan = Get-CsTenantDialPlan | Sort-Object -Property Identity
  }
  $($global:TeamsFunctionsTenantDialPlan.Identity -replace 'Tag:', '')
}

# NormalizationRules - replacement for CsVoiceNormalizationRule
$global:TfAcSbTenantDialPlanNormalizationRuleIds = {
  $null = Assert-MicrosoftTeamsConnection
  if (-not $global:TeamsFunctionsTenantDialPlan) {
    $global:TeamsFunctionsTenantDialPlan = (Get-CsTenantDialPlan | Sort-Object -Property Identity).Identity -replace 'Tag:', ''
  }
  if (-not $global:TeamsFunctionsTenantDialPlanNormalizationRule) {
    $global:TeamsFunctionsTenantDialPlanNormalizationRuleIds = $global:TeamsFunctionsTenantDialPlan | ForEach-Object {
      $DPName = $_.Identity -replace 'Tag:', ''
      $_.NormalizationRules | ForEach-Object { "$DPName\$($_.Name)" }
    }
  }
  $global:TeamsFunctionsTenantDialPlanNormalizationRuleIds
}

# CsTeamsIPPhonePolicy
$global:TfAcSbTeamsIPPhonePolicy = {
  $null = Assert-MicrosoftTeamsConnection
  (Get-CsTeamsIPPhonePolicy | Sort-Object -Property Identity).Identity -replace 'Tag:', ''
}

# CsCallingLineIdentity
$global:TfAcSbTeamsCallingLineIdentity = {
  $null = Assert-MicrosoftTeamsConnection
  (Get-CsCallingLineIdentity | Sort-Object -Property Identity).Identity -replace 'Tag:', ''
}

# CsTeamsCallingPolicy
$global:TfAcSbTeamsCallingPolicy = {
  $null = Assert-MicrosoftTeamsConnection
  (Get-CsTeamsCallingPolicy | Sort-Object -Property Identity).Identity -replace 'Tag:', ''
}

# CsTeamsCallParkPolicy
$global:TfAcSbTeamsCallParkPolicy = {
  $null = Assert-MicrosoftTeamsConnection
  (Get-CsTeamsCallParkPolicy | Sort-Object -Property Identity).Identity -replace 'Tag:', ''
}

# CsTeamsEmergencyCallingPolicy
$global:TfAcSbEmergencyCallingPolicy = {
  $null = Assert-MicrosoftTeamsConnection
  (Get-CsTeamsEmergencyCallingPolicy | Sort-Object -Property Identity).Identity -replace 'Tag:', ''
}

# CsTeamsEmergencyCallRoutingPolicy
$global:TfAcSbEmergencyCallRoutingPolicy = {
  $null = Assert-MicrosoftTeamsConnection
  (Get-CsTeamsEmergencyCallRoutingPolicy | Sort-Object -Property Identity).Identity -replace 'Tag:', ''
}

# ISO3166Country
$global:TfAcSbTwoLetterCountryCode = {
  if (-not $global:TeamsFunctionsCountryTable) { $global:TeamsFunctionsCountryTable = (Get-ISO3166Country | Select-Object TwoLetterCode).TwoLetterCode }
  $global:TeamsFunctionsCountryTable
}

# PublicHolidayCountry - Subset of ISO3166Country (supported by Nager.API)
$global:TfAcSbNagerAPICountryCode = {
  $null = Assert-MicrosoftTeamsConnection
  (Get-PublicHolidayCountry | Sort-Object CountryCode).CountryCode
}

# TeamsFunctionsMSAzureAdLicenses - License
$global:TfAcSbAzureAdLicense = {
  if (-not $global:TeamsFunctionsMSAzureAdLicenses) { $global:TeamsFunctionsMSAzureAdLicenses = Get-AzureAdLicense -WarningAction SilentlyContinue }
  ($global:TeamsFunctionsMSAzureAdLicenses).ParameterName.Split('', [System.StringSplitOptions]::RemoveEmptyEntries) | Sort-Object
}

# TeamsFunctionsMSAzureAdLicenses - CallingPlanLicense
$global:TfAcSbCallingPlanLicense = {
  if (-not $global:TeamsFunctionsMSAzureAdLicenses) { $global:TeamsFunctionsMSAzureAdLicenses = Get-AzureAdLicense -WarningAction SilentlyContinue }
  $global:TeamsFunctionsMSAzureAdCallingPlan = ($global:TeamsFunctionsMSAzureAdLicenses | Where-Object LicenseType -EQ 'CallingPlan')
  $global:TeamsFunctionsMSAzureAdCallingPlan.ParameterName.Split('', [System.StringSplitOptions]::RemoveEmptyEntries) | Sort-Object
}

# TeamsFunctionsMSAzureAdLicenseServicePlans - License Service Plan
$global:TfAcSbAzureAdLicenseServicePlan = {
  if (-not $global:TeamsFunctionsMSAzureAdLicenseServicePlans) { $global:TeamsFunctionsMSAzureAdLicenseServicePlans = Get-AzureAdLicenseServicePlan -WarningAction SilentlyContinue }
  ($global:TeamsFunctionsMSAzureAdLicenseServicePlans).ServicePlanName.Split('', [System.StringSplitOptions]::RemoveEmptyEntries) | Sort-Object -Unique
}

# CsTenantNetworkSite - Network Site
$global:TfAcSbTeamsNetworkSiteId = {
  $null = Assert-MicrosoftTeamsConnection
  (Get-CsTenantNetworkSite | Select-Object Identity).Identity
}

# CsTenantNetworkSubnet - Network Subnet
$global:TfAcSbTeamsNetworkSubnetId = {
  $null = Assert-MicrosoftTeamsConnection
  (Get-CsTenantNetworkSubnet | Select-Object Identity).Identity
}

# CsAutoAttendantSupportedLanguage - AutoAttendant Language Id
$global:TfAcSbSupportedLanguage = {
  $null = Assert-MicrosoftTeamsConnection
  if (-not $global:CsAutoAttendantSupportedLanguage) { $global:CsAutoAttendantSupportedLanguage = Get-CsAutoAttendantSupportedLanguage -ErrorAction SilentlyContinue }
  $($global:CsAutoAttendantSupportedLanguage)
}

<# Phased out in favor of full object above
$global:TfAcSbSupportedLanguageIds = {
  $null = Assert-MicrosoftTeamsConnection
  if (-not $global:CsAutoAttendantSupportedLanguage) { $global:CsAutoAttendantSupportedLanguage = Get-CsAutoAttendantSupportedLanguage -ErrorAction SilentlyContinue }
  $($global:CsAutoAttendantSupportedLanguage.Id)
}
#>


# CsAutoAttendantSupportedTimeZone & TeamsFunctionsSupportedTimeZone - AutoAttendant TimeZone Id
$global:TfAcSbSupportedTimeZone = {
  $null = Assert-MicrosoftTeamsConnection
  if (-not $global:CsAutoAttendantSupportedTimeZone) { $global:CsAutoAttendantSupportedTimeZone = Get-CsAutoAttendantSupportedTimeZone -ErrorAction SilentlyContinue }
  #[Collections.Generic.List[object]]$global:TeamsFunctionsSupportedTimeZone = $CsAutoAttendantSupportedTimeZone.DisplayName.Substring(1, $_.DisplayName.IndexOf(')')) | Get-Unique
  [Collections.Generic.List[object]]$global:TeamsFunctionsSupportedTimeZone = $CsAutoAttendantSupportedTimeZone.DisplayName | ForEach-Object { $_.Substring($_.IndexOf('(') + 1, $_.IndexOf(')') - 1) } | Get-Unique
  $global:TeamsFunctionsSupportedTimeZone
}


<# TO BE TESTED --- HIGHLY EXPERIMENTAL
# Emergency Address - resultSize may be a bit too large!
#TODO Requires testing, Cmdlet accepts both LocationId and Location, must continue to serve this way. (also: test string is correctly applied (wrapped))
$global:TfAcSbOnlineLisLocation = { (Get-CsOnlineLisLocation | Sort-Object -Property Location).Location }
Register-ArgumentCompleter -CommandName Get-TeamsTelephoneNumber -ParameterName Location -ScriptBlock $global:TfAcSbOnlineLisLocation
Register-ArgumentCompleter -CommandName Set-TeamsTelephoneNumber -ParameterName Location -ScriptBlock $global:TfAcSbOnlineLisLocation
Register-ArgumentCompleter -CommandName Grant-TeamsEmergencyAddress -ParameterName PolicyName -ScriptBlock $global:TfAcSbOnlineLisLocation
Register-ArgumentCompleter -CommandName Set-TeamsUserEmergencyConfiguration -ParameterName TeamsEmergencyAddress -ScriptBlock $global:TfAcSbOnlineLisLocation
 
# Teams - resultSize may be a bit too large!
$global:TfAcSbTeamName = { (Get-Team -WarningAction SilentlyContinue | Select-Object DisplayName).DisplayName }
Register-ArgumentCompleter -CommandName Get-TeamsTeamChannel -ParameterName Team -ScriptBlock $global:TfAcSbTeamName
 
# ResourceAccount - ResultSize may be too big!
$global:TfAcSbResourceAccount = { (Get-CsOnlineApplicationInstance | Sort-Object UserPrUserPrincipalName | Select-Object UserPrincipalName).UserPrincipalName }
Register-ArgumentCompleter -CommandName Get-TeamsResourceAccount -ParameterName UserPrincipalName -ScriptBlock $global:TfAcSbResourceAccount
Register-ArgumentCompleter -CommandName Set-TeamsResourceAccount -ParameterName UserPrincipalName -ScriptBlock $global:TfAcSbResourceAccount
Register-ArgumentCompleter -CommandName Remove-TeamsResourceAccount -ParameterName UserPrincipalName -ScriptBlock $global:TfAcSbResourceAccount
Register-ArgumentCompleter -CommandName New-TeamsResourceAccountAssociation -ParameterName UserPrincipalName -ScriptBlock $global:TfAcSbResourceAccount
 
# Call Queues - ResultSize may be too big / limited to 100
$global:TfAcSbCallQueue = { (Get-CsCallQueue -WarningAction SilentlyContinue | Select-Object Name).Name }
Register-ArgumentCompleter -CommandName Get-TeamsCallQueue -ParameterName Name -ScriptBlock $global:TfAcSbCallQueue
Register-ArgumentCompleter -CommandName Set-TeamsCallQueue -ParameterName Name -ScriptBlock $global:TfAcSbCallQueue
Register-ArgumentCompleter -CommandName Remove-TeamsCallQueue -ParameterName Name -ScriptBlock $global:TfAcSbCallQueue
Register-ArgumentCompleter -CommandName New-TeamsResourceAccountAssociation -ParameterName CallQueue -ScriptBlock $global:TfAcSbCallQueue
 
# Auto Attendant - ResultSize may be too big / limited to 100
$global:TfAcSbAutoAttendant = { (Get-CsAutoAttendant -WarningAction SilentlyContinue | Select-Object Name).Name }
Register-ArgumentCompleter -CommandName Get-TeamsAutoAttendant -ParameterName Name -ScriptBlock $global:TfAcSbAutoAttendant
Register-ArgumentCompleter -CommandName Remove-TeamsAutoAttendant -ParameterName Name -ScriptBlock $global:TfAcSbAutoAttendant
Register-ArgumentCompleter -CommandName New-TeamsResourceAccountAssociation -ParameterName AutoAttendant -ScriptBlock $global:TfAcSbAutoAttendant
#>