通过快捷方式更改桌面的 Powershell 脚本

2024-05-28

关于为什么从 w/in PS 运行时有效,但从定义为以下的快捷方式运行时无效的任何想法和建议:

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

ChangeDesktop.ps1 的内容:

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

如果我在 PS“命令提示符”环境中,桌面背景会自动删除并刷新,除此之外我必须手动刷新桌面才能实现更改。

系统是Windows Server 2008 R2 - 全新安装。脚本执行策略设置为 RemoteSigned,我没有看到任何 PS 错误。从桌面快捷方式运行时,我只是看不到桌面自动刷新。

抓伤头


rundll32.exe user32.dll, UpdatePerUserSystemParameters实际上并没有为我更改 2008 x64 盒子上的壁纸。但这确实做到了...它调用 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 http://poshcode.org/491

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过快捷方式更改桌面的 Powershell 脚本 的相关文章

随机推荐