Public/Set-LockScreenWallpaper.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
function Set-LockScreenWallpaper{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $False)]
        [String]
        $DownloadDirectory = $env:LocalWallpaperDirectory,

        [Parameter(Mandatory = $False)]
        [String]
        $SyncedDirectory = $env:SyncedWallpaperDirectory,

        [Parameter(Mandatory = $False)]
        [String]
        $WallpaperName = "backgroundDefault.jpg",

        [Parameter(Mandatory = $False)]
        [String]
        $URL = $env:WallpaperURLFormatter
    )

    Start-Transcript -LiteralPath ('{0}\Logs\Set-LockScreenWallpaper.ps1.log' -f $env:SystemRoot) -IncludeInvocationHeader -Force

    Write-Information "################################################"
    Write-Information "# Started $(Get-Date)"

    # $SyncedDir = $env:SyncedWallpaperDirectory
    Write-Information "Synced Dir: $SyncedDirectory"

    # $DownloadDir = $env:LocalWallpaperDirectory
    Write-Information "Download Dir: $DownloadDirectory"

    # $Wallpaper = "wallpaper.jpg"
    Write-Information "Wallpaper: $WallpaperName"

    # $URL = $env:WallpaperURLFormatter
    Write-Information "URL: $URL"

    $DownloadDirectory = Get-DownloadDirectory -DownloadDirectory $DownloadDirectory

    Write-Information "Download Dir Exists: $(Test-Path $DownloadDirectory)"

    $Cards = Get-Cards
    $Monitors = Get-Monitors -Cards $Cards
    $Resolution = Get-PrimaryMonitorResolution -Monitors $Monitors

    Write-Information "Downloading ..."

    Write-Information "URL: $($URL -f $Resolution.Width, $Resolution.Height)"

    $ImagePath = (Join-Path $DownloadDirectory $WallpaperName)
    Invoke-WebRequest ($URL -f $Resolution.Width, $Resolution.Height) -OutFile $ImagePath -UseBasicParsing

    if (((Get-ItemProperty $ImagePath -ErrorAction 'Ignore').Length -eq 0) -and (Test-Path (Join-Path $SyncedDirectory $WallpaperName))) {
        Write-Warning "Didn't download; pulling from synced."
        Copy-Item (Join-Path $SyncedDirectory $WallpaperName) $ImagePath -Force
    } else {
        Write-Information "Downloaded File Exists: $ImagePath"
    }

    Write-Information "################################################"
    Stop-Transcript
}