How to add new fields to an existing IIS web site log using Set-WebConfigurationProperty

时间:2017-04-06 16:43:38

标签: powershell iis

https://www.interfacett.com/blogs/how-change-iis-log-contents-powershell/ tells me how to set default file fields at a service level, helpful :-) But I want to set the actual file fields (add sc-bytes and cs-bytes) at an individual named web site level - as you can so easily though the IIS GUI yes - but so much better to be able to SET and GET (to check) using a repeatable script against multiple IIS web & app servers - yup I would really like to catalogue IIS services with a POSH script ...

So this works beautifully:-

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

.... and I can - thanks to a Stackoverflow tip - use IIS's GUI Config editor to create the same Powershell - at the service level for the default log file settings.

So I tried to use the Config editor at the Web site level to add the sc-bytes and cs-bytes fields to the actual log file but the log file does not appear in the list?

I have given up for now and used the GUI.

I have seen others asking similar questions and hoped that we could all get an answer :-)

2 个答案:

答案 0 :(得分:0)

您需要为该属性使用的值与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

答案 1 :(得分:0)

我在这里主要引用@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" }

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