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

為{幸葍}努か 提交于 2019-12-12 07:46:34

问题


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?


回答1:


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



回答2:


You can use this if you want pretty out:

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


来源:https://stackoverflow.com/questions/12629593/how-to-remotely-check-the-status-of-a-web-application-pool-with-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!