从Powershell调用WinRT Async方法在win8中设置账户图片 [英] Call WinRT Async method from Powershell to set account picture in win8
问题描述
我正在尝试组合一些将使用 AD 缩略图照片在 Windows 8 上设置用户帐户图片的内容.似乎我应该能够使用 WinRT 的 API 来执行此操作.我已经从各种来源拼凑了一些关于从 powershell 调用 API 的内容,但我无法让它工作.这是我尝试做的一个例子:
I'm trying to put something together that will use the AD Thumbnail photo to set a user's account picture on Windows 8. It seems like I should be able to use the API from WinRT to do this. I've pieced something together from various sources about calling the API from powershell, but I can't get it working. Here's an example of what I've tried to do:
$photo = ([ADSISEARCHER]"samaccountname=$($username)").findone().properties.thumbnailphoto
$path = "C:\temp\Photo.jpg"
$photo | set-content $path -encoding byte
[Windows.System.UserProfile.UserInformation,Windows.System.UserProfile,ContentType=WindowsRuntime] > $null
[Windows.System.UserProfile.UserInformation]::SetAccountPictureAsync($photo)
我尝试了其他几种变体,但无论我做什么,最终都会出现这样的错误:
I've tried a couple of other variations, but no matter what I do, I end up with an error like this:
Cannot convert argument "image", with value: "System.Object[]", for "setAccountPictureAsync" to type "Windows.Storage.IStorageFile" . . .
我在这里缺少一些简单的东西来完成这项工作吗?
Is there something simple that I'm missing here to make this work?
我找到了这个博客基思·希尔 (Keith Hill) 的帖子 似乎可能有帮助,但我不确定它是否直接转化为我遇到的问题.
I found this blog post by Keith Hill which seems like it might be helpful, but I am not sure if it directly translates to the issue I'm having.
谢谢!
欧洛克
推荐答案
https://fleexlab.blogspot.com/2018/02/using-winrts-iasyncoperation-in.html 有一个纯 PowerShell 解决方案.
https://fleexlab.blogspot.com/2018/02/using-winrts-iasyncoperation-in.html has a pure-PowerShell solution.
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
}
这可以用作:
$photoPath = "$home\Pictures\Photo.jpg"
$file = Await ([Windows.Storage.StorageFile]::GetFileFromPathAsync($photoPath)) ([Windows.Storage.StorageFile])
$result = Await ([Windows.System.UserProfile.UserInformation]::SetAccountPictureAsync($file)) ([Windows.System.UserProfile.SetAccountPictureResult])
这篇关于从Powershell调用WinRT Async方法在win8中设置账户图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!