简体   繁体   中英

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

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?

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

如果你想要漂亮,你可以使用它:

Invoke-Command -ComputerName RemoteHostName -ScriptBlock { Get-WebAppPoolState | % {  return  @{($_.itemxpath -split ("'"))[1]="$($_.value)" } }}

对于旧版本的 IIS(版本 8 之前):

$items = get-wmiobject -namespace 'root\MicrosoftIISv2' -computername $server -class 'IIsApplicationPoolSetting'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM