2

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?

SteveC
  • 15,808
  • 23
  • 102
  • 173
mwjackson
  • 5,403
  • 10
  • 53
  • 58

2 Answers2

2

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: Can I setup an IIS MIME type in .NET?

Community
  • 1
  • 1
mwjackson
  • 5,403
  • 10
  • 53
  • 58
0

I had this same problem. An alternative to the Interop.IISOle.dll is to use InvokeMember to set the COM bindings.

$adsiPrefix = "IIS://$serverName"
$iisPath = "W3SVC"
$iisADSI = [ADSI]"$adsiPrefix/$iisPath"

$site = $iisADSI.Create("IISWebServer", $script:webSiteNumber)

$xapMimeType = New-Object -comObject MimeMap
    SetCOMProperty $xapMimeType "Extension" ".xap"
    SetCOMProperty $xapMimeType "MimeType" "application/x-silverlight-app"
    $site.Properties["MimeMap"].Add($xapMimeType)

    $site.SetInfo()
    $site.CommitChanges()

function SetCOMProperty($target, $member, $value) {
    $target.psbase.GetType().InvokeMember($member, [System.Reflection.BindingFlags]::SetProperty, $null, $target, $value)        
}
e82.eric
  • 313
  • 4
  • 11