IIS7.5 PowerShell preloadEnabled

har*_*ang 16 powershell iis-7.5

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

小智 27

这应该可以解决问题.您可以使用它get-itemproperty来验证它是否有效.我花了一段时间才弄清楚preloadEnabled在powershell中找到了哪些内容,但是如果你将网站路径管道化get-member,那么你可以从那里开始工作.

import-module webadministration
set-itemproperty IIS:\Sites\SiteName -name applicationDefaults.preloadEnabled -value True
Run Code Online (Sandbox Code Playgroud)

  • 该脚本似乎不会影响与IIS管理器中操作的属性相同的属性。 (2认同)

小智 7

实际上,有一种方法可以做到这一点(假设您要在/上设置一个应用程序,并且知道站点的名称):

[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()
Run Code Online (Sandbox Code Playgroud)

  • 这是设置特定网站的“默认”应用程序(path =“ /”)的preloadEnabled属性时对我唯一有效的方法。Set-ItemProperty似乎无法为根应用程序设置preloadEnabled属性。我已经在两个平台上成功进行了测试:Windows 7 SP1 64位带IIS 7.5和已安装的应用程序初始化模块以及Windows Server 2012 R2 64位带IIS 8.5。两种环境都为各自的平台安装了Windows Management Framework 5.0 RTM(即Powershell 5.0)。 (2认同)

kir*_*abk 5

这有点晚了,但这会对其他人有所帮助......这对我有用,并且不那么冗长.关键的区别是我删除了ApplicationDefaults因为我正在设置应用程序,而不是默认设置:

Set-ItemProperty IIS:\Sites\<siteName>\<applicationName> -name preloadEnabled -value True
Run Code Online (Sandbox Code Playgroud)

WHERE:可能等于默认网站可能等于MyApplication


che*_*oli -1

我也一直在寻找这个,但在 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
Run Code Online (Sandbox Code Playgroud)