Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get App Pool Identity for IIS in Power Shell Script

Tags:

powershell

iis

Trying to get Application Pool Identity in IIS for a specific name, for example : Test

Succeded in getting it via below code but dont want to loop through all the webapps, is there any easy of getting it by specifying the name?

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
like image 419
Joseph Avatar asked Aug 11 '16 13:08

Joseph


People also ask

How do I check IIS application pool status in PowerShell?

To get the IIS application pool names using PowerShell, you need to use the IIS PSDrive but for that, we need the IIS PowerShell module WebAdministration or IISAdministration on the server we are running the command. If the WebAdministration module is already installed, use the below command to import the module.


3 Answers

Not strictly what the OP wanted, but google led me here for querying all App Pool Identities. This is my version

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
like image 94
fiat Avatar answered Oct 16 '22 13:10

fiat


Needed this but none of the answers had complete code so I put something together.

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

Output

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         
like image 6
Nick W. Avatar answered Oct 16 '22 12:10

Nick W.


Just combine the path and retrieve the item. This will work:

$item = Get-Item (Join-Path 'IIS:\AppPools\' 'Test') | 
   select -ExpandProperty processModel | 
   select -expand identityType
like image 6
Martin Brandl Avatar answered Oct 16 '22 13:10

Martin Brandl