PowerShell获取必应今日美图并设置为桌面墙纸

#########################################################################
# PowerShell获取必应今日美图并设置为桌面墙纸
#########################################################################
function Save-BingTodayImage() {
    #必应图片故事API
    $bingImageApi ='https://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1&mkt=zh-cn'
    $bingUri = 'https://www.bing.com/'
    # 获取图片链接
    Write-Host 正在下载,请稍后。
    [xml]$bingImageXml = (Invoke-WebRequest -Uri $bingImageApi).Content
    # Write-Host " 今日图片故事: $( $bingImageXml.images.image.copyright ) "
    $imgLink = '{0}{1}' -f $bingUri , $bingImageXml.images.image.url
    # 下载和保存图片
    $imageDir = "D:\OneDrive\BingWallpaper"
    if ( -not (Test-Path $imageDir) ) {
        mkdir $imageDir | Out-Null
    }
    $imageFile = Join-Path $imageDir ( '{0}.jpg' -f $bingImageXml.images.image.enddate)
    # 下载图片
    Invoke-WebRequest -Uri $imgLink -OutFile $imageFile
    return $imageFile
}
# 获取今日必应背景图片
$image=Save-BingTodayImage
Write-Host 下载已完成,正在设置壁纸。
#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();
      }
   }
}
"@
# 设置为桌面墙纸
[Wallpaper.Setter]::SetWallpaper( $image, 2 )
Write-Host 已完成,关闭并自动退出。

2018-05-22 17:36:41 通过 网页

共有0条评论!

发表评论