【发布时间】: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