Search code examples
powershelliispowershell-core

Can i get the "Alias" of an application from an IIS Site with Powershell?


I am trying to grab the information shown in IIS when you run the UI, namely the list of "Applications" running under a site. I'm aware that the UI refers to this as the "Alias".

UI of IIS showing Alias property

This value is not the same as "Name", nor is it the same as "host name".

I've tried the following:

$sites = Get-ChildItem "IIS:\Sites\Default Web Site" | Where-Object { $_.NodeType -eq "Application" }
foreach ($site in $sites)
{
    $alias = ($webSite.Name.Split("\"))[-1]
}

This returns the wrong values (for example, it may return api/mike rather than "Mike") Since the Alias is not necessarily the hostname minus a slash, this standard and often referred to answer does not work.

I've also tried:

import-module webadministration

$apppools = (Get-IISAppPool).Name

foreach($appPool in $appPools) {

    $apps = (Get-WebConfigurationProperty "/system.applicationHost/sites/site/application[`@applicationPool='$appPool']" "machine/webroot/apphost" -name path).ItemXPath 

    foreach ($s in $apps) {
        $name = $s -replace "\/system.applicationHost\/sites\/site\[\@name='", ""
        $name = $name -replace "' and \@id='\d{1,10}'\]\/application\[\@path='", ""
        $name = $name -replace "'\]",""
        $name = $name -replace "Default Web Site/", ""
        $out += $name + "`n"
    }

    $out
}

This also provides the "api/Mike" value, rather than "Mike".

I've then tried, without any success:

$Binding = (Get-ItemProperty -Path IIS:\Sites\Default Web Site -Name Bindings).Collection.bindingInformation
$HostName = ($Binding -split ":")[-1]

and the often quoted answer of:

$app = get-webapplication
foreach($a in $app)
{
$a.Attributes[0].Value;
}

Again, these all produce the wrong value. (and "api/" is not the only prefix, sometimes you have "Default Web Site/api/..." among others)

Is there a way to get the list as shown in the screenshot?


Solution


  • Get-IISSite outputs Microsoft.Web.Administration.Site instances that have an .Application collection whose elements are Microsoft.Web.Administration.Application instances, whose .Path property seems to correspond what is shown as Alias in the IIS administration GUI, but prefixed with /, with the default application of a site always having path /; for nested applications, the alias is the last /-separated token.

    Therefore, the following should list all aliases for the Default Web Site site's non-default top-level applications:

    @((Get-IISSite 'Default Web Site').Applications.Path) -replace '.*/' -ne ''
    
    • -replace '.*/' trims everything up to and including the last (or only) / character from the .Path property values.

      • Note that .Path is applied to the .Applications collection, yet it is the .Path property values of its elements that are returned, courtesy of PowerShell's member-access enumeration feature.
    • -ne '' filters out the top-level default application (whose path is /)

    • The @(...) enclosure ensures that the returned paths are treated as an array even if only one path happens to be returned (/), which in turn ensures that -replace outputs an array so that the -ne operation acts as filter to weed out empty-string elements (in effect then returning an empty array); without it, -ne would return a Boolean, i.e. $false.