从 PowerShell 调用 Windows 运行时类

Jul*_*ing 6 powershell windows-runtime

有没有办法从 PowerShell 脚本调用 Windows 运行时 (WinRT) 类(或对象)?我知道您可以调用 COM 对象,WinRT 类应该“公开”为……但到目前为止我的尝试都失败了……

这是我正在尝试的代码:

$lockscreen = New-Object -comObject Windows.System.UserProfile.LockScreen
Run Code Online (Sandbox Code Playgroud)

这给了我以下错误:

New-Object : Retrieving the COM class factory for component with CLSID {00000000-0000-0000-0000-000000000000} failed
due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
Run Code Online (Sandbox Code Playgroud)

有谁知道我应该用于 WinRT 类的正确“COM 类”吗?

Kei*_*ill 6

这是一些似乎有效的hacky:

PS> new-object "Windows.System.UserProfile.LockScreen,Windows.System.UserProfile,ContentType=WindowsRuntime"
new-object : Constructor not found. Cannot find an appropriate constructor for type
Windows.System.UserProfile.LockScreen,Windows.System.UserProfile,ContentType=WindowsRuntime.
At line:1 char:1
+ new-object "Windows.System.UserProfile.LockScreen,Windows.System.UserProfile,Con ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand

PS> [Windows.System.UserProfile.LockScreen]::OriginalImageFile


AbsolutePath   : C:/Windows/Web/Screen/img100.png
AbsoluteUri    : file:///C:/Windows/Web/Screen/img100.png
LocalPath      : C:\Windows\Web\Screen\img100.png
Authority      :
HostNameType   : Basic
IsDefaultPort  : True
IsFile         : True
IsLoopback     : True
PathAndQuery   : C:/Windows/Web/Screen/img100.png
...
Run Code Online (Sandbox Code Playgroud)

请注意,第一次调用失败,因为 LockScreen 没有构造函数,但该调用执行某些操作来拉入 WinRT 投影/元数据,以便您现在可以调用 LockScreen 类上的静态方法/属性。

免责声明:我找不到有关此 New-Object 语法的任何文档,因此考虑到它本质上是一个“隐藏”且可能未完全开发的功能,Microsoft 完全有可能对其进行更改。


klu*_*msy 5

只需引用类型,即可“加载”程序集...

[Windows.System.UserProfile.LockScreen,Windows.System.UserProfile,ContentType=WindowsRuntime]
[Windows.System.UserProfile.LockScreen]::OriginalImageFile
Run Code Online (Sandbox Code Playgroud)

如果您不希望在 powershell 结果中返回该类型,则

$null = [Windows.System.UserProfile.LockScreen,Windows.System.UserProfile,ContentType=WindowsRuntime]
Run Code Online (Sandbox Code Playgroud)