【问题标题】:Powershell to Remove IIS Logging FieldPowershell 删除 IIS 日志记录字段
【发布时间】:2020-12-11 17:23:45
【问题描述】:

尝试使用 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

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

【问题讨论】:

  • 您的$loggingFilter 在实际字符串值中有引号,因为它们用反引号转义,而您的第二个Get-WebConfigurationProperty 示例在SpecificSite 周围没有任何引号。尝试将您的 Get- / Set-WebConfigurationProperty 作为 2-liner $loggingFilter = "/system.applicationHost/sites/site[@name=`"$websiteName`"]/LogFile" 然后 Get-WebConfigurationProperty -Filter $loggingFilter -Name LogExtFileFlags 看看是否有效。

标签: powershell logging iis


【解决方案1】:

这里有两个 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 ""

【讨论】:

    猜你喜欢
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多