2012-02-25 72 views
14

任何想法和爲什麼這個工程的建議,從瓦特/ PS運行時,而不是從一個快捷方式運行定義爲當:ChangeDesktop.ps1的PowerShell腳本來更改桌面

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -File "C:\Users\bin\ChangeDesktop.ps1" 

內容:

set-itemproperty -path "HKCU:Control Panel\Desktop" -name WallPaper -value "" 
rundll32.exe user32.dll, UpdatePerUserSystemParameters 

如果我在PS「命令提示符」環境中,桌面背景會自動刪除並刷新,除此之外,我必須手動刷新桌面才能進行更改。

系統是Windows Server 2008 R2 - 全新安裝。 Script executionpolicy被設置爲RemoteSigned,並且我沒有看到任何PS錯誤。我從桌面快捷方式運行時看不到桌面自動刷新。

劃痕頭

+1

快捷方式行事很像CMD提示,因此測試快捷命令行出現,而不是在PowerShell提示符。 – 2012-02-26 01:27:00

回答

26

rundll32.exe user32.dll, UpdatePerUserSystemParameters實際上並沒有改變牆紙,我在2008年64盒。雖然這樣做......它調用Win32 API來調用更改壁紙。如果將它保存爲ChangeDesktop.ps1腳本,它應該可以工作。它會在下面刪除任何桌面壁紙。但是,如果你想設置一個你可以用一個支持的圖像文件的這樣的路徑編輯的最後一行:

[Wallpaper.Setter]::SetWallpaper('C:\Wallpaper.bmp', 0) 

第二個參數是造型:

0:瓷磚 1:中心 2:彈力 3:沒有變化

腳本:

Add-Type @" 
using System; 
using System.Runtime.InteropServices; 
using Microsoft.Win32; 
namespace Wallpaper 
{ 
    public enum Style : int 
    { 
     Tile, Center, Stretch, NoChange 
    } 
    public class Setter { 
     public const int SetDesktopWallpaper = 20; 
     public const int UpdateIniFile = 0x01; 
     public const int SendWinIniChange = 0x02; 
     [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, Wallpaper.Style style) { 
     SystemParametersInfo(SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange); 
     RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true); 
     switch(style) 
     { 
      case Style.Stretch : 
       key.SetValue(@"WallpaperStyle", "2") ; 
       key.SetValue(@"TileWallpaper", "0") ; 
       break; 
      case Style.Center : 
       key.SetValue(@"WallpaperStyle", "1") ; 
       key.SetValue(@"TileWallpaper", "0") ; 
       break; 
      case Style.Tile : 
       key.SetValue(@"WallpaperStyle", "1") ; 
       key.SetValue(@"TileWallpaper", "1") ; 
       break; 
      case Style.NoChange : 
       break; 
     } 
     key.Close(); 
     } 
    } 
} 
"@ 

[Wallpaper.Setter]::SetWallpaper('', 0) 

最初從PoshCode:http://poshcode.org/491

+1

非常感謝,安迪。這確實有效。我會保持好奇,爲什麼從快捷方式運行時,我第一次嘗試的其他方法不起作用。 Windows Powershell有時是一個奇怪的野獸 - 至少在我看來,它依然如此。 – joebalt 2012-02-26 01:13:37

+0

@JoeBaltimore我用'rundll32.exe'得到了奇怪的結果。有時候,它會從手動輸入的控制檯工作,有時不會,有時它會從用快捷方式調用的腳本工作,有時候不會......至少這種方法始終有效:-)它看起來像[其他人](http ://social.technet.microsoft.com/Forums/ar/w7itproui/thread/c5979d78-9732-41a7-8f7b-a9991e318b76)也有更新與'rundll32'壁紙相同的問題。 – 2012-02-26 01:40:39

+0

謝謝。我一直在這方面掙扎數小時。 – Jente 2014-10-07 08:51:01

0

這可能會有些奇怪,但對我而言,使用單引號而不是雙引號。所以它看起來是這樣的:

Set-ItemProperty -path "HKCU:Control Panel\Desktop" -name 'wallpaper' -value 'some value' 
rundll32.exe user32.dll, UpdatePerUserSystemParameters 
-1

這個腳本的作品奇蹟。對於域部署,我們不希望它每次用戶登錄時都不斷更改背景。

我做了以下更改,以便檢查計算機上的背景是否存在於所需的位置,如果它確實存在,則退出,如果它不繼續進行文件複製並設置背景。

它首先映射隱藏的共享,將文件複製到所需的目錄,設置壁紙,然後斷開隱藏的共享。如果貴公司已經使用「X」,則插入另一個驅動器號。 :d

$strFileName="C:\Users\Public\Pictures\background.jpg" 
If (Test-Path $strFileName){ 
    # // File exists 
    Exit-PSSession 
}Else{ 
    # // File does not exist 
New-PSDrive -Name X -PSProvider Filesystem -Root \\hiddenfileshare\wallpapers 
Copy-Item X:\background.jpg C:\Users\Public\Pictures 
[Wallpaper.Setter]::SetWallpaper('C:\Users\Public\Pictures\background.jpg',  0) 
Remove-PSDrive X 
} 
0

通過安迪·阿里斯門迪提供的腳本是真棒!

我用它來作一個有趣的項目 - 設定隨機牆紙從網上刮掉。

我在這裏發佈它感興趣的任何人。在使用它之前,您需要更改腳本源頂部的一些常量。您還需要下載HtmlAgilityPack.dll庫(腳本註釋中有說明)。

享受!

P.S.如果我正在使用的壁紙網站出現故障或更改其佈局,腳本中的拼貼將會變爲地獄,但以我的腳本爲例,我敢打賭,您可以製作另一個壁紙刮板。

############## CONSTANTS ############## 

# add the library for parsing html - HtmlAgilityPack - download it with nuget from https://www.nuget.org/packages/HtmlAgilityPack 
# download nuget command line from https://dist.nuget.org/index.html and install HtmlAgilityPack with "nuget install HtmlAgilityPack" from the cmd 

# enter the path to HtmlAgilityPack.dll library used for html parsing 
$html_parser_path = "C:\Users\username\Documents\htmlagilitypack\HtmlAgilityPack.1.4.9.5\lib\Net20\HtmlAgilityPack.dll" 

# choose where your wallpapers will be downloaded 
$wallpaper_dir_path = "C:\Users\username\Pictures\" 

# get random wallpaper category from wallpaperscraft.com - the ones below are my favourite categories, edit it if you want to get other categories 

<# 
you can choose your favorite wallpaper categories from the list below 
    3D 
    Abstract 
    Animals 
    Anime 
    Brands 
    Cars 
    City 
    Fantasy 
    Flowers 
    Food 
    Games 
    Girls 
    Hi-Tech 
    Holidays 
    Macro 
    Men 
    Movies 
    Music 
    Nature 
    Other 
    Space 
    Sport 
    Textures 
    TV Series 
    Vector 
#> 

$categories = @("animals","flowers","macro","nature","space") 



# I download my wallpapers from the site below - real quality wallpapers 
# don't forget to change your resolution - I'm using a 1920x1080 monitor 

<# 
A list of resolutions to choose from: 

    1600x1200 
    1400x1050 
    1280x1024 
    1280x960 
    1152x864 
    1024x768 
    3840x2400 
    3840x2160 
    3840x1200 
    2560x1600 
    2560x1440 
    2560x1080 
    2560x1024 
    2048x1152 
    1920x1200 
    1920x1080 
    1680x1050 
    1600x900 
    1440x900 
    1280x800 
    1280x720 
    2160x3840 
    1440x2560 
    1366x768 
    1080x1920 
    1024x600 
    960x544 
    800x1280 
    800x600 
    720x1280 
    540x960 
    480x854 
    480x800 
    400x480 
    360x640 
    320x480 
    320x240 
    240x400 
    240x320 
    2732x2732 
    2048x2048 
    1080x1920 
    1024x1024 
    750x1334 
    640x1136 
    640x960 
    320x480 
    1366x768 
    1920x1080 
    360x640 
    1024x768 
    1600x900 
    1280x900 
    1440x900 
    1280x1024 
    800x600 
    1680x1050 
    2560x1440 
    320x480 
    1920x1200 
    480x800 
    720x1280 
#> 

$resolution = "1920x1080" # default resolution 
$url = "https://wallpaperscraft.com/catalog/category/$resolution" # category is a placeholder 

############## END OF CONSTANT DECLARATIONS ############## 

Add-Type @" 
using System; 
using System.Runtime.InteropServices; 
using Microsoft.Win32; 
namespace Wallpaper 
{ 
    public enum Style : int 
    { 
     Tile, Center, Stretch, NoChange 
    } 
    public class Setter { 
     public const int SetDesktopWallpaper = 20; 
     public const int UpdateIniFile = 0x01; 
     public const int SendWinIniChange = 0x02; 
     [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, Wallpaper.Style style) { 
     SystemParametersInfo(SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange); 
     RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true); 
     switch(style) 
     { 
      case Style.Stretch : 
       key.SetValue(@"WallpaperStyle", "2") ; 
       key.SetValue(@"TileWallpaper", "0") ; 
       break; 
      case Style.Center : 
       key.SetValue(@"WallpaperStyle", "1") ; 
       key.SetValue(@"TileWallpaper", "0") ; 
       break; 
      case Style.Tile : 
       key.SetValue(@"WallpaperStyle", "1") ; 
       key.SetValue(@"TileWallpaper", "1") ; 
       break; 
      case Style.NoChange : 
       break; 
     } 
     key.Close(); 
     } 
    } 
} 
"@ 





Add-Type -Path $html_parser_path 

$rand_index = Get-Random -minimum 0 -maximum $categories.Length 
$random_category = $categories[$rand_index] 

# replace the placeholder "category" with the random category chosen above 

$url = $url -replace "category", $random_category 

$doc = New-Object HtmlAgilityPack.HtmlDocument 
$doc.LoadHtml((New-Object System.Net.WebClient).DownloadString($url)) 

# NOTE: the html parser I'm using locates elements by XPath only 
$page_links = $doc.DocumentNode.SelectSingleNode("//div[contains(@class, 'pages')]").SelectNodes("a") 

# get last page link 
$last_page_link = $page_links[$page_links.Count - 1].GetAttributeValue("href", "") 

# get last page number 
$last_page_number = [regex]::match($last_page_link,'.*page(\d+)').Groups[1].Value 

$random_page_number = Get-Random -minimum 0 -maximum $last_page_number 
$random_page_addr = "" 

# page 1 doesn't add anything to the url 

if ($random_page_number -gt 0){ 
    $random_page_addr = "/page$random_page_number" 
} 

$doc.LoadHtml((New-Object System.Net.WebClient).DownloadString("$url$random_page_addr")) 

# get wallpaper divs 
$wallpaper_divs = $doc.DocumentNode.SelectNodes("//div[contains(@class, 'wallpaper_pre')]") 

$random_wallpaper_div = Get-Random -minimum 0 -maximum 15 # there are 15 wallpapers on a page 

# get a sample wallpaper link which has to be substituted later 
$sample_wallpaper_link = $wallpaper_divs[$random_wallpaper_div].SelectNodes("a")[0].GetAttributeValue("href", "") 

# substitute the above link to get the image link itself 

$sample_wallpaper_link = $sample_wallpaper_link -replace "download", "image" 
$sample_wallpaper_link = $sample_wallpaper_link -replace "/$resolution", "_$resolution.jpg" 
$sample_wallpaper_link = $sample_wallpaper_link -replace "//", "https://" 

$wallpaper_image_name = [regex]::match($sample_wallpaper_link,'.*image/(\w+)').Groups[1].Value 
$wallpaper_image_name = "$wallpaper_image_name.jpg" 

$wc = New-Object System.Net.WebClient 

$save_location = "$wallpaper_dir_path$wallpaper_image_name" 
$wc.DownloadFile($sample_wallpaper_link, "$save_location") 

[Wallpaper.Setter]::SetWallpaper($save_location, 1)