SOLVED: Get the "connect as" credentials for an IIS web application using the "WebAdministration" PowerShell cmdlets Get-WebApplication

When using the WebAdministration PowerShell cmdlets Get-WebVirtualDirectory the username, password and other virtual directory information as expected.

However when using the Get-WebApplication cmdlet these properties aren't available even though a web application is a virtual directory.

If you look at the applicationHost.config file you'll see that the application in fact has a virtual directory (with path "/") associated with it. 

This virtual directory has the additional information required.

<application path="/TestApp" applicationPool="TestSite">
   <virtualDirectory path="/" physicalPath="C:\TestApp" userName="administrator" password="[enc:AesProvider:B2sdfsd1213E98dV53cb5NfdZndNWvCUf/AekXE=:enc]" />
</application>


To access this information get the "/" virtual directory from the collection property.

$app = Get-WebApplication "TestApp";
$app.Collection[0]|SELECT *;


I was concerned that the "/" virtual directory won't always be the first on in the collection (I can't confirm this) so added the following function to ensure that the correct one is returned.

# Gets the virtual directory associated with the specified application.
Function Get-ApplicationVirtualDirectory
{

    [CmdletBinding()]
    param( 

        [Parameter()]
            [System.Object] $Application

 

    )
    process
    {
        foreach ($virtualDirectory in $application.Collection)
        {
            if ($virtualDirectory.Path -eq "/") { return $virtualDirectory; }
        }
        return $null;

    }

}



While you're here why not check out our IIS Server audit and documentation tool?



Comments

Popular posts from this blog

SOLVED: Exchange Online Management PowerShell Connect-ExchangeOnline bug "A window handle must be configured. See https://aka.ms/msal-net-wam#parent-window-handles"

System error 86 has occurred. The specified network password is not correct.

The security identifier of the "NT SERVICE\WdiServiceHost" account is "S-1-5-80-3139157870-2983391045-3678747466-658725712-1809340420"