【问题标题】:Geting IIS applicationHost.config LogFile settings using IISAdministraton PowerShell module使用 IISAdministration PowerShell 模块获取 IIS applicationHost.config 日志文件设置
【发布时间】:2019-11-20 03:41:45
【问题描述】:

我一直在尝试通过获取和修改applicationHost.config 文件中的基本设置来自学IISAdministration module。我正在努力理解如何使用WebAdministration module 来执行与下面的示例等效的 IISAdministration。我目前仍在阅读this great article,了解 IIS 配置的工作原理,但我只是不明白。

工作 WebAdministration 示例:

#Gets the Log File format set in the applicationHost.config file
Get-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -Filter "system.applicationHost/sites/siteDefaults/logFile" -Name "logFormat"

这是我使用 IISAdministraton 命令想到的,但我收到“警告:配置集合元素不存在”。我试图设置的设置确实存在,所以我一定做错了什么。任何指导或解释将不胜感激

$section = Get-IISConfigSection -SectionPath "system.applicationHost/sites"

Get-IISConfigCollection $section | Get-IISConfigCollectionElement -ConfigAttribute @{"Name"="logFormat"}

更新:

我找到了一种使用 IISAdministration 访问 logFormat 属性的方法。我真诚地希望这不是访问配置文件中嵌套属性的自然方式,而我恰好偶然发现了在此模块中实现我的目标的众多方法之一。我仍在使用这些命令并阅读有关它们的更多信息。

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

更新:

我发现使用 IISAdministration 访问 logFormat 属性的另一种方法,但我仍然不太满意。

$manager = Get-IISServerManager
$config = $manager.GetApplicationHostConfiguration()
$section = $config.GetSection("system.applicationHost/sites")
$siteDefaults = $section.GetChildElement("siteDefaults")
$siteDefaults.GetChildElement("logFile")["logFormat"]

【问题讨论】:

  • 您应该假定 WebAdministration 已过时,并避免在您的任何脚本中使用它。
  • @LexLi 我强烈反对任何人都认为它已经过时。它完成了与 IISAdmin 完全相同的工作,但没有那么优雅。我承认返回类型在使用 IISAdmin 返回时更有用,这也是我学习 IISAdministration 的部分原因。这是一篇关于主题octopus.com/blog/iis-powershell 的精彩文章。最新版本的 Windows Server 仍支持 WebAdmin
  • 支持很多东西,比如经典的 ASP,但是当新设计明显取代时,为什么还要继续使用它们。
  • 我听到你的声音并且在大多数情况下会同意,但说该模块已过时实际上是错误的。

标签: powershell iis windows-server-2012-r2


【解决方案1】:

在为 WebAdministration 内存泄漏问题苦苦挣扎了一段时间后,我目前正致力于在多个 DSC 模块中用相应的 IISAdministration cmdlet 替换 WebAdministration cmdlet。我仍在寻找完成每项任务的最有效方法,并偶然发现了这个线程。我想提供另一种选择:

$sm = Get-IISServerManager
$sm.SiteDefaults.LogFile.LogFormat

【讨论】:

  • 太棒了!您的评论促使我找到此页面:docs.microsoft.com/en-us/iis/configuration/… - 我的目标是“检查所有复选框”(选择所有字段)并完成它:$sm.Sites[$siteName].LogFile.LogExtFileFlags = "Date,时间、ClientIP、UserName、SiteName、ComputerName、ServerIP、Method、UriStem、UriQuery、BytesSent、BytesRecv、TimeTaken、ProtocolVersion、Host、HttpStatus、Win32Status、ServerPort、UserAgent、HttpSubStatus、Cookie、Referer"
【解决方案2】:

HiTech/ 再次查看后,我删除了我之前的答案。

正如你所发现的。您不能像使用 WebAdmin 那样使用 IISAdmin 直接访问元素。

您已经导航,直到您所追求的内容被公开为属性或值。这实际上是示例 1 中的 detailed in the cmdlet help of Get-IISConfigElement

修改为显示每个部分的输出并获取日志文件格式。

# Gets the Log File format set in the applicationHost.config file
Get-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -Filter "system.applicationHost/sites/siteDefaults/logFile" -Name "logFormat"
# W3C


# Attempt at using IISADministration to get the same information

($ConfigSection = Get-IISConfigSection -SectionPath "system.applicationHost/sites")
<#
...
ChildElements         : {siteDefaults, applicationDefaults, virtualDirectoryDefaults}
ElementTagName        : system.applicationHost/sites
...
#>
($SitesCollection = Get-IISConfigCollection -ConfigElement $ConfigSection)
<#
Attributes      : {name, id, serverAutoStart, state}
ChildElements   : {bindings, limits, logFile, traceFailedRequestsLogging...}
...
RawAttributes   : {[name, Default Web Site], [id, 1], [serverAutoStart, True], [state, 1]}
...
#>
($Site = Get-IISConfigCollectionElement -ConfigCollection $SitesCollection -ConfigAttribute @{"name" = "Default Web Site"})
<#
Attributes      : {name, id, serverAutoStart, state}
ChildElements   : {bindings, limits, logFile, traceFailedRequestsLogging...}
...}
RawAttributes   : {[name, Default Web Site], [id, 1], [serverAutoStart, True], [state, 1]}
...
#>
($Elem = Get-IISConfigElement -ConfigElement $Site -ChildElementName "logfile")
<#
Attributes      : {logExtFileFlags, customLogPluginClsid, logFormat, logTargetW3C...}
ChildElements   : {customFields}
ElementTagName  : logFile
...
RawAttributes   : {[logExtFileFlags, 2478031], [customLogPluginClsid, ], [logFormat, 2], [logTargetW3C, 1]...}
...
#>
($LogDetails = Get-IISConfigAttributeValue -ConfigElement $Elem -AttributeName "logformat")

# W3C

所以你最终得到了这个,没有像 WebAdmin 构造那样简洁......

Get-IISConfigSection -SectionPath "system.applicationHost/sites" | 
%{Get-IISConfigCollection -ConfigElement $PSItem} | 
%{Get-IISConfigCollectionElement -ConfigCollection $PSItem -ConfigAttribute @{"name" = "Default Web Site"}} | 
%{Get-IISConfigElement -ConfigElement $PSItem -ChildElementName "logfile"} | 
%{Get-IISConfigAttributeValue -ConfigElement $PSItem -AttributeName "logformat"}

# Results 

W3C

是的,如果没有循环,上述内容不会给出目标结果。

您的第一次更新,虽然您对它不满意,但它是迄今为止最简洁的。然而,即使我们都尝试过。这些配置只是 XML 文件,因此只需使用 XML cmdlet 或 .Net XML 命名空间即可。

【讨论】:

  • 感谢您的跟进。可悲的是,与您类似,我的研究和测试并没有比我的更新中的更好。随着我继续学习该模块,我可以预见将来需要编写混合 WebAdmin 和 IISAdmin 的脚本。我同意您使用 vanilla XML cmdlet 的建议。它们非常适合简单地检索信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-03
  • 1970-01-01
  • 2011-06-05
  • 1970-01-01
  • 2012-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多