0

I'm trying to update window background using powershell. It works well when I execute it manually.

However for some needs, I want to run it by window scheduler or other automation program. And it doesn't work...

[Params]::SystemParametersInfo($SPI_SETDESKWALLPAPER, 0, $Image, $fWinIni) returns 0. Does anyone know why it doesn't work automatically? (I set the highest authority for scheduler.)

Function Set-WallPaper {


 
   <#
 
    .SYNOPSIS
    Applies a specified wallpaper to the current user's desktop
    
    .PARAMETER Image
    Provide the exact path to the image
 
    .PARAMETER Style
    Provide wallpaper style (Example: Fill, Fit, Stretch, Tile, Center, or Span)
  
    .EXAMPLE
    Set-WallPaper -Image "C:\Wallpaper\Default.jpg"
    Set-WallPaper -Image "C:\Wallpaper\Background.jpg" -Style Fit
  
#>
 
   param (
      [parameter(Mandatory = $True)]
      # Provide path to image
      [string]$Image,
      # Provide wallpaper style that you would like applied
      [parameter(Mandatory = $False)]
      [ValidateSet('Fill', 'Fit', 'Stretch', 'Tile', 'Center', 'Span')]
      [string]$Style
   )
 
   $WallpaperStyle = Switch ($Style) {
  
      "Fill" { "10" }
      "Fit" { "6" }
      "Stretch" { "2" }
      "Tile" { "0" }
      "Center" { "0" }
      "Span" { "22" }
  
   }


 
   If ($Style -eq "Tile") {
 
      New-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name WallpaperStyle -PropertyType String -Value $WallpaperStyle -Force
      New-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name TileWallpaper -PropertyType String -Value 1 -Force
 
   }
   Else {
 
      New-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name WallpaperStyle -PropertyType String -Value $WallpaperStyle -Force
      New-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name TileWallpaper -PropertyType String -Value 0 -Force
 
   }



   Add-Type -TypeDefinition @" 
   using System; 
   using System.Runtime.InteropServices;
     
   public class Params
   { 
       [DllImport("User32.dll",CharSet=CharSet.Unicode)] 
       public static extern int SystemParametersInfo (Int32 uAction, 
                                                      Int32 uParam, 
                                                      String lpvParam, 
                                                      Int32 fuWinIni);
   }
"@ 

  

   $SPI_SETDESKWALLPAPER = 0x0014
   $UpdateIniFile = 0x01
   $SendChangeEvent = 0x02
  
   $fWinIni = $UpdateIniFile -bor $SendChangeEvent

   $ret = [Params]::SystemParametersInfo($SPI_SETDESKWALLPAPER, 0, $Image, $fWinIni)

}


  Set-WallPaper -Image "D:\export\myname\aaa.png" -Style Fit

Tried it using ps1, bat. Neither worked.

Lentasha
  • 1
  • 1
  • 1
    well is the task running as the user you're trying to change the wallpaper or is it running as System? – Santiago Squarzon May 18 '23 at 02:07
  • @SantiagoSquarzon I'm running it with current user account.. Administrator account for now – Lentasha May 18 '23 at 04:41
  • Did you start PS by right click on PS shortcut and select Run As Admin? – jdweng May 18 '23 at 16:05
  • @jdweng sorry for late response. It did work, but what I want to try is to run this script automatically, like window scheduler :( – Lentasha May 21 '23 at 23:32
  • See : https://docs.bmc.com/docs/networkautomationreporting/1911/running-windows-scheduled-tasks-as-administrator-900830633.html?force_isolation=true – jdweng May 22 '23 at 12:19

0 Answers0