code365scripts.teams.psm1

function GenerateThumbnail {
    param (
        [string]$filePath
    )
    $fileName = Split-Path $filePath -Leaf
    $thumbName = "$($fileName.Split('.')[0])_thumb.jpg"
    
    $full = [System.Drawing.Image]::FromFile($filePath);
    $thumb = $full.GetThumbnailImage(280, 158, $null, [System.IntPtr]::Zero);

    $encoder = GetEncoder

    $myEncoder = [Drawing.Imaging.Encoder]::Quality
    $myEncoderParameters = New-Object Drawing.Imaging.EncoderParameters(1)
    $myEncoderParameter = New-Object Drawing.Imaging.EncoderParameter($myEncoder, 24L)
    $myEncoderParameters.Param[0] = $myEncoderParameter

    $thumb.Save($filePath.replace($fileName, $thumbName), $encoder, $myEncoderParameters);
    $full.Dispose();
    $thumb.Dispose();
}

function GetEncoder() {
    $format = [Drawing.Imaging.ImageFormat]::Jpeg

    $codecs = [System.Drawing.Imaging.ImageCodecInfo]::GetImageDecoders()
    foreach ($codec in $codecs) {
        if ($codec.FormatID -eq $format.Guid) {
            return $codec;
        }
    }
    return $null;
}

function EnsureRequiredModules {
    [CmdletBinding()]
    param (
        [string[]]$requiredModules
    )

    $foundModules = Get-Module $requiredModules -ListAvailable
    if ($null -eq $foundModules) {
        $foundModules = @()
    }
    else {
        $foundModules = $foundModules.Name
    }

    if ($foundModules.Count -lt $requiredModules.Count) {

        $needToInstallModules = $requiredModules | Where-Object { $_ -notin $foundModules }

        $p = Read-Host -Prompt "该命令依赖 $([string]::Join(',',$needToInstallModules)) 模块,当前没有检测到,是否要立即安装?【Y/n】"
        if ($p.Length -eq 0 -xor $p.ToLower() -eq "y") {
            Install-Module $needToInstallModules -Scope CurrentUser
        }
        else {
            return
        }
    }
}

<#
.SYNOPSIS
    批量下载Teams视频会议的背景图
.DESCRIPTION
    下载 https://adoption.microsoft.com/microsoft-teams/custom-backgrounds-gallery/ 这里列出的美图,保存到Teams指定的目录,并且进行压缩,生成缩略图。你只需要运行这个脚本,然后再去打开Teams的视频会议,选择背景图时就可以看到这些新的图片。
.EXAMPLE
    PS C:\> Get-TeamsVideoMeetingBackgrounds
    下载所有推荐的美图到Teams指定目录
.EXAMPLE
    PS C:\> gtvb
    下载所有推荐的美图到Teams指定目录
.LINK
    https://github.com/code365opensource/code365scripts/tree/master/code365scripts.teams
#>

function Get-TeamsVideoMeetingBackgrounds {
    [CmdletBinding()]
    [Alias("gtvb")]
    param ()
    
    if ($PSVersionTable.PSVersion.Major -lt 6) {
        # 老版本Powershell需要手工导入这个图像库
        Add-Type -AssemblyName System.Drawing
    }

    $path = "$home\AppData\Roaming\Microsoft\Teams\Backgrounds\uploads"

    if (-not (Test-Path $path)) {
        New-Item $path -ItemType Directory | Out-Null
    }

    # UseBasicparsing 可以提高性能,而且避免弹出一个对话框的问题,因为它不会尝试用IE去解析文档。(使用老版本的PowerShell的话)
    $images = (Invoke-WebRequest -Uri "https://adoption.microsoft.com/microsoft-teams/custom-backgrounds-gallery/" -UseBasicParsing).Links.href | Where-Object { ($_ -like "https://adoption.azureedge.net/wp-content/custom-backgrounds-gallery*.jpg") -and (-not (Test-Path "$path\$(Split-Path $_ -Leaf)")) } | Select-Object -Unique 

    $wc = New-Object System.Net.WebClient
    $index = 1
    $count = $images.Count

    $images | ForEach-Object {
    
        $fileName = Split-Path $_ -Leaf
        $filePath = "$path\$fileName"
        # 使用web client可以明显提高性能
        $wc.DownloadFile($_, $filePath) 
        Write-Progress -Activity "Download Background image" -Status "Save file: $_" -PercentComplete ($index / $count * 100)

        # Invoke-WebRequest -Uri $_ -OutFile $filePath
        GenerateThumbnail -filePath $filePath

        Write-Progress -Activity "Download Background image" -Status "Generate thumbnail: $fileName" -PercentComplete ($index / $count * 100)

        $index = $index + 1
    }
}


<#
.SYNOPSIS
    删除Teams客户端缓存
#>

function Remove-TeamsClientCache {
    [CmdletBinding(DefaultParameterSetName = 'default',
        SupportsShouldProcess = $true,
        PositionalBinding = $false,
        HelpUri = 'https://scripts.code365.xyz/code365scripts.teams/',
        ConfirmImpact = 'Medium')]
    [Alias()]
    Param ()
    
    begin {
        $res = Read-Host -Prompt "这个命令将退出当前的Teams,并且清理客户端缓存,是否继续【y/N】?"

        if ($res.ToLower() -ne "y") {
            return
        }

        $errorpreference = $ErrorActionPreference
        $ErrorActionPreference = "SilentlyContinue"
    }
    
    process {

        Get-Process | Where-Object { $_.ProcessName -eq "Teams" } | Stop-Process
        Remove-Item –path $env:APPDATA"\Microsoft\teams\cache\*" | Out-Null
        Remove-Item –path $env:APPDATA"\Microsoft\teams\blob_storage\*" | Out-Null
        Remove-Item –path $env:APPDATA"\Microsoft\teams\databases\*" | Out-Null
        Remove-Item –path $env:APPDATA"\Microsoft\teams\GPUcache\*" | Out-Null
        Remove-Item –path $env:APPDATA"\Microsoft\teams\IndexedDB\*" -recurse | Out-Null
        Remove-Item –path $env:APPDATA"\Microsoft\teams\Local Storage\*" -recurse | Out-Null
        Remove-Item –path $env:APPDATA"\Microsoft\teams\tmp\*" | Out-Null
        Remove-Item –path $env:APPDATA"\Microsoft\teams\Code Cache\*" -Recurse | Out-Null
        Remove-Item –path $env:APPDATA"\Microsoft\teams\backgrounds\*" -Recurse | Out-Null
        Write-Host "客户端缓存已经清理,请重新启动Teams客户端"
    }
    
    end {
        
        $ErrorActionPreference = $errorpreference
    }
}

<#
.SYNOPSIS
    批量添加用户到某个团队
.DESCRIPTION
    
.EXAMPLE
    PS C:\> Import-TeamUser -teamName "开发测试" -users mike@xyz.com,tom@xyz.com
    用逗号分开不同的用户名
.EXAMPLE
    PS C:\> Import-TeamUser -teamName "开发测试" -users mike,tom
    用逗号分开不同的用户名,如果不带邮箱后缀,则自动以当前用户的邮箱后缀补充
#>

function Import-TeamUser {
    [CmdletBinding(DefaultParameterSetName = "default")]
    param (
        [Parameter(ParameterSetName = "default", Mandatory = $true)]
        [string]$teamName,
        [Parameter(ParameterSetName = "default", Mandatory = $true)]
        [string[]]$users
    )
    
    begin {
        EnsureRequiredModules -requiredModules @("MicrosoftTeams")
    }
    
    process {
        $connect = Connect-MicrosoftTeams
        if ($null -ne $connect) {
            $domain = $connect.Account.Id.split('@')[1]
        }

        $team = Get-Team -DisplayName $teamName
        if ($null -eq $team) {
            Write-Host "无法查找到该团队,请检查名称"
            return
        }

        # 处理用户列表


        $index = 1
        $count = $users.Count
        
        foreach ($item in $users) {


            if (!($item.EndsWith("@$domain"))) {
                $item += "@$domain"
            }

            if ($item.Contains("@") -band !($item.EndsWith("@$domain"))) {
                Write-Progress -Activity "批量导入用户" -Status "$item 不是当前公司用户,取消操作" -PercentComplete ($index++ / $count * 100)
                Write-Host "$item 不是当前公司用户,取消操作"
            }
            else {
                Add-TeamUser -GroupId $team.GroupId -User $item
                Write-Progress -Activity "批量导入用户" -Status $item -PercentComplete ($index++ / $count * 100)
                Write-Host "$item 导入成功"

            }

        }
    }
    
    end {
        
    }
}