我希望能够在PowerShell中复制此adsutil.vbs行为:
cscript adsutil.vbs set W3SVC/$(ProjectWebSiteIdentifier)/MimeMap
".pdf,application/pdf"我已经获得了website对象:
$website = gwmi -namespace "root\MicrosoftIISv2" -class "IISWebServerSetting"
-filter "ServerComment like '%$name%'"
if (!($website -eq $NULL)) {
#add some mimetype
}并列出MimeMap集合:
([adsi]"IIS://localhost/MimeMap").MimeMap谁知道如何填充空格,以便我可以向现有的IIS6网站添加mimetype?
发布于 2010-06-03 22:07:40
好吧,在经历了许多挫折和研究之后,这就是我想出的解决方案……
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"发布于 2012-02-17 12:18:59
我也有同样的问题。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)
}https://stackoverflow.com/questions/2957994
复制相似问题