2016-01-20 13 views
0

我已經創建了從Windows 10計算機(客戶端)到Windows Server 2012 R2計算機(服務器)的PowerShell會話並嘗試卸載功能:遠程調用 - 要刪除的命令 - WindowsFeature不能用作模塊不可用

$session | Invoke-Command -ScriptBlock { Remove-WindowsFeature Server-Gui-Shell }

我收到的錯誤:

Invoke-Command : The term 'Remove-WindowsFeature' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. 

如果我RDP服務器機器,並啓動PowerShell中,命令是可用的,但在客戶機上它不是,也沒有模塊ServerManager

這意味着我試圖在服務器上運行的命令需要以某種方式在客戶端上可用,這對我來說似乎很奇怪。

我錯過了什麼嗎?

+0

'Invoke-Command'不通過管道接受'PSSession'。 – PetSerAl

+0

就是這樣!雖然錯誤沒有指向它,但將會話作爲參數傳遞會使其工作。我如何將你標記爲答案? –

回答

1

Invoke-Command通過流水線不接受-Session參數的值。通過管道它接受輸入對象,將被傳遞給調用命令:

12345 | Invoke-Command { "Pipeline input: $input" } 
# Pipeline input: 12345 

當你的命令沒有爲遠程調用任何目標,Invoke-Command本地計算機上調用命令。由於本地計算機沒有請求命令,因此會產生錯誤。

+0

這是解決方法,再次感謝。 –