【问题标题】:Get App Pool Identity for IIS in Power Shell Script在 Powershell 脚本中获取 IIS 的应用程序池标识
【发布时间】:2016-08-11 13:47:52
【问题描述】:

尝试在 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

【问题讨论】:

    标签: powershell iis


    【解决方案1】:

    严格来说不是 OP 想要的,但谷歌让我在这里查询所有应用程序池标识。这是我的版本

    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
    

    【讨论】:

      【解决方案2】:

      只需组合路径并检索项目。这将起作用:

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

      【讨论】:

      • 在下面使用,但不起作用: $webapp = "Test" $item = Get-Item (Join-Path 'IIS:\AppPools\$webapp') |选择-ExpandProperty processModel |选择 -expand 身份类型
      • 是的,因为你必须使用Get-Item (Join-Path 'IIS:\AppPools' $webapp) 而不是Get-Item (Join-Path 'IIS:\AppPools\$webapp')...
      • 像魅力一样工作!
      • @MartinlBarandl 如果身份是 NetworkService,则代码可以正常工作,但如果身份是特定用户,则代码可以正常工作,例如: Deploy\ABC 然后而不是获取身份为 Deploy\ABC,我得到作为特定用户的输出。请提出建议。
      • 在这种情况下,您必须检索 processModel 对象的用户名和密码属性。
      【解决方案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         
      

      【讨论】:

        【解决方案4】:

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

        假设 $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
        
        

        【讨论】:

          猜你喜欢
          • 2015-03-30
          • 2015-04-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-08
          • 1970-01-01
          • 2012-07-03
          相关资源
          最近更新 更多