2014-09-19 94 views
0

我正在脚本化任务以定期远程重新启动一些服务器应用程序池。我正在使用Invoke-Command,如下所示:Powershell调用命令查询

Invoke-Command -ComputerName $server {Restart-WebItem "IIS:\AppPools\DefaultAppPool"} 

这个工作就好了;但是,如果我参数化应用程序池如下

$appPool = "IIS:\AppPools\DefaultAppPool" 
Invoke-Command -ComputerName $server {Restart-WebItem $appPool} 

它失败

Unexpected object type. 
Parameter name: pspath 

我认为这仅仅是一个语法问题,但我不知道是什么。

回答

1

远程主机$ appPool将不存在。 如果您使用PS V3可以用using:关键词前缀的变量 - >

$appPool = "IIS:\AppPools\DefaultAppPool" 
Invoke-Command -ComputerName $server {Restart-WebItem $using:appPool} 

前3版本,你必须使用-argumentList参数

+0

优秀的使用$语法工作。奇怪的是,我的印象是,在命令被远程执行之前,本地变量已经解决了。 我正在使用-ArgumentList获取相同的错误消息。 – 2014-09-19 13:18:07

+0

@KevinRoche nope,这就是为什么'-argumentlist'用于:“提供命令中局部变量的值。在命令在远程计算机上运行之前,命令中的变量被这些值替换。” – 2014-09-19 13:21:36

+0

刚刚用-ArgumentList尝试了一些测试,但我错过了一些东西。这个函数的-PSPath参数是一个字符串数组,所以我尝试了{Restart-WebItem} -ArgumentList(,$ appPool)和{Restart-WebItem} -ArgumentList $ appPool - 与原始相同的错误。什么是正确的语法? – 2014-09-19 13:47:07