Inconsistency with my code, it seems to work on random occasions - windows

I have made this code that is meant to download a picture and then set it as a wallpaper. My problem is that sometimes it works a couple of tries in a row and sometimes it doesn't work at all.(I have deleted the picture that is set to be downloaded after every single try.) I would be very happy if someone has the solution to this problem. Anyway,... here is the code:
Invoke-WebRequest https://wallpapercave.com/wp/wp69067421.jpg -o E:\skiniw.jpg
Function Set-WallPaper($Value)
{
Set-ItemProperty -path 'HKCU:\Control Panel\Desktop\' -name wallpaper -value $value
rundll32.exe user32.dll, UpdatePerUserSystemParameters
}
Set-WallPaper -value "E:\skiniw.jpg"

Try waiting for the file to be downloaded and test the file is actually there before setting it as the wallpaper:
Start-Job -Name DownloadWallpaper -ScriptBlock { Invoke-WebRequest "https://wallpapercave.com/wp/wp69067421.jpg" -o "E:\skiniw.jpg" }
Wait-Job -Name DownloadWallpaper
if (Test-Path "E:\skiniw.jpg"){
Function Set-WallPaper($Value){
Set-ItemProperty -path 'HKCU:\Control Panel\Desktop\' -name wallpaper -value $value
rundll32.exe user32.dll, UpdatePerUserSystemParameters
}
Set-WallPaper -value "E:\skiniw.jpg"
}

Related

Change Windows Lockscreen with Powershell: Daily Wallpaper in 4k UHD with Bing Daily Wallpaper API (Powershell)

I wanted a program that changes my Wallpaper daily, but the "Daily Desktop Wallpaper"-App only can do Full-HD and the official program from Microsoft is not only Adware, but displays an ugly watermark in the bottom right corner and can't change the Lock Screen, so I made my own small script to do that (with some help) that I wanted to share so others don't have to waste their time with this (hence the long title). It uses an API from github.
To do this everyday automatically, put the following action in a Task Scheduler Task that starts daily at a specific time:
Program/script: powershell.exe
Add arguments: -executionPolicy bypass -WindowStyle hidden -File "path\to\changeDesktopToNewestInPicturesPath.ps1"
To the question:
I still have one small problem: How do I change the Lock Screen? The current implementation does not seem to work... (In comments at the end):
Also, any suggestions are very welcome, as I am still pretty new to Powershell.
$dir = "~/Pictures/DailyWallpapers"
if (-not (Test-Path -Path $dir)) {
mkdir $dir
}
$bingApiRequest = Invoke-RestMethod -Uri "https://bing.biturl.top/?resolution=3840" -ContentType "application/json" -Method Get
$fileName = $bingApiRequest.url.split("=")[-1]
Invoke-WebRequest -Uri $bingApiRequest.url -OutFile "~/Pictures/DailyWallpapers/$($fileName)"
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$filepath = $latest.FullName
$code = #'
using System.Runtime.InteropServices;
namespace Win32{
public class Wallpaper{
[DllImport("user32.dll", CharSet=CharSet.Auto)]
static extern int SystemParametersInfo (int uAction , int uParam , string lpvParam , int fuWinIni) ;
public static void SetWallpaper(string thePath){
SystemParametersInfo(20,0,thePath,3);
}
}
}
'#
add-type $code
#Desktop Wallpaper
[Win32.Wallpaper]::SetWallpaper($filepath)
# $currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
# if ($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
# Write-Host "changing Lock Screen..."
# #Lockscreen
# $regKey = 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\PersonalizationCSP'
# if (!(Test-Path -Path $regKey)) {
# $null = New-Item -Path $regKey
# }
# Set-ItemProperty -Path $regKey -Name LockScreenImagePath -value $filepath
# Set-ItemProperty -Path $regKey -Name LockScreenImageUrl -value $filepath
# Set-ItemProperty -Path $regKey -Name LockScreenImageStatus -value 1
# }

Search for registry key path

please, could you help me with searching for registry path?
I am trying to find path of REG_BINARY with name 00036601 in HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Outlook\Profiles\Outlook\20424cec73cea54ab3d011f91bf036b2
I have problem because last folder(20424cec73cea54ab3d011f91bf036b2) in path is different on every laptop. Cannot find any working solution with REG QUERY in cmd or powershell.
I know how to find it in known path or list all subkeys, but failed to filter one value.
So i want to get output like: key name 00036601 found in HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Outlook\Profiles\Outlook\20424cec73cea54ab3d011f91bf036b2
EDIT: sorry for my english, maybe i am notz describing it correctly, please, could you look on image?
Regedit
I am looking for string name 00036601 - marked in image. Thanks for help
EDIT2: i found way how to do it with cmd "REG QUERY HKCU\SOFTWARE\Microsoft /s /f 00036601"
But not with powershell...
You can search the registry with PowerShell. I do not have the same registry path as you have.
Get-ChildItem -Path HKCU:\Software\Microsoft\Office\16.0 `
-Recurse -ErrorAction SilentlyContinue |
Where-Object { $_.PSChildName -eq '00036601' }
If you must do it from a cmd.exe shell:
powershell -NoLogo -NoProfile ^
"Get-ChildItem -Path HKCU: -Recurse -ErrorAction SilentlyContinue | " ^
"Where-Object { $_.PSChildName -eq '00036601' }"
EDIT: that was never going to get there.
This works on my machine to find the IM enabled setting.
$r = Get-ChildItem -Path 'HKCU:/Software/Microsoft/Office/' -Recurse -ErrorAction SilentlyContinue |
Where-Object { $_.Property -eq 'EnablePresence' }
(Get-ItemProperty -Path $r.PSPath).EnablePresence
Please try this on your machine.
$r = Get-ChildItem -Path 'HKCU:/Software/Microsoft/Office/' -Recurse -ErrorAction SilentlyContinue |
Where-Object { $_.Property -eq '00036601' }
(Get-ItemProperty -Path $r.PSPath).'00036601'

Unable to Set local group policy using powershell

I try to set local group policy using PowerShell. My goal is to enable
Disable changing home page settings
Below is the PowerShell script:
# Set the values as needed
$dchps_control_panel = "HKCU:\Software\Policies\Microsoft\Internet Explorer\Control Panel"
$dchps_main = "HKCU:\Software\Policies\Microsoft\Internet Explorer\Main"
$homepage = "HomePage"
$enabled = 1
$startpage = "Start Page"
$startpage_value = "www.google.com"
# Test if the Registry Key exists already
if(-not (Test-Path $dchps_control_panel) -or -not (Test-Path $dchps_main) )
{
# Create Control Panel Key to enable and Main Key
New-Item -Path $dchps_control_panel -Force
New-Item -Path $dchps_main -Force
}
# Enable Disable changing home page settings
Set-ItemProperty -Path $dchps_control_panel -Name $homepage -Value $enabled -Type DWord
#
# Set Start Page
Set-ItemProperty -Path $dchps_main -Name $startpage -Value $startpage_value -Type String
The registry keys both get created. However, when I check the "gpedit.msc" the setting still remains to disable and nothing was configured.
Thanks

Creating a PowerShell Streamer Function w/youtube-dl, ffmpeg & ffplay

My question is in regards to combining youtube-dl, ffmpeg, ffplay and PowerShell to handle video URLs.
Some examples I've seen have piped a binary stream from youtube-dl to an external player using the Windows Command Prompt as demonstrated:
youtube-dl --output - "https://youtube.com/mygroovycontent" | mpc-hc.exe /play /close -
This works fine in Command Prompt as it does not mangle the binary stream. If you try and run the same command in PowerShell it doesn't handle the binary stream so well and modifies the output, making it unreadable to the external player.
In light of this I've written the following PowerShell function to get around this issue. It tries to mirror a similar function I've written in Bash (See: https://github.com/adamchilcott/.dotfiles/blob/master/.bash_functions.d/streamer.sh)
The reason I've handled youtube-dl, ffmpeg and ffplay seperately is that defining the ffmpeg binary location in youtube-dl as an external program creates some issues when passing it in PowerShell.
I was hoping that someone could take a look at my script and provide some feedback on what I have done here and if it can be improved upon or if a better implementation is already available?
Best,
Adam.
BEGIN POWERSHELL
Function streamer
{
Param
(
[string] $streamURL
)
Begin
{
}
Process
{
$streamDir = "$env:TEMP\YTD.d"
$ytdBin = "Z:\PortableApps\CommandLineApps\youtube-dl\youtube-dl.exe"
$streamExtractor = &$ytdBin --no-warnings --get-url $streamURL
$ffmpegBin = "Z:\PortableApps\CommandLineApps\ffmpeg-20170702-c885356-win64-static\bin\ffmpeg.exe"
$ffplayBin = "Z:\PortableApps\CommandLineApps\ffmpeg-20170702-c885356-win64-static\bin\ffplay.exe"
if
(
-not (Test-Path -Path $streamDir -PathType Any)
)
{
New-Item $streamDir -type directory -ErrorAction SilentlyContinue
}
Start-Process -FilePath $ffmpegBin -ArgumentList "-loglevel quiet -i $streamExtractor -c copy $streamDir\streamContainer.m2ts" -NoNewWindow -ErrorAction SilentlyContinue
Do
{
Start-Sleep -Seconds 1
}
Until
(
(Get-Item $streamDir\streamContainer.m2ts -ErrorAction SilentlyContinue).Length -gt 256kb
)
&$ffplayBin -loglevel quiet $streamDir\streamContainer.m2ts
if
(
(Test-Path -Path $streamDir -PathType Any) -eq $true -and (Get-Process -Name ffplay -ErrorAction SilentlyContinue) -eq $null
)
{
Do
{
Stop-Process -Name ffmpeg -ErrorAction SilentlyContinue
}
Until
(
(Get-Process -Name ffmpeg -ErrorAction SilentlyContinue) -eq $null
)
Remove-Item $streamDir -Recurse -ErrorAction SilentlyContinue
}
}
End
{
}
}
streamer -streamURL https://www.youtube.com/watch?v=9uFXw7vKz14
END POWERSHELL

Change the Show Hidden Items setting on a folder

Where is the setting to "show / hide items" on a particular folder stored?
I looking to be able to change it programmatically, hopefully in Powershell.
So try this :
$file = "c:\temp\t.txt"
# Hidde $file
Set-ItemProperty -Path $file -Name attributes -Value ([io.fileattributes]::Hidden)
# Remove Hidden if set
Set-ItemProperty -Path $file -Name attributes -Value ((Get-ItemProperty $file).attributes -bxor ([io.fileattributes]::Hidden))
I try you're code but this part doesn't work:
Set-ItemProperty -Path $file -Name attributes -Value ((Get-ItemProperty $file).attributes -bxor ([io.fileattributes]::Hidden))
I just replace "hiden" by "normal" and it's work:
Set-ItemProperty -Path $file -Name attributes -Value ([io.fileattributes]::Normal)
Thank you.

Resources