Last active
March 3, 2017 11:56
-
-
Save jamessantiago/52a8bf8b080461609023 to your computer and use it in GitHub Desktop.
Copies to lockscreen, copies to desktop, applies watermark, detects and applies closest resolution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#expected to be executed from a folder with wallpapers in the format of BackGroundWIDTHxHEIGHT.jpg (e.g. Background1920x1200.jpg) | |
$consoleDebug = $true | |
$remoteDebug = $false | |
$LogFile = "$($env:temp)\Wallpaper_$([int]((get-date -UFormat "%s") / 86400)).log" | |
Function LogMessage($message) | |
{ | |
if ($consoleDebug) | |
{ | |
write-host $message | |
} | |
if ($remoteDebug) | |
{ | |
Add-Content $LogFile -value "$($(date).ToString("ddMMMyyy HH:mm:ss.fff")):`t`t$message" -ea 0 | |
} | |
} | |
#code to force wallpaper to change | |
Add-Type @" | |
using System; | |
using System.Runtime.InteropServices; | |
using Microsoft.Win32; | |
namespace Wallpaper | |
{ | |
public class Setter { | |
[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(20, 0, path, 0x01 | 0x02); | |
} | |
} | |
} | |
"@ | |
#set directory for manual run nonsense | |
[system.io.directory]::SetCurrentDirectory($pwd.ProviderPath) | |
#default resolution | |
$width = 1600 | |
$height = 1200 | |
try | |
{ | |
$monitor = Get-WmiObject win32_desktopmonitor | |
$width = $monitor.ScreenWidth | |
$height = $monitor.ScreenHeight | |
} | |
catch { | |
#it's ok, just continue with defaults | |
LogMessage "Failed to get native resolution: $($_.exception.message)" | |
} | |
#aspect needed to determine closest matches | |
$aspect = $width/$height | |
LogMessage "width: $width" | |
LogMessage "height: $height" | |
LogMessage "aspect: $aspect" | |
LogMessage "Copying backgrounds to lockscreen" | |
cp *.jpg $env:SystemRoot\SysNative\oobe\info\backgrounds -Force | |
LogMessage "Backgrounds copied" | |
#default to use if no exact or closest same aspect is matched | |
$default = "Background1600x1200.jpg" | |
#default for portrait backgrounds e.g. 960/1280 | |
if ($aspect -lt 1) | |
{ | |
$default = "Background1024x1280.jpg" | |
} | |
#distance for closest resolution | |
$distance = [int]::maxvalue | |
#iterate through all backgrounds to find exact or closest match | |
ls *.jpg |% { | |
#extract resolution from file name | |
$res = $_.Name -Replace "Background", "" -replace ".jpg", "" | |
$resolution = $res -split "x" | |
$w = $resolution[0] | |
$h = $resolution[1] | |
$a = $w/$h | |
if ($width -eq $w -and $height -eq $h) #exact match | |
{ | |
$default = $_.Name | |
$distance = 0 | |
} | |
elseif ($aspect -eq $a) { #matches aspect, find distance | |
$d = [math]::abs($width - $w) + [math]::($height - $h) | |
if ($d -lt $distance) #distance is closer than before | |
{ | |
$distance = $d | |
$default = $_.Name | |
} | |
} | |
} | |
LogMessage "Closest resolution detected is $default at distance of $distance" | |
LogMessage "Watermarking background with computer info" | |
try | |
{ | |
$image = new-object System.Drawing.Bitmap $pwd\$default | |
$g = [System.Drawing.Graphics]::FromImage($image) | |
$g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic | |
$g.DrawString($(hostname), $(New-Object System.Drawing.Font "verdana", 32), ` | |
[System.Drawing.SystemBrushes]::HighlightText, $image.Width - 500, $image.Height - 160) | |
$mac = (Get-WmiObject win32_networkadapterconfiguration -Namespace "root\CIMV2" |? {$_.IPEnabled -eq "True"}).MacAddress | |
if ($mac.Count -gt 0) | |
{ | |
$g.DrawString($mac[0], $(New-Object System.Drawing.Font "verdana", 32), ` | |
[System.Drawing.SystemBrushes]::HighlightText, $image.Width - 500, $image.Height - 100) | |
} | |
$image.Save("$pwd\Background_Temp.jpg", ([System.Drawing.Imaging.ImageFormat]::Jpeg)) | |
$default = "BackGround_Temp.jpg" | |
LogMessage "Watermark applied" | |
} | |
Catch { | |
LogMessage "Failed to apply watermark: $($_.message.exception)" | |
} | |
LogMessage "Copying to desktop background" | |
cp $default $env:SystemRoot\SysNative\oobe\info\backgrounds -force | |
cp $default $env:SystemRoot\Web\Wallpaper\theWallpaper.jpg -force | |
try | |
{ | |
$user = (get-wmiobject win32_computerscreen -ea 0).username | |
$user = $(user -split "\\")[1] | |
if (test-path $env:SystemDrive\Users\$user\AppData\Roaming\Microsoft\Windows\Themes) | |
{ | |
cp $default $env:SystemDrive\Users\$user\AppData\Roaming\Microsoft\Windows\Themes\TranscodedWallpaper.jpg -force | |
[Wallpaper.Setter]::SetWallpaper("$env:SystemRoot\Web\Wallpaper\theWallpaper.jpg") | |
LogMessage "Set wallpaper for logged in user $user" | |
} | |
} | |
catch {} | |
LogMessage "Complete" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment