views:

194

answers:

1

I want to be able to replicate this adsutil.vbs behaviour in powershell:

cscript adsutil.vbs set W3SVC/$(ProjectWebSiteIdentifier)/MimeMap ".pdf,application/pdf"

and I've gotten as far as getting the website object:

$website  = gwmi -namespace "root\MicrosoftIISv2" -class "IISWebServerSetting" -filter "ServerComment like '%$name%'"
if (!($website -eq $NULL)) { 
    #add some mimetype
}

and listing out the MimeMap collection:

([adsi]"IIS://localhost/MimeMap").MimeMap

Anyone know how to fill in the blanks so that I can add mimetypes to an exiting IIS6 website?

A: 

Ok well after much frustration and research, this is the solution I've come up with...

a) Grab the COM DLL "Interop.IISOle.dll" and put it somewhere easily referenced (eg. reference the COM component "Active DS IIS Namespace Provider" in a dummy project, build and grab the DLL from the bin folder)

b)

function AddMimeType ([string] $websiteId, [string] $extension, [string] $application)
{
    [Reflection.Assembly]::LoadFile("yourpath\Interop.IISOle.dll") | Out-Null;
    $directoryEntry = New-Object System.DirectoryServices.DirectoryEntry("IIS://localhost/W3SVC/$websiteId/root");
    Try {
        $mimeMap = $directoryEntry.Properties["MimeMap"]

        $mimeType = New-Object "IISOle.MimeMapClass";
        $mimeType.Extension = $extension
        $mimeType.MimeType = $application

        $mimeMap.Add($mimeType)
        $directoryEntry.CommitChanges()
    } Finally {
        if ($directoryEntry -ne $null) {
            if ($directoryEntry.psbase -eq $null) {
                $directoryEntry.Dispose()
            } else {
                $directoryEntry.psbase.Dispose()
            }
        }
    }
}

c) Sample usage:

AddMimeType "123456" ".pdf" "application/pdf"

References: http://stackoverflow.com/questions/234526/can-i-setup-an-iis-mime-type-in-net

jacko