I'm really struggling with this. The command is similar to what is confirmed here, and on numerous other sites.
I want to add a custom logging field to IIS via the Add-WebConfigurationProperty
Powershell command. The custom field name is X-Forwarded-For
. However, I have resorted to also trying to add a test
field. I'm using IIS v10 on Windows Server 2019.
I use this:
Add-WebConfigurationProperty -filter "system.applicationHost/sites/siteDefaults/logFile/customFields" -name "." -value @{logFieldName='test';sourceName='X-Forwarded-For';sourceType='RequestHeader'}
The first time I run it, I get this:
Add-WebConfigurationProperty : Filename: \\?\C:\Windows\system32\inetsrv\config\applicationHost.config
Error: Cannot commit configuration changes because the file has changed on disk
The second time I run it, I get this:
Add-WebConfigurationProperty : Filename:
Error: Cannot add duplicate collection entry of type 'add' with unique key attribute 'logFieldName' set to 'test'
And, at no time does it ever modify the applicationHost.config file at all:
<siteDefaults>
<logFile logExtFileFlags="Date, Time, ClientIP, UserName, ServerIP, Method, UriStem, UriQuery, HttpStatus, Win32Status, BytesSent, BytesRecv, TimeTaken, ServerPort, UserAgent, Referer, HttpSubStatus" logFormat="W3C" directory="D:\inetpub\logs\logfiles">
<customFields>
<clear />
</customFields>
</logFile>
<traceFailedRequestsLogging directory="D:\inetpub\logs" />
<ftpServer>
<logFile directory="D:\inetpub\logs\logfiles" />
</ftpServer>
</siteDefaults>
Nor does the field ever show up in IIS Manager. Nor does it show up with Get-WebConfigurationProperty
.
In addition, I manually added the X-Forwarded-For
through the IIS Manager GUI (which immediately modified the applicationHost.config properly and shows up via Get-WebConfigurationProperty
), and then tried adding it through Powershell and I again get the can't add "duplicate" field error. Then, when I delete the field through the GUI and run the command again, I get the cannot commit changes error. If I run the command again, I get the cannot add duplicate error. Yet, the applicationHost.config still doesn't get modified. So clearly the command IS affecting the same area I am changing in the GUI, and it appears to do something because I can't add duplicates, but the changes never appear in applicationHost.config or in the GUI. I've tried closing and re-opening IIS Manager and restarting IIS.
What am I missing?
Apparently, my Powershell session was messed up.
After closing Powershell and re-opening it, I was able to execute the command successfully and the change immediately showed up in the GUI and applicationHost.config after refreshing.
The problem re-appears when:
After this, Powershell must be closed and re-opened, after which I can add the property successfully.