【问题标题】:How to use POWERSHELL to set MimeTypes in an IIS6 website?如何使用 POWERSHELL 在 IIS6 网站中设置 MimeTypes?
【发布时间】:2010-06-02 13:17:42
【问题描述】:

我希望能够在 PowerShell 中复制此 adsutil.vbs 行为:

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

我已经获得了网站对象:

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

并列出 MimeMap 集合:

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

任何人都知道如何填写空白,以便我可以将 mimetypes 添加到现有的 IIS6 网站?

【问题讨论】:

    标签: powershell iis-6 wmi


    【解决方案1】:

    好吧,经过多次挫折和研究,​​这就是我想出的解决方案......

    a) 获取 COM DLL "Interop.IISOle.dll" 并将其放在易于引用的位置 (例如,在虚拟项目中引用 COM 组件“Active DS IIS Namespace Provider”,构建并从 bin 文件夹中获取 DLL)

    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) 示例用法:

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

    参考:Can I setup an IIS MIME type in .NET?

    【讨论】:

    • 哈!...您刚刚为我节省了将我的答案中的代码转换为 PS 的工作 :)
    【解决方案2】:

    我遇到了同样的问题。 Interop.IISOle.dll 的替代方法是使用 InvokeMember 设置 COM 绑定。

    $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)        
    }
    

    【讨论】:

      猜你喜欢
      • 2010-09-30
      • 1970-01-01
      • 2010-09-16
      • 1970-01-01
      • 2012-12-23
      • 1970-01-01
      • 2010-09-24
      • 1970-01-01
      相关资源
      最近更新 更多