![見出し画像](https://assets.st-note.com/production/uploads/images/149169845/rectangle_large_type_2_8397fc90e9e82e6d08244254aa5a7506.png?width=1200)
PowerShellで壁紙を変更したい
Windowsの壁紙をPowerShellで変更するコードです。
一部AIに書いてもらいました。
$wallpaperPath = "c:\temp\neko.png" # 画像ファイルフルパス
if($wallpaperPath -eq "" -or $wallpaperPath -eq $null){
write-host "画像ファイルフルパス指定なし"
return
}
if(-not(test-path "$wallpaperPath")) {
write-host "画像ファイルが見つかりません"
return
}
$setwallpapersource = @"
using System.Runtime.InteropServices;
public class wallpaper
{
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 ) {
SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
}
}
"@
Add-Type -TypeDefinition $setwallpapersource
# 壁紙のパスを指定
[wallpaper]::SetWallpaper($wallpaperPath)
全体を関数化して1行目の$wallpaperPathのところを関数の引数にすれば汎用的に使えます。
PowerShellとAIは相性が悪いみたいで、ところどころ間違ったコードを解答しますね。ほかの人はどうやって活用してるんですかね