Powershell – Remote Invoke-Command Powershell Stop-WebAppPool No Longer Working After Powershell 5 Upgrade

powershellpowershell-v5.0

We have a PowerShell script that takes down a remote app pool via the following commands:

$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"

This script has worked for a very long time when the build server that is issuing the command is on PowerShell 4 and the remote server is on PowerShell version 2. However, this weekend I upgraded the build server to Windows Management Framework 5 (and Powershell 5) and the Stop-WebAppPool command stopped working when ran remotely via Invoke-Command via our build server. I confirmed that from my local machine which is also on PowerShell 5 I also cannot issue this command. However, from any machine that is on Powershell 4 I CAN issue this command to the remote server and it works.

Other things I've tried that may be pertinent:
* If I establish a remote PowerShell session and issue the command interactively it works fine.
* I can run the command to check the app pool status and it works fine: Invoke-Command -ComputerName $server -scriptBlock $checkAppPoolStatusScript -ArgumentList $appPoolname
* Establishing a session and then calling Invoke-Command -Session $mySession... didn't help either. It still does not stop the app pool.

Any help would be greatly appreciated. I'm wondering if there's an issue with Powershell 5 issuing remote commands to PowerShell 2… or maybe something related to security changed when installing Windows Management Framework 5… or… who knows.

Best Answer

You can pass a configuration to the Invoke-Command cmdlet to have it use Powershell 2.

Register-PSSessionConfiguration -Name PS2 -PSVersion 2.0 Invoke-Command -ConfigurationName PS2 -ComputerName $env:computername -ScriptBlock {$PSVersionTable.PSVersion}

For example

Related Topic