code365scripts.teams.psm1

<#
.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
    }
    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 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();
    }




    $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
    }
}