【问题标题】:How to add new fields to an existing IIS web site log using Set-WebConfigurationProperty如何使用 Set-WebConfigurationProperty 将新字段添加到现有 IIS 网站日志
【发布时间】:2020-08-29 03:14:00
【问题描述】:

https://www.interfacett.com/blogs/how-change-iis-log-contents-powershell/ 告诉我如何在服务级别设置默认文件字段,很有帮助:-) 但是我想在一个单独的命名网站级别设置实际的文件字段(添加 sc-bytes 和 cs-bytes)——因为你可以通过 IIS GUI 很容易地是的——但是能够设置和获取(到检查)对多个 IIS Web 和应用服务器使用可重复的脚本 - 是的,我真的很想使用 POSH 脚本对 IIS 服务进行编目...

所以这很好用:-

Invoke-Command –Session $session {
Set-WebConfigurationProperty -Filter System.Applicationhost/Sites/SiteDefaults/logfile -Name LogExtFileFlags -Value "Date,Time,ClientIP"}

....而且我可以 - 感谢 Stackoverflow 提示 - 使用 IIS 的 GUI 配置编辑器创建相同的 Powershell - 在默认日志文件设置的服务级别。

我尝试使用网站级别的 Config 编辑器将 sc-bytes 和 cs-bytes 字段添加到实际日志文件中,但日志文件没有出现在列表中?

我现在已经放弃并使用了 GUI。

我看到其他人问过类似的问题,希望我们都能得到答案:-)

【问题讨论】:

  • 问题很不清楚。你到底在找什么?为什么sc-bytes 在服务器级命令中不起作用或如何在站点级设置?

标签: powershell iis


【解决方案1】:

您需要用于属性的值与 iislog 中的列名不同,而是枚举 LogExtFileFlags 的值。您可以使用提供的 MSDN 链接来查找值或使用服务器上的 GUI 设置它,并使用 Get-WebConfigurationProperty 获取值。例如:

Get-WebConfigurationProperty -Filter System.Applicationhost/Sites/SiteDefaults/logfile -Name
LogExtFileFlags
Date,Time,ClientIP,ServerIP,BytesSent,BytesRecv

#This doesn't work because sc-bytes and cs-bytes are not the right values
Set-WebConfigurationProperty -Filter System.Applicationhost/Sites/SiteDefaults/logfile -Name LogExtFileFlags -Value "Date,Time,ClientIP,sc-bytes,cs-bytes"

#However this works
Set-WebConfigurationProperty -Filter System.Applicationhost/Sites/SiteDefaults/logfile -Name LogExtFileFlags -Value "Date,Time,ClientIP,BytesSent,BytesRecv"

要获取和设置特定网站的日志记录字段,请使用:

#Get current
PS > (Get-ItemProperty 'IIS:\Sites\Default Web Site\' -Name logfile).logExtFileFlags
Date,Time,ClientIP,ServerIP,BytesSent,BytesRecv

#Set new value
PS > Set-ItemProperty 'IIS:\Sites\Default Web Site\' -Name logfile -Value @{logExtFileFlags = "Date,T
ime,ClientIP,ServerIP,BytesSent" }

#Verify
PS > (Get-ItemProperty 'IIS:\Sites\Default Web Site\' -Name logfile).logExtFileFlags
Date,Time,ClientIP,ServerIP,BytesSent

【讨论】:

  • 感谢您的时间和麻烦,很抱歉隐藏了这个问题,谢谢。
  • 太棒了。 :-) 如果它解决了您的问题,请记住通过接受正确答案来结束问题。
  • Import-Module WebAdministration Set-Location IIS:\ #Get current (Get-ItemProperty '.\Sites\Contoso\' -Name logfile).logExtFileFlags #Set new value Set-ItemProperty '.\Sites \Contoso\' -Name logfile -Value @{logExtFileFlags = "Date,Time,ClientIP,UserName,ServerIP,Method,UriStem,UriQuery,HttpStatus,Win32Status,BytesSent,BytesRecv,TimeTaken,ServerPort,UserAgent,Referer,HttpSubStatus" } #验证 (Get-ItemProperty '.\Sites\Contoso\' -Name logfile).logExtFileFlags
  • #它工作得很好——我现在可以添加 BytesSent,BytesRecv 列,看看我有#我喜欢你答案的点符号。 #很高兴提醒您,与 SQL 服务器一样,IIS 管理单元具有驱动器隐喻,可让您围绕结构进行 DIR 和 CD - 尽管有很多集合破坏了简单的乐趣 - Select * 仅此而已#再次感谢你:-)
【解决方案2】:

我在这里主要引用@Frode 解决方案。但是我在调​​用 Get-ItemProperty 时遇到了这个错误:

#Get current
PS > (Get-ItemProperty 'IIS:\Sites\Default Web Site\' -Name logfile).logExtFileFlag
Get-ItemProperty : Cannot find drive. A drive with the name 'IIS' does not exist.
At line:1 char:2
+ (Get-ItemProperty 'IIS:\Sites\Default Web Site\' -Name logfile).logEx ...
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (IIS:String) [Get-ItemProperty], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetItemPropertyCommand

奇怪的是能够在网站名称上执行 Get-ItemProperty,我必须在每台机器上至少执行一次此命令(Win Server 2019):

PS > Get-WebConfigurationProperty -Filter System.Applicationhost/Sites/SiteDefaults/logfile -Name LogExtFileFlags

执行后,我可以在 IIS 中引用任何网站并设置字段列表:

PS > Set-ItemProperty 'IIS:\Sites\Integration Web Service\' -Name logfile -Value @{logExtFileFlags = "Date,Time,ClientIP,UserName,ServerIP,Method,UriStem,UriQuery,HttpStatus,Win32Status,TimeTaken,ServerPort,UserAgent,Referer,Host,HttpSubStatus" }

不确定我是否有这种行为的原因,或者是否有更好的方法来解决此问题,但至少这为我解决了问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-22
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 2017-12-12
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多