获取微软每日更新的聚焦图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 将复制出来的缓存图片保存在下面的文件夹
# 新增目录
add-type -AssemblyName System.Drawing
New-Item "$($env:USERPROFILE)\Pictures\Spotlight" -ItemType directory -Force;
New-Item "$($env:USERPROFILE)\Pictures\Spotlight\CopyAssets" -ItemType directory -Force;
New-Item "$($env:USERPROFILE)\Pictures\Spotlight\Horizontal" -ItemType directory -Force;
New-Item "$($env:USERPROFILE)\Pictures\Spotlight\Vertical" -ItemType directory -Force;

# 先将横竖图片全部复制到上面新建的CopyAssets文件夹中
foreach($file in (Get-Item "$($env:LOCALAPPDATA)\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets\*"))
{
# 聚焦图片的位置
if ((Get-Item $file).length -lt 100kb) { continue }
Copy-Item $file.FullName "$($env:USERPROFILE)\Pictures\Spotlight\CopyAssets\$($file.Name).jpg";
}

# 将CopyAssets这个临时中转图片的文件夹中的宽度不一的图片分类,分别复制到对应宽度的两个文件夹
foreach($newfile in (Get-Item "$($env:USERPROFILE)\Pictures\Spotlight\CopyAssets\*"))
{
$image = New-Object -comObject WIA.ImageFile;
$image.LoadFile($newfile.FullName);
if($image.Width.ToString() -eq "1920"){ Move-Item $newfile.FullName "$($env:USERPROFILE)\Pictures\Spotlight\Horizontal" -Force; }
# 将宽为1920px的移动到开始新建的Horizontal文件夹中
elseif($image.Width.ToString() -eq "1080"){ Move-Item $newfile.FullName "$($env:USERPROFILE)\Pictures\Spotlight\Vertical" -Force; }
}

# 此处可插入设置壁纸代码

# 移除临时文件夹CopyAssets对象
Remove-Item "$($env:USERPROFILE)\Pictures\Spotlight\CopyAssets\*";
#pause

将图片设置为壁纸

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 壁纸设置函数
function Set-Wallpaper
{
param(
[Parameter(Mandatory=$true)]
$Path,

[ValidateSet('Center', 'Stretch')]
$Style = 'Center'
# 两种壁纸风格
)

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, 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;
}
key.Close();
}
}
}
"@

[Wallpaper.Setter]::SetWallpaper( $Path, $Style )
}

# 设置图片文件夹的路径
$filePath = "$($env:USERPROFILE)\Pictures\Spotlight\Horizontal\*"
# 选中最后写入时间的文件的路径
$file = Get-Item -Path $filePath | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1

# 调用设置壁纸函数,传入目标图像对象的路径
Set-Wallpaper -Path $file.FullName
echo $file.FullName


其他有趣代码

get-random控制台闪烁

在Powershell中有一条命令Get-Random,千万不要以为它的作用仅限于产生随机数。它还可以从固定的集合中随机产生对象呢。

例如控制台的颜色是一个枚举的类型,我们可以将所有支持的控制台背景颜色的枚举值作为一个集合,然后让它随机一个值,并每秒钟设置控制台颜色一次,以此来造成高度闪烁提示用户。不尽然有实用性,但是可以拿来玩!

1
2
3
4
5
6
7
$consoleBackGroundColors=[enum]::GetNames([System.ConsoleColor])
for($i=0; $i -lt 1000000;$i++)
{
$Host.UI.RawUI.BackgroundColor=Get-Random -InputObject $consoleBackGroundColors
cls
sleep -Milliseconds 1
}

摘自:https://www.pstips.net/powershell-generate-object.html

定时抓取屏幕

参考链接:https://www.jb51.net/article/56137.htm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116


=====文件名:Get-TimedScreenshot.ps1=====

function Get-TimedScreenshot {

<#

Author:fuhj(powershell#live.cn ,http://fuhaijun.com)

Takes screenshots at a regular interval and saves them to disk.

.PARAMETER Path

Specifies the folder path.

.PARAMETER Interval

Specifies the interval in seconds between taking screenshots.

.Parameter EndTime

Specifies when the script should stop running in the format HH-MM

.Example

Get-TimedScreenshot -Path c:\temp\ -Interval 30 -EndTime 14:00

#>
[CmdletBinding()] Param(

[Parameter(Mandatory=$True)]

[ValidateScript({Test-Path -Path $_ })]

[String] $Path,
[Parameter(Mandatory=$True)]

[Int32] $Interval,
[Parameter(Mandatory=$True)]

[String] $EndTime

)
#Define helper function that generates and saves screenshot

Function Get-Screenshot {

$ScreenBounds = [Windows.Forms.SystemInformation]::VirtualScreen

$ScreenshotObject = New-Object Drawing.Bitmap $ScreenBounds.Width, $ScreenBounds.Height

$DrawingGraphics = [Drawing.Graphics]::FromImage($ScreenshotObject)

$DrawingGraphics.CopyFromScreen( $ScreenBounds.Location, [Drawing.Point]::Empty, $ScreenBounds.Size)

$DrawingGraphics.Dispose()

$ScreenshotObject.Save($FilePath)

$ScreenshotObject.Dispose()

}

Try {
#load required assembly

Add-Type -Assembly System.Windows.Forms
Do {

#get the current time and build the filename from it

$Time = (Get-Date)
[String] $FileName = "$($Time.Month)"

$FileName += '-'

$FileName += "$($Time.Day)"

$FileName += '-'

$FileName += "$($Time.Year)"

$FileName += '-'

$FileName += "$($Time.Hour)"

$FileName += '-'

$FileName += "$($Time.Minute)"

$FileName += '-'

$FileName += "$($Time.Second)"

$FileName += '.png'
#use join-path to add path to filename

[String] $FilePath = (Join-Path $Path $FileName)
#run screenshot function

Get-Screenshot
Write-Verbose "Saved screenshot to $FilePath. Sleeping for $Interval seconds"
Start-Sleep -Seconds $Interval

}
#note that this will run once regardless if the specified time as passed

While ((Get-Date -Format HH:mm) -lt $EndTime)

}
Catch {Write-Error $Error[0].ToString() + $Error[0].InvocationInfo.PositionMessage}

}


进阶参考

上述链接还提到了以下内容,日后借鉴

调用组件通过SMTP发邮件

用System.Net.Mail.MailMessage组件调用SMTP发送邮件

脚本加密

生成随机名称和电话号码 https://blog.csdn.net/culun797375/article/details/108841296

PowerShell使用教程(转) - 调皮的小孩子的文章 - 知乎 https://zhuanlan.zhihu.com/p/88917107