-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwotd.ps1
114 lines (94 loc) · 3.65 KB
/
wotd.ps1
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
# Arguments manager
param ($Setup, $Locale, $Dest)
$RegKeyPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\PersonalizationCSP"
############ Installation ############
if ($Setup -eq "install" -Or $Setup -eq "uninstall") {
if ([bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544") -eq $false) {
Write-Host "[Warning] Need administrator rights!"
Exit
}
}
if ($Setup -eq "install" ) {
# Set default language code
if (!$Locale) {
$Locale = "fr-FR"
}
# Set default location
if (!$Dest) {
$DriveLetter = (Get-PSDrive -PSProvider FileSystem | Select-Object -ExpandProperty Name -First 1)
$Dest = $DriveLetter + ":\WOTD"
mkdir "$($DriveLetter):\WOTD"
}
# Check if path is whithout space
if (($Dest | Select-String -pattern " ").length -gt 0) {
Write-Host "[Warning] Make sure destination path is without spaces"
Exit
}
# Copy file to location
Copy-Item "$($PSScriptRoot)\wotd.ps1" -Destination "$($Dest)"
Copy-Item "$($PSScriptRoot)\startup.vbs" -Destination "$($Dest)"
Copy-Item "$($PSScriptRoot)\README.md" -Destination "$($Dest)"
# Set new task scheduler
$action = New-ScheduledTaskAction -Execute "wscript" -Argument "//nologo `"$($Dest)\startup.vbs`" $($Locale) `"`"`"$($Dest)`"`"`""
$trigger = New-ScheduledTaskTrigger -AtLogon
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "WallpaperOfTheDay" -Description "Daily change wallpaper" -Settings $settings
# Set registry keys for lockscreen wallpaper
if(!(Test-Path $RegKeyPath)) {
New-Item -Path $RegKeyPath -Force
}
}
if ($Setup -eq "uninstall" ) {
Set-Location -Path "..\"
Remove-Item -Path $RegKeyPath
Remove-Item $PSScriptRoot -Force -Recurse
Unregister-ScheduledTask -TaskName "WallpaperOfTheDay" -Confirm:$false
Exit
}
############ Set wallpaper ############
# Check internet connection for 5 minutes by step of 30 seconds
For ($i = 0; $i -lt 10; $i++) {
if (Test-Connection 1.1.1.1 –Count 1 –Quiet) {
break
}
elseif ($i -eq 9) {
Exit
}
else {
Start-Sleep -s 30
}
}
# Downloading image
try {
$WebClient = New-Object System.Net.WebClient
$json = $WebClient.DownloadString("https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=$($Locale)")
$JsonObject = $json | ConvertFrom-Json
$WebClient.DownloadFile("https://bing.com" + $JsonObject.images.url, $Dest + "\wotd.jpg")
}
catch [System.Net.WebException],[System.IO.IOException],[System.Net.ArgumentNullException] {
Exit
}
# Wallpaper setting class
$setwallpapersrc = @"
using System.Runtime.InteropServices;
public class Wallpaper
{
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);
}
}
"@
Add-Type -TypeDefinition $setwallpapersrc
# Set desktop wallpaper
Start-Sleep -s 3 # To prevent black screen after downloading image
[Wallpaper]::SetWallpaper("$($Dest)\wotd.jpg")
# Set lockscreen wallpaper
New-ItemProperty -Path $RegKeyPath -Name "LockScreenImageStatus" -Value "1" -PropertyType DWORD -Force
New-ItemProperty -Path $RegKeyPath -Name "LockScreenImagePath" -Value "$($Dest)\wotd.jpg" -PropertyType STRING -Force
New-ItemProperty -Path $RegKeyPath -Name "LockScreenImageUrl" -Value "$($Dest)\wotd.jpg" -PropertyType STRING -Force