Get-BingImage.ps1
function Get-BingImage { param ( [ValidateSet("en-US", "zh-CN", "ja-JP", "en-AU", "en-UK", "de-DE", "en-NZ")] [Parameter(ValueFromPipeline=$true)] [string]$Market = "de-DE", [alias("DP")] [ValidateScript({Test-Path -Path $_})] [string]$DestinationDirectory = [Environment]::GetFolderPath([System.Environment+SpecialFolder]::DesktopDirectory), [ValidateRange(1,6)] [UInt16]$Last = 6, [alias("OI")] [switch] [bool]$OpenImage = $false ) begin { $BingBaseUri = "http://www.bing.com" } process { $BingImagesOverviewUri = "$BingBaseUri/HPImageArchive.aspx?format=xml&idx=0&n=$Last&mkt=$Market" $BingImagesXDoc = [xml](Invoke-WebRequest -Uri $BingImagesOverviewUri | Select-Object -ExpandProperty Content) $BingImagesXDoc.images.image | ForEach-Object -Process { $DownloadUri = "{0}{1}_1920x1080.jpg" -f $BingBaseUri, $_.urlBase # 1366x768, 1920x1080 if($_.copyright.Replace('/', '; ').Trim() -match "^(.+) (\(.+\))$") { $DestinationFile = $Matches[1] $DestinationFile = "BingImage {0}, {1}.jpg" -f $_.StartDate, $DestinationFile $DestinationPath = (Join-Path -Path $DestinationDirectory -ChildPath $DestinationFile) Invoke-WebRequest -Uri $DownloadUri -OutFile $DestinationPath New-Object PSObject | Add-Member -Name "FileName" -Value $DestinationFile -MemberType NoteProperty -PassThru | Add-Member -Name "Directory" -Value $DestinationDirectory -MemberType NoteProperty -PassThru if($OpenImage) { Start-Process -FilePath $DestinationPath } } else { Write-Error "Filename incorrect" } } } end{} } |