Powershell – How to remotely check the status of a web application pool with PowerShell

application-pooliis-7powershell

I see a lot of scripts for recycling application pools on a web server running IIS7 but is there a way to check, with PowerShell, that the web application pool is running or stopped? I can't seem to figure out a way to remotely have Get-WebAppPoolState return the status on the Application Pool, and my Google-fu has not been able to come up with a replacement. I can remotely get gwmi to work and recycle or start my app pools but ideally I only want to have to run this if the app pool is actually stopped.

Would I need to work this out with PSExec or is there an alternative I can use similar to gwmi and have a one line command to call the app pool on the IIS7 server and give me the status?

Best Answer

You can use Invoke-Command to invoke the Get-WebAppPoolState cmdlet on the remote machine.

$appPoolStatus = Invoke-Command -ComputerName RemoteHostName {Import-Module WebAdministration; Get-WebAppPoolState DefaultAppPool}
$appPoolStatus.Value

Note that if you are going to use variables defined locally on the calling machine, you will have to treat them according to the rules. This link is an excellent post explaining them:

http://blogs.msdn.com/b/powershell/archive/2009/12/29/arguments-for-remote-commands.aspx

Example:

$appPoolName = "SomeAppPoolName"
$appPoolStatus = Invoke-Command -ComputerName RemoteHostName { param($apn) Import-Module WebAdministration; Get-WebAppPoolState $apn} -Args $appPoolName