简体   繁体   English

IIS7.5 PowerShell 预加载已启用

[英]IIS7.5 PowerShell preloadEnabled

我是否可以使用 PowerShell 命令(例如,New-WebSite)来创建网站并设置网站的 preloadEnabled="true"?

This should do the trick.这应该可以解决问题。 You can use the get-itemproperty to verify that it worked.您可以使用get-itemproperty来验证它是否有效。 It took me a while to figure out where to find preloadEnabled within powershell but if you pipe the site path to get-member , then you can work your way from there.我花了一段时间才弄清楚在 powershell 中哪里可以找到preloadEnabled但如果您将站点路径通过管道传递给get-member ,那么您可以从那里开始工作。

import-module webadministration
set-itemproperty IIS:\Sites\SiteName -name applicationDefaults.preloadEnabled -value True

There is in fact a way to do this (assuming you have a single application at / that you want to set it for and you know the name of your site):实际上有一种方法可以做到这一点(假设您在 / 上有一个要为其设置的应用程序,并且您知道站点的名称):

[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()

This is a bit late, but this will help others... This worked for me and was a little less verbose.这有点晚了,但这会帮助其他人......这对我有用,而且不那么冗长。 The key difference is that I removed ApplicationDefaults because I am setting the application, not the defaults here:关键区别在于我删除了 ApplicationDefaults 因为我正在设置应用程序,而不是这里的默认值:

Set-ItemProperty IIS:\Sites\<siteName>\<applicationName> -name preloadEnabled -value True

WHERE: 'SiteName' might equal Default Web Site 'ApplicationName' might equal MyApplication WHERE:“SiteName”可能等于默认网站“ApplicationName”可能等于 MyApplication

You can enable preload for the root application of a website like this:您可以为网站的根应用程序启用预加载,如下所示:

$w = New-Item "IIS:\Sites\AAA" -type site –physicalPath "C:\W" -bindings $binding
$w.Collection[0].preloadEnabled = $true
$w | Set-Item

I could offer more granular and deterministic approach:我可以提供更细粒度和确定性的方法:

# 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

I've been looking for this too, but couldn't find anything in WebAdministration to set this option.我也一直在寻找这个,但在 WebAdministration 中找不到任何设置此选项的内容。 Presumably the approach would be to call New-ItemProperty on the correct WebApplication.据推测,该方法是在正确的 WebApplication 上调用 New-ItemProperty。 Unfortunately, I was unable to get the "default" application for a given website, or to set this property on it.不幸的是,我无法获得给定网站的“默认”应用程序,也无法在其上设置此属性。 It kinda seems like the WebAdministration module (which enables cmdlets like New-WebSite) was written with earlier versions of IIS in mind, and certainly before the Application Initialization module.似乎 WebAdministration 模块(它启用像 New-WebSite 这样的 cmdlet)是用早期版本的 IIS 编写的,当然在应用程序初始化模块之前。

This is a workaround, which forces the setting of these properties by editing the underlying applicationHost.config file.这是一种变通方法,它通过编辑基础 applicationHost.config 文件来强制设置这些属性。 This is a slightly simplified version of a script we're now using.这是我们现在使用的脚本的稍微简化版本。 You'll need to run this script as an administrator.您需要以管理员身份运行此脚本。

# 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

Old question, but I wanted to share my insights.老问题,但我想分享我的见解。

I needed to do this from an Octopus post-deployment script, where my App was in the Site root.我需要从 Octopus 部署后脚本执行此操作,我的应用程序位于站点根目录中。 I tried all the suggested solutions here and the one from Robert Moore was the only one working for me.我在这里尝试了所有建议的解决方案,而 Robert Moore 的解决方案是唯一对我有用的解决方案。

However, I ended up using this which also did the job:但是,我最终使用了它,它也完成了这项工作:

$Site = Get-Item IIS:\Sites\<site name>
$Site.applicationDefaults.preloadEnabled = $true
$Site | Set-Item -Verbose

Warning : Do not do this if you have any Apps in your Site (see comment from @Sergey Nudnov警告:如果您的站点中有任何应用程序,请不要这样做(请参阅@Sergey Nudnov 的评论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM