2

我正在尝试构建 powershell 程序,该程序将:

  1. 连接到远程服务器
  2. 显示活动服务器上活动 IIS 应用程序池的数量
  3. 根据选择(1,2,3,4,....n 等),它将重置应用程序池

你能给我一些建议吗?

4

3 回答 3

4

试试这个:

[Reflection.Assembly]::LoadWithPartialName('Microsoft.Web.Administration')
$sm = [Microsoft.Web.Administration.ServerManager]::OpenRemote('server1')
$sm.ApplicationPools['AppPoolName'].Recycle()
于 2013-05-06T12:29:38.450 回答
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 
}
于 2013-05-07T12:47:02.237 回答
0

我对现有代码无能为力,但其中一些链接

  1. 在此处查看远程 powershell 会话

  2. 查看Windows PowerShell 中的 Web 服务器 (IIS) 管理 Cmdlet,特别是Get-WebApplicationGet-WebAppPoolState

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

于 2013-05-06T12:14:04.347 回答