Classes/ImageManager.ps1

class ImageManager {
    [LibraryContext]$Context
    [string]$DefaultWallpaperUrl
    [string]$DefaultLockscreenUrl

    ImageManager([LibraryContext]$context) {
        $this.Context = $context
        $this.DefaultWallpaperUrl  = "https://raw.githubusercontent.com/kittl-partner/intune/refs/heads/main/windows/images/wallpaper.png"
        $this.DefaultLockscreenUrl = "https://raw.githubusercontent.com/kittl-partner/intune/refs/heads/main/windows/images/lockscreen.png"
    }


    # Set Wallpaper #
    # => NEEDS TO RUN AS USER!
    [void]SetWallpaper([string]$Url) {
        if (-not $Url) { $Url = $this.DefaultWallpaperUrl }

        try {
            # Public user => Images folder
            $DestinationDir = "$env:LOCALAPPDATA\Company\Wallpapers"
            if (-not (Test-Path $DestinationDir)) {
                New-Item -Path $DestinationDir -ItemType Directory -Force
            }
            $DestinationPath = Join-Path $DestinationDir "wallpaper.png"

            # Download the image
            $this.Context.LogManager.Log("Downloading Wallpaper from $Url to $DestinationPath")
            Invoke-WebRequest -Uri $Url -OutFile $DestinationPath

            # Set the wallpaper
            $SPI_SETDESKWALLPAPER = 0x0014
            $UpdateIniFile = 0x01
            $SendChangeEvent = 0x02
            $Flags = $UpdateIniFile -bor $SendChangeEvent

            [Wallpaper]::SystemParametersInfo($SPI_SETDESKWALLPAPER, 0, $DestinationPath, $Flags)

            $this.Context.LogManager.Log("Wallpaper set to $DestinationPath")
        } catch {
            $this.Context.LogManager.Log("An error occurred: $_", "ERROR")
        }
    }


    # Set Lockscreen
    # => NEEDS TO RUN AS SYSTEM!
    [void]SetLockscreen([string]$Url) {
        if (-not $Url) { $Url = $this.DefaultLockscreenUrl }

        try {
            $DestinationDir = "$env:SystemRoot\Web\Screen\Company"
            if (-not (Test-Path $DestinationDir)) {
                New-Item -Path $DestinationDir -ItemType Directory -Force
            }
            $DestinationPath = Join-Path $DestinationDir "lockscreen.png"

            # Download the image
            $this.Context.LogManager.Log("Downloading Lockscreen from $Url to $DestinationPath")
            Invoke-WebRequest -Uri $Url -OutFile $DestinationPath

            # Modify the registry to set the lock screen wallpaper
            $RegPath = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\Personalization"

            # Ensure the registry path exists
            If (!(Test-Path $RegPath)) {
                New-Item -Path $RegPath -Force | Out-Null
            }

            # Set the wallpaper path in the registry
            Set-ItemProperty -Path $RegPath -Name "LockScreenImage" -Value $DestinationPath -Type String

            # Force policy update (might not work immediately, restart is recommended)
            gpupdate /force
            Write-Host "Lock screen wallpaper set to $DestinationPath. A restart or sign-out may be required."
            $this.Context.LogManager.Log("Lockscreen set to $DestinationPath")
        } catch {
            $this.Context.LogManager.Log("An error occurred: $_", "ERROR")
        }
    }
}