2013-01-01 56 views
4

有没有方法从PowerShell脚本调用Windows运行时(WinRT)类(或对象)?我知道,你可以调用COM对象,WinRT的类都应该被“暴露”作为......但到目前为止,我的尝试都失败了......从PowerShell调用Windows运行时类

这是我的代码,我想:

$lockscreen = New-Object -comObject Windows.System.UserProfile.LockScreen 

使我有以下错误:

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)). 

有谁知道正确的“COM类”,我应该使用WinRT的班?

+0

Hmmmm做。好问题。至少你需要使用PowerShell v3(无论如何都使用Win8默认)和.NET 4.5。 Scott Hanselman的这篇博文似乎表明它可能来自C#,所以PowerShell也应该可以这样做。 http://www.hanselman.com/blog/HowToCallWinRTAPIsInWindows8FromCDesktopApplicationsWinRTDiagram.aspx – Goyuix

回答

4

这里是东西哈克,似乎工作:

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 
... 

注意,由于的LockScreen没有构造函数的第一次调用失败,但该呼叫做一些事情在WinRT的投影/元数据,例如,你现在可以打电话拉LockScreen类中的静态方法/属性。

免责声明:没有,我可以在这个新对象语法找到这样是完全可能的,微软可以改变它认为它本质上是一个“隐藏”的,可能没有完全开发功能的任何文档。

+0

Keith,你可以看看http://serverfault.com/questions/465884/what-the-heck-is-going-on-with-这个广告过滤器在PowerShell的?似乎它应该在你的街道上。 – Iain

1

只是引用类型,“装入”装配......

[Windows.System.UserProfile.LockScreen,Windows.System.UserProfile,ContentType=WindowsRuntime] 
[Windows.System.UserProfile.LockScreen]::OriginalImageFile 

如果你不想在你的PowerShell的结果要返回的类型,然后

$null = [Windows.System.UserProfile.LockScreen,Windows.System.UserProfile,ContentType=WindowsRuntime]