我正在尝试将使用AD缩略图照片的东西组合在一起,在Windows 8上设置用户的帐户图片。似乎我应该可以使用WinRT中的API来执行此操作。我从各种来源拼凑了一些关于从powershell调用API的东西,但是我无法让它工作。这是我尝试做的一个例子:
$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)
我尝试了其他几种变体,但无论我做什么,我最终都会遇到这样的错误:
Cannot convert argument "image", with value: "System.Object[]", for "setAccountPictureAsync" to type "Windows.Storage.IStorageFile" . . .
我是否有一些简单的东西让我无法完成这项工作?
我发现this blog post by Keith Hill似乎可能有帮助,但我不确定它是否会直接转化为我遇到的问题。
谢谢!
Aurock
答案 0 :(得分:0)
SetAccountPicture
期望一个对象实现IStorageFile而不是字节数组。我会将图片保存到您的Pictures文件夹,然后将其加载到StorageFile中,如下所示。您应该能够将该对象传递给SetAccountPicture()方法,例如
$photoPath = "$home\Pictures\Photo.jpg"
$asyncOp = [Windows.Storage.StorageFile]::GetFileFromPathAsync($photoPath)
$typeName = 'PoshWinRT.AsyncOperationWrapper[Windows.Storage.StorageFile]'
$wrapper = new-object $typeName -Arg $asyncOp
$file = $wrapper.AwaitResult()
$asyncOp = [Windows.System.UserProfile.UserInformation]::SetAccountPictureAsync($file)
$typeName = 'PoshWinRT.AsyncOperationWrapper[Windows.System.UserProfile.SetAccountPictureResult]'
$wrapper = new-object $typeName -Arg $asyncOp
$result = $wrapper.AwaitResult()
$wrapper.Dispose()
答案 1 :(得分:0)
https://fleexlab.blogspot.com/2018/02/using-winrts-iasyncoperation-in.html有一个纯PowerShell解决方案。
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])