2013-01-01 20 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]