Activate IIS site logging field [PowerShell]

隐身守侯 提交于 2021-02-08 09:53:38

问题


Is there a way to activate logging fields on a specific website in IIS, preferably through PowerShell?

I want to activate the 'Client IP Address' logging field without needing to RDP to the machine and setting it manually.

I am running Windows Server 2012 with IIS 8.5


回答1:


You can also set the flags by using the following methodology whereby you use the total value of the flags required to by set as per the list below.

The values are set by 2 to the power of the flag number. Total up all the flags you want turned on and you get the value that you can set the logExtFileFlags to.

<flag number= "0" flag name="Date" value="1" /> 
<flag number= "1" flag name="Time" value="2" /> 
<flag number= "2" flag name="ClientIP" value="4" /> 
<flag number= "3" flag name="UserName" value="8" /> 
<flag number= "4" flag name="SiteName" value="16" /> 
<flag number= "5" flag name="ComputerName" value="32" /> 
<flag number= "6" flag name="ServerIP" value="64" /> 
<flag number= "7" flag name="Method" value="128" /> 
<flag number= "8" flag name="UriStem" value="256" /> 
<flag number= "9" flag name="UriQuery" value="512" /> 
<flag number= "10" flag name="HttpStatus" value="1024" /> 
<flag number= "11" flag name="Win32Status" value="2048" /> 
<flag number= "12" flag name="BytesSent" value="4096" /> 
<flag number= "13" flag name="BytesRecv" value="8192" /> 
<flag number= "14" flag name="TimeTaken" value="16384" /> 
<flag number= "15" flag name="ServerPort" value="32768" /> 
<flag number= "16" flag name="UserAgent" value="65536" /> 
<flag number= "17" flag name="Cookie" value="131072" /> 
<flag number= "18" flag name="Referer" value="262144" /> 
<flag number= "19" flag name="ProtocolVersion" value="524288" /> 
<flag number= "20" flag name="Host" value="1048576" /> 
<flag number= "21" flag name="HttpSubStatus" value="2097152" /> 

E.G. We want UserName, ServerIP, ServerPort and Host turned on. This requires us to add 8 + 64 + 32768 + 1048576 = 1081416

The command therefore is :

Set-ItemProperty "IIS:\Sites\$siteName" -name LogFile.logExtFileFlags -value 1081416



回答2:


After a bit more searching, I found a baseline for my own solution at: Set IIS Log Fields via PowerShell

The Activate-LoggingField function takes in a website name and logging field name as parameters

It gets the supplied website's active logging fields via the Get-WebConfigurationProperty cmdlet and checks if the supplied logging field is present. If it is not present, it will attempt to add it via the Set-WebConfigurationProperty cmdlet

Import-Module Webadministration

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 "MySite" -loggingField "ClientIP"


来源:https://stackoverflow.com/questions/36552280/activate-iis-site-logging-field-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!