2014-09-10 18 views
0

我與都執行這樣的PowerShell腳本工作:如何使用Psconsole.psc1 -Command的Get- {}任何

PS C:\long\path\to\Psconsole.psc1 -Command {Get-whatever...} 

他們都產生了錯誤:

You must provide a value expression on the right hand side of the value '-' operator. *

下面是一個例子:

PS C:\> "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\psconsole.psc1" -Command "Get-Service" 

同樣的錯誤信息:

`You must provide a value expression on the right hand side of the value '-' operator.` 
... At line 1... 
- <<< Command "Get-Service" 
    + CategoryInfo:   PaserError: (:) [], ParentContainsErrorRecordException 
    + FullyQualifiedError: ExpectedValueExpression 

任何人都可以告訴我一種方法來獲得-Command的工作嗎?

回答

0

通過將您的路徑用引號括起來,您告訴PowerShell它是一個字符串,然後 - 之前的「-Command」被解釋爲操作符而不是參數。

要解決這個問題,用「呼叫」操作員「&」前綴的語句:

& "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\psconsole.psc1" -Command "Get-Service" 

編輯:如果您使用Tab鍵路徑的自動完成,控制檯會自動把&當它看到完整路徑中的空間時,請在那裏爲您調用操作員。

+0

該呼叫操作員修復了錯誤。謝謝。現在該命令創建一個PowerShell窗口。但是,它不會執行它中的命令。這可能足以滿足我的需求(需要測試更多),但只是想警告其他讀者。 – dossa22 2014-09-10 22:11:35

相關問題