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.

  • Installing Windows Management Framework / PowerShell 4 on the target server solved the problem -- although it doesn't really answer the question of why does this happen and is there a quick fix. – jakejgordon Oct 3 '16 at 18:25
  • It's nearly impossible to guide you without an error. Trap the error and write it out so we know what's wrong. – Colyn1337 Oct 6 '16 at 9:21
  • @Colyn1337 There are no errors whatsoever that I can see. I even checked the event logs on the server and there are no issues. :( – jakejgordon Oct 6 '16 at 18:58
  • You have to trap them. – Colyn1337 Oct 7 '16 at 4:51

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.