首页 > 解决方案 > Powershell 删除 IIS 日志记录字段

问题描述

尝试使用 PowerShell删除特定站点的现有 IIS 日志记录字段。

我有一个 PS 将添加所需的字段,取自activate-iis-site-logging-field-powershell - 这是我能找到的唯一一种成功将字段添加到特定站点名称的方法。

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"

如果我直接使用这个命令,我会得到一个错误 - 不知道为什么,因为它应该是从上面生成的相同命令。

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

就我而言,Cookie字段保持选中状态(由另一个应用程序定期添加)。使用 Clear-WebConfiguration 或 Remove-WebConfiguration 先删除部分或全部字段并不顺利。


旁注:使用id而不是name适用于特定站点(但不能跨服务器扩展)。从 applicationHost.config:

<site name="SpecificSite" id="2" serverAutoStart="true">

所以我认为我可以在命令中单独使用 [@name=SpecificSite] ,但是除非名称是变量,否则使用站点名称似乎不起作用。预期的?

这行得通

Get-WebConfigurationProperty -Filter /system.applicationHost/sites/site[@id=2]/LogFile -Name LogExtFileFlags

但这不是(名称周围有或没有引号,不使用名称的变量)

Get-WebConfigurationProperty -Filter /system.applicationHost/sites/site[@name=SpecificSite]/LogFile -Name LogExtFileFlags

//post-accepted edit - 我的错误是没有将整个 -Filter 值用引号括起来。我只是在“SpecificSite”周围使用引号。我首先使用了站点 ID,它没有任何引号 - 这让我很失望。再次感谢。

标签: powershellloggingiis

解决方案


这是两个 powershell 命令,用于添加字段并删除格式正确的所有字段。你可以参考一下。

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

推荐阅读