我们有一个PowerShell脚本,通过以下命令closures远程应用程序池:
$appPoolName = "myAppPool" $server = "myserver.domain.com" $stopAppPoolScript = {param($appPoolname); Import-Module WebAdministration; Stop-WebAppPool -Name $appPoolName;} #remotely execute script to stop the app pool Invoke-Command -ComputerName $server -scriptBlock $stopAppPoolScript -ArgumentList $appPoolname #sleep for 10 seconds Start-Sleep -s 10 #print out the status of the app pool Invoke-Command -ComputerName $server -scriptBlock $checkAppPoolStatusScript -ArgumentList $appPoolname #always says "Started"
这个脚本已经工作了很长时间,当发出命令的构build服务器是在PowerShell 4上,而远程服务器是在PowerShell版本2上。但是,这个周末我把构build服务器升级到Windows Management Framework 5(和Powershell 5 ),而通过我们的构build服务器通过Invoke-Command远程运行时,Stop-WebAppPool命令停止工作。 我确认,从我的本地机器,这也在PowerShell 5我也不能发出这个命令。 但是,从任何位于Powershell 4上的机器上,我都可以将这个命令发送到远程服务器,并且它可以工作。
其他事情我已经尝试过,可能是相关的:*如果我build立一个远程的PowerShell会话,交互式地发出命令,它工作正常。 *我可以运行命令来检查应用程序池的状态,它工作正常: Invoke-Command -ComputerName $server -scriptBlock $checkAppPoolStatusScript -ArgumentList $appPoolname *build立一个会话,然后调用Invoke-Command -Session $mySession...也没有帮助。 它仍然不会停止应用程序池。
任何帮助将不胜感激。 我想知道Powershell 5发布PowerShell 2的远程命令是否存在问题,或者安装Windows Management Framework 5时可能与安全相关的更改…或者谁知道。
您可以将configuration传递给Invoke-Command cmdlet以使其使用Powershell 2。
Register-PSSessionConfiguration -Name PS2 -PSVersion 2.0 Invoke-Command -ConfigurationName PS2 -ComputerName $env:computername -ScriptBlock {$PSVersionTable.PSVersion}
例如