DSCResources/MSFT_TeamsChannel/MSFT_TeamsChannel.psm1
function Get-TargetResource { [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param ( [Parameter(Mandatory = $true)] [System.String] [ValidateLength(1, 50)] $DisplayName, [Parameter(Mandatory = $true)] [System.String] $TeamName, [Parameter()] [System.String] [ValidateLength(0, 50)] $NewDisplayName, [Parameter()] [System.String] [ValidateLength(1, 1024)] $Description, [Parameter()] [ValidateSet("Present", "Absent")] [System.String] $Ensure = "Present", [Parameter()] [System.Management.Automation.PSCredential] $GlobalAdminAccount, [Parameter()] [System.String] $ApplicationId, [Parameter()] [System.String] $TenantId, [Parameter()] [System.String] $CertificateThumbprint ) Write-Verbose -Message "Getting configuration of Teams channel $DisplayName" #region Telemetry $ResourceName = $MyInvocation.MyCommand.ModuleName.Replace("MSFT_", "") $data = [System.Collections.Generic.Dictionary[[String], [String]]]::new() $data.Add("Resource", $ResourceName) $data.Add("Method", $MyInvocation.MyCommand) $data.Add("Principal", $GlobalAdminAccount.UserName) $data.Add("TenantId", $TenantId) Add-M365DSCTelemetryEvent -Data $data #endregion $ConnectionMode = New-M365DSCConnection -Platform 'MicrosoftTeams' -InboundParameters $PSBoundParameters $nullReturn = $PSBoundParameters $nullReturn.Ensure = "Absent" Write-Verbose -Message "Checking for existance of team channels" try { $team = Get-TeamByName $TeamName Write-Verbose -Message "Retrieve team GroupId: $($team.GroupId)" $channel = Get-TeamChannel -GroupId $team.GroupId ` -ErrorAction SilentlyContinue ` | Where-Object -FilterScript { ($_.DisplayName -eq $DisplayName) } #Current channel doesnt exist and trying to rename throw an error if (($null -eq $channel) -and $PSBoundParameters.ContainsKey("NewDisplayName")) { Write-Verbose -Message "Cannot rename channel $DisplayName , doesnt exist in current Team" throw "Channel named $DisplayName doesn't exist in current Team" } if ($null -eq $channel) { Write-Verbose -Message "Failed to get team channels with ID $($team.GroupId) and display name of $DisplayName" return $nullReturn } return @{ DisplayName = $channel.DisplayName TeamName = $team.DisplayName Description = $channel.Description NewDisplayName = $NewDisplayName Ensure = "Present" ApplicationId = $ApplicationId TenantId = $TenantId CertificateThumbprint = $CertificateThumbprint GlobalAdminAccount = $GlobalAdminAccount } } catch { try { Write-Verbose -Message $_ $tenantIdValue = "" if (-not [System.String]::IsNullOrEmpty($TenantId)) { $tenantIdValue = $TenantId } elseif ($null -ne $GlobalAdminAccount) { $tenantIdValue = $GlobalAdminAccount.UserName.Split('@')[1] } Add-M365DSCEvent -Message $_ -EntryType 'Error' ` -EventID 1 -Source $($MyInvocation.MyCommand.Source) ` -TenantId $tenantIdValue } catch { Write-Verbose -Message $_ } return $nullReturn } } function Set-TargetResource { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [System.String] [ValidateLength(1, 50)] $DisplayName, [Parameter(Mandatory = $true)] [System.String] $TeamName, [Parameter()] [System.String] [ValidateLength(0, 50)] $NewDisplayName, [Parameter()] [System.String] [ValidateLength(1, 1024)] $Description, [Parameter()] [ValidateSet("Present", "Absent")] [System.String] $Ensure = "Present", [Parameter()] [System.Management.Automation.PSCredential] $GlobalAdminAccount, [Parameter()] [System.String] $ApplicationId, [Parameter()] [System.String] $TenantId, [Parameter()] [System.String] $CertificateThumbprint ) Write-Verbose -Message "Setting configuration of Teams channel $DisplayName" #region Telemetry $ResourceName = $MyInvocation.MyCommand.ModuleName.Replace("MSFT_", "") $data = [System.Collections.Generic.Dictionary[[String], [String]]]::new() $data.Add("Resource", $ResourceName) $data.Add("Method", $MyInvocation.MyCommand) $data.Add("Principal", $GlobalAdminAccount.UserName) $data.Add("TenantId", $TenantId) Add-M365DSCTelemetryEvent -Data $data #endregion $channel = Get-TargetResource @PSBoundParameters $CurrentParameters = $PSBoundParameters $team = Get-TeamByName $TeamName if ($team.Length -gt 1) { throw "Multiple Teams with name {$($TeamName)} were found" } Write-Verbose -Message "Retrieve team GroupId: $($team.GroupId)" $CurrentParameters.Remove("TeamName") | Out-Null $CurrentParameters.Add("GroupId", $team.GroupId) $CurrentParameters.Remove("GlobalAdminAccount") | Out-Null $CurrentParameters.Remove("ApplicationId") | Out-Null $CurrentParameters.Remove("TenantId") | Out-Null $CurrentParameters.Remove("CertificateThumbprint") | Out-Null $CurrentParameters.Remove("Ensure") | Out-Null if ($Ensure -eq "Present") { # Remap attribute from DisplayName to current display name for Set-TeamChannel cmdlet if ($channel.Ensure -eq "Present") { if ($CurrentParameters.ContainsKey("NewDisplayName")) { Write-Verbose -Message "Updating team channel to new channel name $NewDisplayName" $CurrentParameters.Remove("DisplayName") | Out-Null Set-TeamChannel @CurrentParameters -CurrentDisplayName $DisplayName } } else { if ($CurrentParameters.ContainsKey("NewDisplayName")) { $CurrentParameters.Remove("NewDisplayName") } Write-Verbose -Message "Creating team channel $DislayName" New-TeamChannel @CurrentParameters } } else { if ($channel.DisplayName) { Write-Verbose -Message "Removing team channel $DislayName" Remove-TeamChannel -GroupId $team.GroupId -DisplayName $DisplayName } } } function Test-TargetResource { [CmdletBinding()] [OutputType([System.Boolean])] param ( [Parameter(Mandatory = $true)] [System.String] [ValidateLength(1, 50)] $DisplayName, [Parameter(Mandatory = $true)] [System.String] $TeamName, [Parameter()] [System.String] [ValidateLength(0, 50)] $NewDisplayName, [Parameter()] [System.String] [ValidateLength(1, 1024)] $Description, [Parameter()] [ValidateSet("Present", "Absent")] [System.String] $Ensure = "Present", [Parameter()] [System.Management.Automation.PSCredential] $GlobalAdminAccount, [Parameter()] [System.String] $ApplicationId, [Parameter()] [System.String] $TenantId, [Parameter()] [System.String] $CertificateThumbprint ) #region Telemetry $ResourceName = $MyInvocation.MyCommand.ModuleName.Replace("MSFT_", "") $data = [System.Collections.Generic.Dictionary[[String], [String]]]::new() $data.Add("Resource", $ResourceName) $data.Add("Method", $MyInvocation.MyCommand) $data.Add("Principal", $GlobalAdminAccount.UserName) $data.Add("TenantId", $TenantId) Add-M365DSCTelemetryEvent -Data $data #endregion Write-Verbose -Message "Testing configuration of Teams channel $DisplayName" $CurrentValues = Get-TargetResource @PSBoundParameters Write-Verbose -Message "Current Values: $(Convert-M365DscHashtableToString -Hashtable $CurrentValues)" Write-Verbose -Message "Target Values: $(Convert-M365DscHashtableToString -Hashtable $PSBoundParameters)" $TestResult = Test-M365DSCParameterState -CurrentValues $CurrentValues ` -Source $($MyInvocation.MyCommand.Source) ` -DesiredValues $PSBoundParameters ` -ValuesToCheck @("Ensure") Write-Verbose -Message "Test-TargetResource returned $TestResult" return $TestResult } function Export-TargetResource { [CmdletBinding()] [OutputType([System.String])] param ( [Parameter()] [System.String] $ApplicationId, [Parameter()] [System.String] $TenantId, [Parameter()] [System.String] $CertificateThumbprint, [Parameter()] [System.Management.Automation.PSCredential] $GlobalAdminAccount ) #region Telemetry $ResourceName = $MyInvocation.MyCommand.ModuleName.Replace("MSFT_", "") $data = [System.Collections.Generic.Dictionary[[String], [String]]]::new() $data.Add("Resource", $ResourceName) $data.Add("Method", $MyInvocation.MyCommand) $data.Add("Principal", $GlobalAdminAccount.UserName) $data.Add("TenantId", $TenantId) Add-M365DSCTelemetryEvent -Data $data #endregion $ConnectionMode = New-M365DSCConnection -Platform 'MicrosoftTeams' -InboundParameters $PSBoundParameters try { $teams = Get-Team -ErrorAction Stop $j = 1 $content = '' Write-Host "`r`n" -NoNewline foreach ($team in $Teams) { $channels = Get-TeamChannel -GroupId $team.GroupId $i = 1 Write-Host " |---[$j/$($Teams.Length)] Team {$($team.DisplayName)}" foreach ($channel in $channels) { Write-Host " |---[$i/$($channels.Length)] $($channel.DisplayName)" -NoNewline if ($ConnectionMode -eq 'Credential') { $params = @{ TeamName = $team.DisplayName DisplayName = $channel.DisplayName GlobalAdminAccount = $GlobalAdminAccount } } else { $params = @{ TeamName = $team.DisplayName DisplayName = $channel.DisplayName ApplicationId = $ApplicationId TenantId = $TenantId CertificateThumbprint = $CertificateThumbprint } } $result = Get-TargetResource @params if ($ConnectionMode -eq 'Credential') { $result.GlobalAdminAccount = Resolve-Credentials -UserName "globaladmin" $result.Remove("ApplicationId") $result.Remove("TenantId") $result.Remove("CertificateThumbprint") } else { $result.Remove("GlobalAdminAccount") } $content += " TeamsChannel " + (New-Guid).ToString() + "`r`n" $content += " {`r`n" $currentDSCBlock = Get-DSCBlock -Params $result -ModulePath $PSScriptRoot if ($ConnectionMode -eq 'Credential') { $content += Convert-DSCStringParamToVariable -DSCBlock $currentDSCBlock -ParameterName "GlobalAdminAccount" } $content += " }`r`n" $i++ Write-Host $Global:M365DSCEmojiGreenCheckMark } $j++ } return $content } catch { try { Write-Verbose -Message $_ $tenantIdValue = "" if (-not [System.String]::IsNullOrEmpty($TenantId)) { $tenantIdValue = $TenantId } elseif ($null -ne $GlobalAdminAccount) { $tenantIdValue = $GlobalAdminAccount.UserName.Split('@')[1] } Add-M365DSCEvent -Message $_ -EntryType 'Error' ` -EventID 1 -Source $($MyInvocation.MyCommand.Source) ` -TenantId $tenantIdValue } catch { Write-Verbose -Message $_ } return "" } } Export-ModuleMember -Function *-TargetResource |