如何用IISAdministration模块获取applicationHost.config的LogFile配置?
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.
Recommended Methods to Access logFormat
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

