$bing="www.bing.com" $xmlURL="http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1&mkt=en-US" $saveDir="D:\wallpapers\" $picExt=".jpg" $desiredPicRes="_1440x900"
$fullstop = $false$saveDir ile belirtilen klasörün var olduğundan emin olmalıyız. Fedora için yaptığımız gibi var olsun olmasın yaratılması için
New-Item -ItemType Directory -Path $saveDir -ErrorAction SilentlyContinue | Out-Null
if (-Not (Test-Path $saveDir))
{
New-Item -ItemType Directory _path $saveDir
}
[xml]$response = (New-Object System.Net.WebClient).DownloadString($xmlURL) $defaultPicURL = $response.images.image.url $desiredPicURL = $response.images.image.urlbase + $desiredPicRes + $picExt
$checkfile = $checkfile.split(".")[1] + "." + $checkfile.split(".")[2]
try {
$resp = iwr -Uri ($bing + $desiredPicURL) -EA Stop -Method Head
# desired URL OK
Write-Host "desired res OK."
$picName = $desiredPicURL.split(".")[1] + "." + $desiredPicURL.split(".")[2]
$fullPath = Join-Path -Path $saveDir -ChildPath $picName
}
catch {
# desired not available
try {
$resp = iwr -Uri ($bing + $defaultPicURL) -EA Stop -Method Head
Write-Host "desired not found, falling back to default."
$picName = $defaultPicURL.split("&")[0]
$picName = $picName.split(".")[1] + "." + $picName.split(".")[2]
$fullPath = Join-Path -Path $saveDir -ChildPath $picName
}
catch {
# neither default available
Write-Host "None available right now."
$fullstop = $true
Break
}
}
finally {
if ($fullstop) {
# here means nothing found
Add-Content -Path $logfile -Value "$bugun - nothing found."
}
else {
# hear means something is found, either desired or default
try {
$resp = iwr -Uri ($bing + $defaultPicURL) -OutFile $fullPath
Add-Content -Path $logfile -Value "$bugun - downloaded : $fullpath"
Set-WallPaper $fullpath
}
catch {
$hata = $PSItem.Exception.ToString()
Write-Host "last stage error: $hata"
Add-Content -Path $logfile -Value "$bugun - last stage error."
}
}
}
Write-Host "File is already downloaded:" (dir (Join-Path -Path $saveDir -ChildPath ($checkfile + "*")))[0]
Add-Content -Path $logfile -Value "$bugun - file is already downloaded."
}
Hayatını kodlama ile kazananlar; yukarıdaki karmaşa için kusura bakmayın.
Sonrasında bunu Görev Zamanlayıcısıyla otomatik çalıştırmaya ayarlamak kalıyor. Bunun için Set-ExecutionPolicy ile çalıştırma politikasını uygun şekilde değiştirmek, ps1 dosyasını imzalamak ya da komut satırından powershell.exe ile birlikte -ExecutionPolicy anahtarını kullanmak gerekebilir.
Function Set-WallPaper($Image) {<#.SYNOPSISApplies a specified wallpaper to the current user's desktop .PARAMETER ImageProvide the exact path to the image.EXAMPLESet-WallPaper -Image "C:\Wallpaper\Default.jpg"#>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)}