Trying to remove an existing IIS logging field for a specific site using PowerShell.
I have a PS that will add the desired fields, taken from activate-iis-site-logging-field-powershell - this was the only method I could found to successfully add fields to a specific site name .
Function Activate-LoggingField { param([Parameter(Mandatory=$true)][string]$websiteName, [Parameter(Mandatory=$true)][string]$loggingField) $loggingFilter = "/system.applicationHost/sites/site[@name=`"$websiteName`"]/LogFile" $currentLoggingFields = Get-WebConfigurationProperty -Filter $loggingFilter -Name LogExtFileFlags if ($currentLoggingFields -notmatch $loggingField) { $newLoggingFields = "$currentLoggingFields,$loggingField" Set-WebConfigurationProperty -Filter $loggingFilter -Name LogExtFileFlags -Value $newLoggingFields } } Activate-LoggingField -websiteName "SpecificSite" -loggingField > "Date,Time,ClientIP,UserName,SiteName,ComputerName,ServerIP,Method,UriStem,UriQuery,HttpStatus,Win32Status,BytesSent,BytesRecv,TimeTaken,ServerPort,UserAgent,Referer,ProtocolVersion,Host,HttpSubStatus"
If I use this command directly, I get an error - not sure why, as it should be the same command generated from above.
Set-WebConfigurationProperty -Filter System.Applicationhost/Sites/Site[@name="SpecificSite"]/logfile -Name LogExtFileFlags -Value "Date,Time"
WARNING: Target configuration object 'System.Applicationhost/Sites/Site[@name=SpecificSite]/logfile is not found at path 'MACHINE/WEBROOT/APPHOST'.
In my case, the Cookie field remains selected (periodically added by another application). No luck removing some or all the fields first, using Clear-WebConfiguration or Remove-WebConfiguration.
Side note: using the id instead of name works for specific sites (but is not scalable across servers). From applicationHost.config:
<site name="SpecificSite" id="2" serverAutoStart="true">
So I'd think I could use [@name=SpecificSite] in a command all by itself, but using the site name doesn't seem to work unless the name is a variable. Expected?
this works
Get-WebConfigurationProperty -Filter /system.applicationHost/sites/site[@id=2]/LogFile -Name LogExtFileFlags
but this does not (with or without quotes around the name, not using a variable for the name)
Get-WebConfigurationProperty -Filter /system.applicationHost/sites/site[@name=SpecificSite]/LogFile -Name LogExtFileFlags
//post-accepted edit - my mistake was not wrapping the entire -Filter value in quotes. I was just using quotes on around "SpecificSite". I used the site ID first, which works without any quotes - that threw me off. Thanks again.
Here are two powershell commands to add fields and remove all fields with correct format. You can refer to it.
Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter "system.applicationHost/sites/site[@name='sitename']/logFile" -name "logExtFileFlags" -value "Date,Time,ClientIP,UserName,SiteName,ComputerName,ServerIP,Method,UriStem,UriQuery,HttpStatus,Win32Status,BytesSent,BytesRecv,TimeTaken,ServerPort,UserAgent,Cookie,Referer,ProtocolVersion,Host,HttpSubStatus"
Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter "system.applicationHost/sites/site[@name='sitename']/logFile" -name "logExtFileFlags" -value ""
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.