How to change the lock screen with powershell using LockScreen.SetImageFileAsync method

0

I tried using the scripts mentioned in Windows 10 Lock Screen Changing After Sign Out, https://itectec.com/superuser/how-to-change-the-lock-screen-picture-automatically/ and Using a UWP API Namespace in PowerShell

# Change this to the path where you keep the desired background image
$imagePath = 'C:\path\to\image.ext'

$newImagePath = [System.IO.Path]::GetDirectoryName($imagePath) + '\' + (New-Guid).Guid + [System.IO.Path]::GetExtension($imagePath)
Copy-Item $imagePath $newImagePath
[Windows.System.UserProfile.LockScreen,Windows.System.UserProfile,ContentType=WindowsRuntime] | Out-Null
Add-Type -AssemblyName System.Runtime.WindowsRuntime
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0]
Function Await($WinRtTask, $ResultType) {
    $asTask = $asTaskGeneric.MakeGenericMethod($ResultType)
    $netTask = $asTask.Invoke($null, @($WinRtTask))
    $netTask.Wait(-1) | Out-Null
    $netTask.Result
}
Function AwaitAction($WinRtAction) {
    $asTask = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and !$_.IsGenericMethod })[0]
    $netTask = $asTask.Invoke($null, @($WinRtAction))
    $netTask.Wait(-1) | Out-Null
}
[Windows.Storage.StorageFile,Windows.Storage,ContentType=WindowsRuntime] | Out-Null
$image = Await ([Windows.Storage.StorageFile]::GetFileFromPathAsync($newImagePath)) ([Windows.Storage.StorageFile])
AwaitAction ([Windows.System.UserProfile.LockScreen]::SetImageFileAsync($image))
Remove-Item $newImagePath

but they give an error:

Exception calling "Wait" with "1" argument(s): "One or more errors occurred."
At D:\Lockscr.ps1:21 char:5
+     $netTask.Wait(-1) | Out-Null
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : AggregateException

Exception             : System.Management.Automation.MethodInvocationException: Exception calling "Wait" with "1" argument(s): "One or more 
                        errors occurred." ---> System.AggregateException: One or more errors occurred. ---> System.IO.FileNotFoundException: 
                        The system cannot find the file specified. (Exception from HRESULT: 0x80070002)
                           --- End of inner exception stack trace ---
                           at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
                           at CallSite.Target(Closure , CallSite , Object , Int32 )
                           --- End of inner exception stack trace ---
                           at System.Management.Automation.ExceptionHandlingOps.ConvertToMethodInvocationException(Exception exception, Type 
                        typeToThrow, String methodName, Int32 numArgs, MemberInfo memberInfo)
                           at CallSite.Target(Closure , CallSite , Object , Int32 )
                           at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
                           at System.Management.Automation.Interpreter.DynamicInstruction`3.Run(InterpretedFrame frame)
                           at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
TargetObject          : 
CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
FullyQualifiedErrorId : AggregateException
ErrorDetails          : 
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at AwaitAction, D:\DATA\WALLP\lockscr\Lockscr2.ps1: line 21
                        at <ScriptBlock>, D:\DATA\WALLP\lockscr\Lockscr2.ps1: line 25
powershell
lock-screen
windows-10-v2004
asked on Super User Dec 2, 2020 by Kovinda Karnanayake • edited Dec 3, 2020 by Kovinda Karnanayake

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0