powershell脚本让bing每日图片作为壁纸 9


新建一个autorun.vbs文件, 修改其中Bing-Wallpapers.ps1的路径, 然后把autorun.vbs放到C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup

set ws=wscript.createobject("wscript.shell")
ws.run "PowerShell.exe -ExecutionPolicy Bypass -File C:\Users\jin7\Documents\WindowsPowerShell\Bing-Wallpapers.ps1",0

新建Bing-Wallpapers.ps1文件

# Bing Wallpapers
# Fetch the Bing wallpaper image of the day
# <https://github.com/timothymctim/Bing-wallpapers>
#
# Copyright (c) 2015 Tim van de Kamp
# License: MIT license
# Modified by 周蒙牛
Param(
    # Get the Bing image of this country
    [ValidateSet('en-US', 'de-DE', 'en-AU', 'en-CA', 'en-NZ', 'en-UK', 'ja-JP', 'zh-CN')][string]$locale = 'zh-CN',

    # Download the latest $files wallpapers
    [int]$files = 0,

    # Resolution of the image to download
    [ValidateSet('auto', '1024x768', '1280x720', '1366x768', '1920x1080')][string]$resolution = 'auto',

    # Destination folder to download the wallpapers to
    [string]$downloadFolder = "$([Environment]::GetFolderPath("MyPictures"))\Bing-Wallpapers"
)
# Max item count: the number of images we'll query for
[int]$maxItemCount = [System.Math]::max(1, [System.Math]::max($files, 8))
# URI to fetch the image locations from
[string]$hostname = "https://www.bing.com"
[string]$uri = "$hostname/HPImageArchive.aspx?format=xml&idx=0&n=$maxItemCount&mkt=$locale"

# Get the appropiate screen resolution
if ($resolution -eq 'auto') {
    Add-Type -AssemblyName System.Windows.Forms
    $primaryScreen = [System.Windows.Forms.Screen]::AllScreens | Where-Object {$_.Primary -eq 'True'}
    if ($primaryScreen.Bounds.Width -le 1024) {
        $resolution = '1024x768'
    }
    elseif ($primaryScreen.Bounds.Width -le 1280) {
        $resolution = '1280x720'
    }
    elseif ($primaryScreen.Bounds.Width -le 1366) {
        $resolution = '1366x768'
    }
    else {
        $resolution = '1920x1080'
    }
}

# Check if download folder exists and otherwise create it
if (!(Test-Path $downloadFolder)) {
    New-Item -ItemType Directory $downloadFolder
}

$request = Invoke-WebRequest -Uri $uri
[xml]$content = $request.Content

$items = New-Object System.Collections.ArrayList
foreach ($xmlImage in $content.images.image) {
    [datetime]$imageDate = [datetime]::ParseExact($xmlImage.startdate, 'yyyyMMdd', $null)
    [string]$imageUrl = "$hostname$($xmlImage.urlBase)_$resolution.jpg"

    # Add item to our array list
    $item = New-Object System.Object
    $item | Add-Member -Type NoteProperty -Name date -Value $imageDate
    $item | Add-Member -Type NoteProperty -Name url -Value $imageUrl
    $null = $items.Add($item)
}

# Keep only the most recent $files items to download
if (!($files -eq 0) -and ($items.Count -gt $files)) {
    # We have too many matches, keep only the most recent
    $items = $items|Sort date
    while ($items.Count -gt $files) {
        # Pop the oldest item of the array
        $null, $items = $items
    }
}

Write-Host "Downloading images..."
$client = New-Object System.Net.WebClient
$imgsArr = @()
foreach ($item in $items) {
    $baseName = $item.date.ToString("yyyy-MM-dd")
    $destination = "$downloadFolder\$baseName.jpg"
    $url = $item.url

    $imgsArr = $imgsArr + $destination
    # Download the enclosure if we haven't done so already
    if (!(Test-Path $destination)) {
        Write-Debug "Downloading image to $destination"
        $client.DownloadFile($url, "$destination")
    }
}

if ($files -gt 0) {
    # We do not want to keep every file; remove the old ones
    Write-Host "Cleaning the directory..."
    $i = 1
    Get-ChildItem -Filter "????-??-??.jpg" $downloadFolder | Sort -Descending FullName | ForEach-Object {
        if ($i -gt $files) {
            # We have more files than we want, delete the extra files
            $fileName = $_.FullName
            Write-Debug "Removing file $fileName"
            Remove-Item "$fileName"
        }
        $i++
    }
}

#The code is from https://stackoverflow.com/questions/9440135/powershell-script-from-shortcut-to-change-desktop
Add-Type @"
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace Wallpaper
{
   public enum Style : int
   {
       Tile, Center, Stretch, NoChange
   }
   public class Setter {
      public const int SetDesktopWallpaper = 20;
      public const int UpdateIniFile = 0x01;
      public const int SendWinIniChange = 0x02;
      [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
      private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
      public static void SetWallpaper ( string path, Wallpaper.Style style ) {
         SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
         RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
         switch( style )
         {
            case Style.Stretch :
               key.SetValue(@"WallpaperStyle", "2") ; 
               key.SetValue(@"TileWallpaper", "0") ;
               break;
            case Style.Center :
               key.SetValue(@"WallpaperStyle", "1") ; 
               key.SetValue(@"TileWallpaper", "0") ; 
               break;
            case Style.Tile :
               key.SetValue(@"WallpaperStyle", "1") ; 
               key.SetValue(@"TileWallpaper", "1") ;
               break;
            case Style.NoChange :
               break;
         }
         key.Close();
      }
   }
}
"@

$today = Get-Date -UFormat "%Y-%m-%d"
$yesterday = Get-Date (Get-Date).AddDays(-1) -UFormat "%Y-%m-%d"
$theDayBeforeYesterday = Get-Date (Get-Date).AddDays(-2) -UFormat "%Y-%m-%d"
$lastPicDate = Get-Date $items[0].date -UFormat "%Y-%m-%d"
switch ($lastPicDate) {
    $today {
        $wallpaper = "$downloadFolder\" + $today + '.jpg'
        break
    }
    $yesterday {
        $wallpaper = "$downloadFolder\" + $yesterday + '.jpg'
        break
    }
    $theDayBeforeYesterday {
        $wallpaper = "$downloadFolder\" + $theDayBeforeYesterday + '.jpg'
        break
    }
    Default {
        $imgsArr = $imgsArr | Sort-Object {Get-Random}
        $wallpaper = $imgsArr[0]
        break
    }
}
[Wallpaper.Setter]::SetWallpaper( $wallpaper, 2 )

×用微信扫描并分享
本文链接: https://www.pstips.net/set-bing-story-image-as-desktop-wallpapers.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

发表评论

电子邮件地址不会被公开。 必填项已用*标注

9 条评论 “powershell脚本让bing每日图片作为壁纸