alboard
alboard

Reputation: 13

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

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 through 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.

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 :-)

Upvotes: 0

Views: 3998

Answers (2)

Moadh
Moadh

Reputation: 25

I am mostly quoting @Frode solution here. But I was getting this error when invoking the 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

Weirdly enough to be able to execute the Get-ItemProperty on a website name, I had to execute atleast once this command on each machine (Win Server 2019) :

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

Once it was executed, I was able to reference any website in IIS ans set the fields list :

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" }

Not sure if there is a reason why I had this behaviour, or if there is a better way to workaround this issue but atleast this fixed it for me.

Upvotes: 0

Frode F.
Frode F.

Reputation: 54881

The values you need to use for the property is not the same as the column-names in the iislog, but values for the enum LogExtFileFlags. You can use the provided MSDN-link to find the values or set it using GUI on a server and and get the values using Get-WebConfigurationProperty. Ex:

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"

To get and set logging fields for a specific web site, use:

#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

Upvotes: 2

Related Questions