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?
The Get-IISAppPool cmdlet gets information about application pools and their current status and other key information. If a specific application pool or a comma delimited list of application pools are requested, only those whose names are passed as an argument are returned.
To stop an IIS website, you will use the Stop-IISSite cmdlet.
You can use this if you want pretty out:
Invoke-Command -ComputerName RemoteHostName -ScriptBlock { Get-WebAppPoolState | % { return @{($_.itemxpath -split ("'"))[1]="$($_.value)" } }}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With