2016-01-20 57 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

这是解决方法,再次感谢。 –