gpt4 book ai didi

powershell - 如何使用 POWERSHELL 在 IIS6 网站中设置 MimeTypes?

转载 作者:行者123 更新时间:2023-12-02 23:49:10 24 4
gpt4 key购买 nike

我希望能够在 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 网站?

最佳答案

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

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) sample 用法:
AddMimeType "123456" ".pdf" "application/pdf"

引用文献: Can I setup an IIS MIME type in .NET?

关于powershell - 如何使用 POWERSHELL 在 IIS6 网站中设置 MimeTypes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2957994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com