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;

    }

}


Comments

Popular posts from this blog

Windows Server 2016, 2019, 2022, Windows 10 and Windows 11: Date and time "Some settings are managed by your organization".

TFTPD32 or TFTPD64 reports Bind error 10013 An attempt was made to access a socket in a way forbidden by its access permissions.

Enable function lock for F1-F12 on HP ZBook mobile workstations