我是否可以使用 PowerShell 命令(例如 New-WebSite)来创建网站并设置网站的 preloadEnabled="true"?
7 回答
这应该可以解决问题。您可以使用get-itemproperty
来验证它是否有效。我花了一段时间才弄清楚preloadEnabled
在 powershell 中的位置,但是如果您将站点路径通过管道传输到get-member
,那么您可以从那里开始工作。
import-module webadministration
set-itemproperty IIS:\Sites\SiteName -name applicationDefaults.preloadEnabled -value True
这有点晚了,但这会帮助其他人......这对我有用,而且不那么冗长。主要区别在于我删除了 ApplicationDefaults 因为我正在设置应用程序,而不是此处的默认值:
Set-ItemProperty IIS:\Sites\<siteName>\<applicationName> -name preloadEnabled -value True
WHERE: 'SiteName' 可能等于 Default Web Site 'ApplicationName' 可能等于 MyApplication
实际上有一种方法可以做到这一点(假设您在 / 有一个要为其设置的应用程序并且您知道您的站点名称):
[System.Reflection.Assembly]::LoadFrom("C:\windows\system32\inetsrv\Microsoft.Web.Administration.dll")
$serverManager = (New-Object Microsoft.Web.Administration.ServerManager)
$serverManager.Sites["YOUR_SITE_NAME"].Applications["/"].SetAttributeValue("preloadEnabled", $true)
$serverManager.CommitChanges()
您可以为网站的根应用程序启用预加载,如下所示:
$w = New-Item "IIS:\Sites\AAA" -type site –physicalPath "C:\W" -bindings $binding
$w.Collection[0].preloadEnabled = $true
$w | Set-Item
我可以提供更细粒度和确定性的方法:
# Creating the website:
$website = New-WebSite -Name $WebSiteName -PhysicalPath $WebSiteFolder -ApplicationPool $PoolName
# Setting preload enabled:
Set-WebConfigurationProperty `
-PSPath "IIS:\" `
-Filter "/system.applicationHost/sites/site[@name='$WebSiteName']/application[@path='/']" `
-Name "preloadEnabled" `
-Value $True # or $False to turn that off
我也一直在寻找这个,但在 WebAdministration 中找不到任何东西来设置这个选项。据推测,该方法是在正确的 WebApplication 上调用 New-ItemProperty。不幸的是,我无法获得给定网站的“默认”应用程序,或在其上设置此属性。看起来 WebAdministration 模块(它启用像 New-WebSite 这样的 cmdlet)是在考虑早期版本的 IIS 的情况下编写的,而且肯定是在应用程序初始化模块之前编写的。
这是一种解决方法,它通过编辑底层 applicationHost.config 文件来强制设置这些属性。这是我们现在使用的脚本的一个稍微简化的版本。您需要以管理员身份运行此脚本。
# Copy applicationHost.config to the temp directory,
# Edit the file using xml parsing,
# copy the file back, updating the original
$file = "applicationhost.config"
$source = Join-Path "$env:windir" "\system32\inetsrv\config\$file"
$temp = Join-Path "$env:temp" "$([Guid]::NewGuid().ToString())"
$tempFile = Join-Path "$temp" "$file"
#update all applications in websites whose name matches this search term
$search = "website name to search for"
#copy applicationHost.config to temp directory for edits
#assignments to $null simply silence output
$null = New-Item -itemType Directory -path $temp
$null = Copy-Item "$source" "$temp"
# Load the config file for edits
[Xml]$xml = Get-Content $tempFile
# find sites matching the $search string, enable preload on all applications therein
$applications = $xml.SelectNodes("//sites/site[contains(@name, `"$search`")]/application")
$applications | % {
$_.SetAttribute("preloadEnabled", "true")
}
#save the updated xml
$xml.Save("$tempFile.warmed")
#overwrite the source with updated xml
Copy-Item "$tempfile.warmed" "$source"
#cleanup temp directory
Remove-Item -recurse $temp
老问题,但我想分享我的见解。
我需要从 Octopus 部署后脚本执行此操作,其中我的应用程序位于站点根目录中。我在这里尝试了所有建议的解决方案,而 Robert Moore 的解决方案是唯一适合我的解决方案。
但是,我最终使用了它,它也完成了这项工作:
$Site = Get-Item IIS:\Sites\<site name>
$Site.applicationDefaults.preloadEnabled = $true
$Site | Set-Item -Verbose
警告:如果您的站点中有任何应用程序,请不要这样做(请参阅@Sergey Nudnov 的评论