You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用IISAdministration模块获取applicationHost.config的LogFile配置?

Working with IISAdministration to Access applicationHost.config Settings

Hey there! Let's break down how to properly use the IISAdministration module to access and modify settings like logFormat in applicationHost.config, since you're transitioning from the WebAdministration module.

Why Your Initial Command Failed

First, let's clear up why you got that "Config collection element does not exist" warning. Your original command tried to pull the sites collection and then look for an element with a Name attribute of logFormat—but that's not how the config structure works. The sites section contains a collection of site elements, while siteDefaults is a child element of the sites section itself, not part of the collection. So your approach was targeting the wrong part of the config hierarchy.

Both methods you found are valid—they just align with different ways of working with the IISAdministration module. Let's break them down:

Method 1: Chained Cmdlets (Clean, Step-by-Step)

This is the more PowerShell-idiomatic approach, using the module's built-in cmdlets to navigate the config hierarchy one level at a time. It's great for readability when you need to trace exactly where you're accessing settings:

Get-IISConfigSection -SectionPath "system.applicationHost/sites" | 
  Get-IISConfigElement -ChildElementName "siteDefaults" | 
  Get-IISConfigElement -ChildElementName "logFile" | 
  Get-IISConfigAttributeValue -AttributeName "logFormat"

To modify the value, just swap Get-IISConfigAttributeValue with Set-IISConfigAttributeValue:

Get-IISConfigSection -SectionPath "system.applicationHost/sites" | 
  Get-IISConfigElement -ChildElementName "siteDefaults" | 
  Get-IISConfigElement -ChildElementName "logFile" | 
  Set-IISConfigAttributeValue -AttributeName "logFormat" -AttributeValue "W3C"

Method 2: ServerManager API (Flexible for Complex Operations)

This approach uses the underlying .NET-based IIS ServerManager API, which is ideal if you need to perform multiple operations on the same config sections (since you can reuse the objects instead of re-querying each time):

# Get the server manager instance
$manager = Get-IISServerManager
# Access the applicationHost.config
$config = $manager.GetApplicationHostConfiguration()
# Navigate to the sites section
$section = $config.GetSection("system.applicationHost/sites")
# Get the siteDefaults child element
$siteDefaults = $section.GetChildElement("siteDefaults")
# Access the logFormat attribute
$logFormat = $siteDefaults.GetChildElement("logFile")["logFormat"]

# To modify and save changes
$siteDefaults.GetChildElement("logFile")["logFormat"] = "IIS"
$manager.CommitChanges() # Don't forget this to save to the config file!

Key Difference from WebAdministration

The WebAdministration module uses a declarative approach with PSPath and Filter strings to target settings directly. IISAdministration, by contrast, is object-oriented—you navigate the actual hierarchy of config sections, elements, and attributes, which maps more closely to how IIS stores its configuration under the hood.

Which Method Should You Use?

  • Use the chained cmdlet approach for simple, one-off operations where readability matters.
  • Use the ServerManager API when you need to perform multiple actions (like modifying multiple attributes or sites) to avoid redundant queries.

Both are officially supported and "correct" ways to work with the module—pick the one that fits your use case!

内容的提问来源于stack exchange,提问作者HiTech

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:21:15