Public/Get-BingPicture.ps1

<#
 
# Get-BingPicture
 
Download der letzten BING-Such-Hintergrundbilder
 
- **Hashtags** UserCmdlet
- **Version** 2019.09.09
 
#>


using namespace System
using namespace System.Runtime.InteropServices
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
Set-StrictMode -Version Latest

# TODO Bzgl. EXIF z.b. https://dennisaa.wordpress.com/2017/04/26/powershell-updating-jpg-metadata/

function Get-BingPicture {
    <#
        .SYNOPSIS
        Download der letzten BING-Such-Hintergrundbilder
 
        .EXAMPLE
        Get-BingPicture -ShowWindow
        Es werden die letzten 8 Bilder im Download-Order gespeichert und angezeigt.
    #>

    [CmdletBinding(SupportsShouldProcess=$true)]
    [OutputType([PSCustomObject])]
    param (
        [string]$DestinationPath = "$env:USERPROFILE\Downloads",

        [ValidateSet("en-US", "zh-CN", "ja-JP", "en-AU", "en-UK", "de-DE", "en-NZ")]
        [string]$Market = "de-DE",

        [ValidateRange(1, 8)]
        [int]$LastPictures = 8,

        [switch]$ShowPicture
    )
    
    begin {
        "Market = $Market | Last Pictures = $LastPictures | Destination Path = $DestinationPath" | Write-Verbose

        $request = Invoke-WebRequest -Uri "http://www.bing.com/HPImageArchive.aspx?mbl=1&mkt=$Market&idx=0&n=$LastPictures" -Verbose:$false
        [xml]$bingPictures = $request.Content
    }
    
    process {}
    
    end {
        $bingPictures.images.image | ForEach-Object -Process {

            $matches = $_.copyright | Select-String -Pattern "^(.+)( \(© )(.*\))$"
            $picTitle          = $matches.Matches[0].Groups[1].Value.Replace("/", "-")
            $picDownloadUrl    = "http://www.bing.com$($_.url)"
            $picPublished      = [DateTime]::ParseExact($_.startdate, "yyyyMMdd", $null)
            $picDestinationUrl = Join-Path -Path $DestinationPath -ChildPath ("{0}.jpg" -f $picTitle)
    
            Invoke-WebRequest -Uri $picDownloadUrl -OutFile $picDestinationUrl -Verbose:$false
            
            $pictureFile = Get-Item -Path $picDestinationUrl
            $pictureFile.CreationTime   = $picPublished
            $pictureFile.LastWriteTime  = $picPublished
    
            [PSCustomObject]@{
                Title     = $picTitle
                Published = $picPublished
                Path      = $picDestinationUrl
            }
    
            if($ShowPicture) {
                Start-Process -FilePath $picDestinationUrl
            }
        }
    }
}