如何使用Powershell在远程服务器上运行IIS应用程序池列表?

时间:2013-05-06 10:52:01

标签: iis powershell pool

我正在尝试构建PowerShell程序:

  1. 连接到远程服务器
  2. 显示活动服务器上活动IIS应用程序池的数量
  3. 根据选择(1,2,3,4,...... n等),它会重置app pool
  4. 你能给我一些提示吗?

3 个答案:

答案 0 :(得分:4)

尝试一下:

[Reflection.Assembly]::LoadWithPartialName('Microsoft.Web.Administration')
$sm = [Microsoft.Web.Administration.ServerManager]::OpenRemote('server1')
$sm.ApplicationPools['AppPoolName'].Recycle()

答案 1 :(得分:3)

根据已经给出的答案,尝试以下方法。它使用PowerShell远程处理,特别是Invoke-Command,因此您需要熟悉它。

[cmdletBinding(SupportsShouldProcess=$true,ConfirmImpact="High")] 
param
(
    [parameter(Mandatory=$true,ValueFromPipeline=$true)] 
    [string]$ComputerName,

    [parameter(Mandatory=$false)] 
    [System.Management.Automation.PSCredential]$Credential
)
begin
{
    if (!($Credential))
    {
        # Prompt for credentials if not passed in
        $Credential = get-credential
    }

    $scriptBlock = {

        Import-Module WebAdministration

        # Get all running app pools
        $applicationPools = Get-ChildItem IIS:\AppPools | ? {$_.state -eq "Started"}
        $i = 0

        # Display a basic menu
        Write-Host "`nApplication Pools`n"
        $applicationPools | % {
            "[{0}]`t{1}" -f $i, $($applicationPools[$i].Name)
            $i++
        }

        # Get their choice
        $response = Read-Host -Prompt "`nSelect Application Pool to recycle"

        # Grab the associated object, which will be null 
        # if an out of range choice was entered
        $appPool = $applicationPools[$response]

        if ($appPool)
        {
            "Recycling '{0}'" -f $appPool.name
            $appPool.recycle()
        }
    }
}
process
{
    Invoke-Command -ComputerName $computerName -Credential $credential -ScriptBlock $scriptBlock 
}

答案 2 :(得分:0)

我对现有代码无法提供帮助,但有哪些链接

  1. 查看远程PowerShell会话here

  2. 查看网络Server (IIS) Administration Cmdlets in Windows PowerShell,特别是Get-WebApplicationGet-WebAppPoolState

  3. 如果重置意味着停止,那么您可以查看Stop-WebAppPool