在Power Shell脚本中获取IIS的应用程序池标识

时间:2016-08-11 13:47:52

标签: powershell iis

尝试在IIS中获取特定名称的应用程序池标识,例如: Test

成功通过下面的代码获取它但不想遍历所有的webapps,有没有通过指定名称来获取它?

Import-Module WebAdministration
Get-WebApplication

$webapps = Get-WebApplication
$list = @()
foreach ($webapp in get-childitem IIS:\AppPools\)
{
    $name = "IIS:\AppPools\" + $webapp.name
    $item = @{}
    if ($webapp.name -eq 'Test')
    {        
        $item = $webapp.processModel.identityType   
        break
    }
}


echo $webapp.processModel.identityType

4 个答案:

答案 0 :(得分:10)

并非严格意味着OP想要的东西,但谷歌引导我在这里查询所有App Pool Identities。这是我的版本

Import-Module WebAdministration;Get-ChildItem -Path IIS:\AppPools\ | 
Select-Object name, state, managedRuntimeVersion, managedPipelineMode, @{e={$_.processModel.username};l="username"}, <#@{e={$_.processModel.password};l="password"}, #> @{e={$_.processModel.identityType};l="identityType"} |
format-table -AutoSize

答案 1 :(得分:6)

只需合并路径并检索项目即可。这将有效:

$item = Get-Item (Join-Path 'IIS:\AppPools\' 'Test') | 
   select -ExpandProperty processModel | 
   select -expand identityType

答案 2 :(得分:3)

需要这个,但是所有答案都没有完整的代码,因此我将一些内容放在一起。

Try {
    Import-Module WebAdministration -ErrorAction Stop
} Catch {
    Write-Error -Message "Unable to load required module."
}

$webapps = Get-ChildItem –Path IIS:\AppPools
$list = [System.Collections.ArrayList]::new()
foreach ($webapp in $webapps) {
    $Pool = "IIS:\AppPools\" + $webapp.name
    $sid = New-Object System.Security.Principal.SecurityIdentifier (
        Get-Item $Pool | select -ExpandProperty applicationPoolSid
    )
    [void]$List.add([PSCustomObject]@{
        Name = $webapp.name
        Pool = $Pool
        ServiceAccount = $sid.Translate([System.Security.Principal.NTAccount])
    })
}
$list

输出

Name              Pool                            ServiceAccount               
----              ----                            --------------               
.NET v4.5         IIS:\AppPools\.NET v4.5         IIS APPPOOL\.NET v4.5        
.NET v4.5 Classic IIS:\AppPools\.NET v4.5 Classic IIS APPPOOL\.NET v4.5 Classic
DefaultAppPool    IIS:\AppPools\DefaultAppPool    IIS APPPOOL\DefaultAppPool   
WsusPool          IIS:\AppPools\WsusPool          IIS APPPOOL\WsusPool         

答案 3 :(得分:0)

我知道这很老,但这是我最终使用的,我认为适合所有人吗?

假设$ Name包含应用程序池的名称:

$sid = New-Object System.Security.Principal.SecurityIdentifier (
    Get-Item IIS:\AppPools\$Name | select -ExpandProperty applicationPoolSid
)

$identity = $sid.Translate([System.Security.Principal.NTAccount])

$identity.Value 

IIS APPPOOL\MyAppPool