-
Notifications
You must be signed in to change notification settings - Fork 0
/
SetDesktopWallpaperWindows.ps1
143 lines (125 loc) · 5.71 KB
/
SetDesktopWallpaperWindows.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
Begin {
#Set global variables
$directory = "C:\Temp"
#Log Function
function Write-LogEntry {
param (
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Value,
[parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[string]$FileName = "SetDesktopWallpaper.log",
[switch]$Stamp
)
#Build Log File appending System Date/Time to output
$LogFile = Join-Path -Path $env:SystemRoot -ChildPath $("Temp\$FileName")
$Time = -join @((Get-Date -Format "HH:mm:ss.fff"), " ", (Get-WmiObject -Class Win32_TimeZone | Select-Object -ExpandProperty Bias))
$Date = (Get-Date -Format "MM-dd-yyyy")
If ($Stamp) {
$LogText = "<$($Value) <time=""$($Time)"" date=""$($Date)"">"
}
else {
$LogText = "$($Value)"
}
Try {
Out-File -InputObject $LogText -Append -NoClobber -Encoding Default -FilePath $LogFile -ErrorAction Stop
}
Catch [System.Exception] {
Write-Warning -Message "Unable to add log entry to $LogFile.log file. Error message at line $($_.InvocationInfo.ScriptLineNumber): $($_.Exception.Message)"
}
}
#Function to apply Desktop Wallpaper
Function DownloadDesktopWallpaper {
#Define URL and download directory
$url = "<url>"
$DownloadDirectory = $directory + "\Wallpaper.jpg"
(New-Object System.Net.WebClient).DownloadFile($url,$DownloadDirectory)
}
Function ApplyDesktopWallpaper {
#Create registry file values
$RegKeyPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\PersonalizationCSP"
$DesktopPath = "DesktopImagePath"
$DesktopStatus = "DesktopImageStatus"
$DesktopUrl = "DesktopImageUrl"
$StatusValue = "1"
$DesktopImageValue = $directory + "\Wallpaper.jpg"
#Build Registry and deploy Desktop Wallpaper
if (!(Test-Path $RegKeyPath)) {
Write-Host "Creating registry path $($RegKeyPath)."
New-Item -Path $RegKeyPath -Force | Out-Null
}
Try {
Write-Host "Attempting to create $($DesktopSatus) Registry and assign Value of $($StatusValue)..."
Write-LogEntry -Value "Attempting to create $($DesktopSatus) Registry and assign Value of $($StatusValue)..."
New-ItemProperty -Path $RegKeyPath -Name $DesktopStatus -Value $StatusValue -PropertyType DWORD -Force | Out-Null
Write-Host "Successfully created new registry value."
Write-LogEntry -Value "Successfully created new registry value."
}
Catch [System.Exception] {
Write-Host " (Failed)"
Write-LogEntry -Value "Failed to create $($DesktopStatus) Registry."
}
Try {
Write-Host "Attempting to create $($DesktopPath) Registry and assign Value of $($DesktopImageValue)..."
Write-LogEntry -Value "Attempting to create $($DesktopPath) Registry and assign Value of $($DesktopImageValue)..."
New-ItemProperty -Path $RegKeyPath -Name $DesktopPath -Value $DesktopImageValue -PropertyType STRING -Force | Out-Null
Write-Host "Successfully created new registry value."
Write-LogEntry -Value "Successfully created new registry value."
}
Catch [System.Exception] {
Write-Host " (Failed)"
Write-LogEntry -Value "Failed to create $($DesktopPath) Registry."
}
Try {
Write-Host "Attempting to create $($DesktopUrl) Registry and assign Value of $($DesktopImageValue)..."
Write-LogEntry -Value "Attempting to create $($DesktopUrl) Registry and assign Value of $($DesktopImageValue)..."
New-ItemProperty -Path $RegKeyPath -Name $DesktopUrl -Value $DesktopImageValue -PropertyType STRING -Force | Out-Null
Write-Host "Successfully created new registry value."
Write-LogEntry -Value "Successfully created new registry value."
}
Catch [System.Exception] {
Write-Host " (Failed)"
Write-LogEntry -Value "Failed to create $($DesktopUrl) Registry."
}
RUNDLL32.EXE USER32.DLL, UpdatePerUserSystemParameters 1, True
}
Write-LogEntry -Value "##################################"
Write-LogEntry -Stamp -Value "Set Desktop Wallpaper Started"
Write-LogEntry -Value "##################################"
If ((Test-Path -Path $directory) -eq $false) {
Write-Hosty "Creating directory to download image file..."
Write-LogEntry -Value "Creating directory to download image file..."
Try {
New-Item -Path $directory -ItemType directory
}
Catch {
Write-Warning "There was an error while attempting to create directory"
Write-LogEntry -Value "There was an error when attempting to create directory"
}
Write-Host "Created directory for image file."
Write-LogEntry -Value "Created directory for image file."
}
Try {
Write-Host "Downloading Image to Computer..."
Write-LogEntry -Value "Downloading Image to Computer..."
DownloadDesktopWallpaper
Write-Host "Downloaded Image successfully."
Write-LogEntry "Downloaded Image successfully."
}
Catch {
Write-Warning $_.Exception
Write-LogEntry -Value "$($_.Exception)"
}
Try {
Write-Host "Attempting to set Image as Desktop Wallpaper..."
Write-LogEntry "Attempting to set Image as Desktop Wallpaper..."
ApplyDesktopWallpaper
Write-Host "Successfully set Desktop Wallpaper"
Write-LogEntry "Successfully set Desktop Wallpaper"
}
Catch {
Write-Warning $_.Exception
Write-LogEntry -Value "$($_.Exception)"
}
}