每日自动更新 bing 壁纸

本贴最后更新于 1884 天前,其中的信息可能已经时移世异

该脚本仅在 win10 下测试通过

<#

.Synopsis
   自动设置bing图片为桌面壁纸
.DESCRIPTION
   添加计划任务,可每日自动更新并设置bing图片为壁纸,并且在图片下添加图片标题及版权文字
.EXAMPLE
    必应每日壁纸.ps1 
    必应每日壁纸.ps1 [-Task]
.INPUTS
   到此 cmdlet 的输入(如果有)
.OUTPUTS
   来自此 cmdlet 的输出(如果有)
.NOTES
   一般注释
.COMPONENT
   此 cmdlet 所属的组件
.ROLE
   此 cmdlet 所属的角色
.FUNCTIONALITY
   最准确描述此 cmdlet 的功能
#>
[CmdletBinding(DefaultParameterSetName)] # 设置默认参数组为空
Param(
    [Parameter(ParameterSetName="Task")]
    [switch]$Task,
    [Parameter(DontShow,ParameterSetName="None")]
    [switch]$None
)

if ($task)
{
    $tem=Get-ScheduledTaskInfo -TaskName "每日自动更换bing壁纸" -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
    if ($tem)
    {
        Write-Error -Message "该计划任务已经存在"

    }
    else
    {
        # 任务操作   程序 ,工作目录,参数
        $A = New-ScheduledTaskAction -Execute "PowerShell"  -WorkingDirectory "$($PSScriptRoot)" -Argument "$($Script:MyInvocation.InvocationName)"
        # 触发器    每天零点一秒 在六小时内随机
        $T = New-ScheduledTaskTrigger -Daily -At 0:0:1  -RandomDelay 6:0:0          
        # 创建者  当前用户名
        $P = New-ScheduledTaskPrincipal "$env:userdomain\$env:username" 
        # 设置集   各种相关设置
        $S = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -WakeToRun -Priority 4 -DontStopIfGoingOnBatteries -DontStopOnIdleEnd -RunOnlyIfNetworkAvailable -MultipleInstances IgnoreNew -StartWhenAvailable -Compatibility win8  -ExecutionTimeLimit 0   -DisallowHardTerminate       
        # 新任务   最后是任务描述
        $D = New-ScheduledTask -Action $A -Principal $P -Trigger $T -Settings $S  -Description "每日自动更新必应桌面壁纸"
        # 注册任务     
        Register-ScheduledTask -TaskName "每日自动更换bing壁纸" -InputObject $D
    }
    return
}

[uri]$binguri="https://cn.bing.com/HPImageArchive.aspx?format=js&n=1&pid=hp"
$T="{0:yyyy.MM.dd`tHH:mm′ss″}" -f (Get-Date)
function out ([string]$p)
{
    Out-File -FilePath $PSScriptRoot\BingWallpaper.log -InputObject "$T`t$p" -Append
}

class bing:Hashtable{
    
    [uri]$Uri
    hidden[uri]$DownUri
    [string]$Copyright
    hidden[string]$DownFile

    bing ([string]$uri){
        [uri]$this.uri=$uri
    }
    
    bingImDo (){
        $d=try{
            Invoke-RestMethod @this
        }catch{
            $_.Exception.Response
        }

        if ($d.images)
        {
            $this.DownUri="https://cn.bing.com"+$d.images.url
            $this.Copyright=$d.images.copyright.Replace(" (","`n(")
            $this.DownFile=$PSScriptRoot+"/"+$d.images.enddate+".jpg"
            $client = new-object System.Net.WebClient
            $client.DownloadFile( $this.DownUri ,$this.DownFile)
        }else{
            out "错误码: $($d.StatusCode)($($d.StatusCode.value__))"
        }
    }
}

Add-Type -AssemblyName System.Drawing
function Draw-WatemarkString()
{
    param(
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [ValidatePattern("(.jpg)|(.png)|(.gif)$")]
        [io.fileinfo]$ImageFile,
 
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$Text,
 
        [ValidateSet("NorthWest", "NorthEast", "SouthWest","SouthEast")]
        [string]$Location='SouthEast',
 
        [System.Drawing.Color]$Color= [System.Drawing.Color]::AntiqueWhite,
 
        [ValidateRange(1,[system.single]::MaxValue)]
        [float]$FontSize = 15,
 
        [string]$FontFamily='Microsoft YaHei',
 
        [ValidateRange(0,[system.single]::MaxValue)]
        [int]$margin = 45,
 
        [switch]$Bold,
        [Switch]$Italic,
        [switch]$Underline,
        [switch]$Strikeout
    )
 
    # test image file exists
    if( -not (Test-Path $ImageFile) )
    {
        throw "图片文件${ImageFile}不存在. "
    }
 
    # font style
    [drawing.fontstyle]$fontStyle= [drawing.fontstyle]::Regular
    (
        ($Bold,[drawing.fontstyle]::Bold),
        ($Italic,[drawing.fontstyle]::Italic),
        ($Underline,[drawing.fontstyle]::Underline),
        ($Strikeout,[drawing.fontstyle]::Strikeout)
    ) | foreach {
        if($_[0])
        {
            $fontStyle = $fontStyle -bxor $_[1]
        }
    }
 
    # graphics and brush
    $font = New-Object System.Drawing.Font($FontFamily, $FontSize,$fontStyle)
    $img = [drawing.image]::FromFile($ImageFile)
    $drawBrush =  [drawing.solidbrush]$Color
    $g = [drawing.graphics]::FromImage($img)
    $textSize = $g.MeasureString($Text,$font).ToPointF()
    $pointF = New-Object System.Drawing.PointF
 <#
    # location
    switch ($Location)
    {
        'NorthWest'
        {
            $pointF.X = $margin
            $pointF.Y = $margin
        }
 
        'NorthEast'
        {
            $pointF.X = $img.Width - $margin - $textSize.X
            $pointF.Y = $margin
        }
 
        'SouthWest'
        {
            $pointF.X = $margin
            $pointF.Y = $img.Height - $margin - $textSize.Y
        }
 
        'SouthEast'
        {#>
            $pointF.X = $img.Width - $margin - $textSize.X
            $pointF.Y = $img.Height - $margin - $textSize.Y
      <#  }
    }#>
 
    # draw string and save image to temporary file
    $g.DrawString($Text,$font,$drawBrush,$pointF)
    $tempImage = '{0}.jpg' -f $ImageFile
    $img.Save($tempImage)
 
    # dispose graphics, brush, image
    $g.Dispose()
    $drawBrush.Dispose()
    $img.Dispose()
 
    # copy temporary file to original source
    Copy-Item $tempImage $ImageFile -Force
    Remove-Item $tempImage
 
}

#完全看不懂,天书
# 注册定义的类
Add-Type @" 
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace Wallpaper
{
	public enum Style : int
	{
		Center, Stretch
	}
	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) {
			SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
			RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);

				key.SetValue(@"WallpaperStyle", "10") ;
				key.SetValue(@"TileWallpaper", "0") ;

			key.Close();
		}
	}
}
"@



$bin=[bing]::new($binguri)
echo 获取图片
$bin.bingImDo()
if ($bin.Count -lt 4)
{
    Write-Warning "获取失败"
    return
}
$g=$T,$bin.Copyright -join "`n"
Draw-WatemarkString -ImageFile $bin.DownFile -Text $g

echo 应用新壁纸
[Wallpaper.Setter]::SetWallpaper( $bin.DownFile )
Remove-Item $bin.DownFile #删除壁纸文件
echo 完成!
Start-Sleep -Seconds 3
#exit
  • 必应
    5 引用 • 19 回帖
  • 壁纸
    13 引用 • 78 回帖
  • 工具

    子曰:“工欲善其事,必先利其器。”

    281 引用 • 716 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...

推荐标签 标签

  • Openfire

    Openfire 是开源的、基于可拓展通讯和表示协议 (XMPP)、采用 Java 编程语言开发的实时协作服务器。Openfire 的效率很高,单台服务器可支持上万并发用户。

    6 引用 • 7 回帖 • 94 关注
  • 倾城之链
    23 引用 • 66 回帖 • 120 关注
  • 996
    13 引用 • 200 回帖 • 6 关注
  • Tomcat

    Tomcat 最早是由 Sun Microsystems 开发的一个 Servlet 容器,在 1999 年被捐献给 ASF(Apache Software Foundation),隶属于 Jakarta 项目,现在已经独立为一个顶级项目。Tomcat 主要实现了 JavaEE 中的 Servlet、JSP 规范,同时也提供 HTTP 服务,是市场上非常流行的 Java Web 容器。

    162 引用 • 529 回帖
  • 博客

    记录并分享人生的经历。

    272 引用 • 2386 回帖
  • CodeMirror
    1 引用 • 2 回帖 • 125 关注
  • Bug

    Bug 本意是指臭虫、缺陷、损坏、犯贫、窃听器、小虫等。现在人们把在程序中一些缺陷或问题统称为 bug(漏洞)。

    71 引用 • 1737 回帖 • 1 关注
  • jsDelivr

    jsDelivr 是一个开源的 CDN 服务,可为 npm 包、GitHub 仓库提供免费、快速并且可靠的全球 CDN 加速服务。

    5 引用 • 31 回帖 • 51 关注
  • Kafka

    Kafka 是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据。 这种动作(网页浏览,搜索和其他用户的行动)是现代系统中许多功能的基础。 这些数据通常是由于吞吐量的要求而通过处理日志和日志聚合来解决。

    35 引用 • 35 回帖
  • 脑图

    脑图又叫思维导图,是表达发散性思维的有效图形思维工具 ,它简单却又很有效,是一种实用性的思维工具。

    21 引用 • 58 回帖
  • SOHO

    为成为自由职业者在家办公而努力吧!

    7 引用 • 55 回帖 • 65 关注
  • GitLab

    GitLab 是利用 Ruby 一个开源的版本管理系统,实现一个自托管的 Git 项目仓库,可通过 Web 界面操作公开或私有项目。

    46 引用 • 72 回帖
  • JavaScript

    JavaScript 一种动态类型、弱类型、基于原型的直译式脚本语言,内置支持类型。它的解释器被称为 JavaScript 引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在 HTML 网页上使用,用来给 HTML 网页增加动态功能。

    713 引用 • 1174 回帖 • 104 关注
  • Bootstrap

    Bootstrap 是 Twitter 推出的一个用于前端开发的开源工具包。它由 Twitter 的设计师 Mark Otto 和 Jacob Thornton 合作开发,是一个 CSS / HTML 框架。

    18 引用 • 33 回帖 • 680 关注
  • Android

    Android 是一种以 Linux 为基础的开放源码操作系统,主要使用于便携设备。2005 年由 Google 收购注资,并拉拢多家制造商组成开放手机联盟开发改良,逐渐扩展到到平板电脑及其他领域上。

    334 引用 • 323 回帖 • 19 关注
  • 面试

    面试造航母,上班拧螺丝。多面试,少加班。

    324 引用 • 1395 回帖
  • 正则表达式

    正则表达式(Regular Expression)使用单个字符串来描述、匹配一系列遵循某个句法规则的字符串。

    31 引用 • 94 回帖
  • 黑曜石

    黑曜石是一款强大的知识库工具,支持本地 Markdown 文件编辑,支持双向链接和关系图。

    A second brain, for you, forever.

    10 引用 • 88 回帖
  • Postman

    Postman 是一款简单好用的 HTTP API 调试工具。

    4 引用 • 3 回帖 • 5 关注
  • abitmean

    有点意思就行了

    38 关注
  • Logseq

    Logseq 是一个隐私优先、开源的知识库工具。

    Logseq is a joyful, open-source outliner that works on top of local plain-text Markdown and Org-mode files. Use it to write, organize and share your thoughts, keep your to-do list, and build your own digital garden.

    5 引用 • 62 回帖
  • NGINX

    NGINX 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 NGINX 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本 0.1.0 发布于 2004 年 10 月 4 日。

    311 引用 • 546 回帖 • 1 关注
  • 星云链

    星云链是一个开源公链,业内简单的将其称为区块链上的谷歌。其实它不仅仅是区块链搜索引擎,一个公链的所有功能,它基本都有,比如你可以用它来开发部署你的去中心化的 APP,你可以在上面编写智能合约,发送交易等等。3 分钟快速接入星云链 (NAS) 测试网

    3 引用 • 16 回帖 • 1 关注
  • Hprose

    Hprose 是一款先进的轻量级、跨语言、跨平台、无侵入式、高性能动态远程对象调用引擎库。它不仅简单易用,而且功能强大。你无需专门学习,只需看上几眼,就能用它轻松构建分布式应用系统。

    9 引用 • 17 回帖 • 600 关注
  • 学习

    “梦想从学习开始,事业从实践起步” —— 习近平

    163 引用 • 473 回帖
  • Firefox

    Mozilla Firefox 中文俗称“火狐”(正式缩写为 Fx 或 fx,非正式缩写为 FF),是一个开源的网页浏览器,使用 Gecko 排版引擎,支持多种操作系统,如 Windows、OSX 及 Linux 等。

    7 引用 • 30 回帖 • 429 关注
  • PHP

    PHP(Hypertext Preprocessor)是一种开源脚本语言。语法吸收了 C 语言、 Java 和 Perl 的特点,主要适用于 Web 开发领域,据说是世界上最好的编程语言。

    165 引用 • 407 回帖 • 510 关注