使用Powershell配置高级IIS日志时出现问题

时间:2015-08-04 19:15:33

标签: asp.net powershell iis

此脚本的目标:

  • 为每个网站创建一个新文件夹
  • 每个网站每天创建一个日志
  • 以特定格式备份/激活脚本的第二个脚本
  • 停止所有正常记录
# Ensure cmdlets are available
Import-Module WebAdministration

$Websites = (Get-Website).Name 

foreach($site in $websites)

 {

    Stop-Website $site

}


# Disables http logging module

Set-WebConfigurationProperty -Filter system.webServer/httpLogging -PSPath 
machine/webroot/apphost -Name dontlog -Value true


# Disables the default advanced logging config

Set-WebConfigurationProperty -Filter "system.webServer/advancedLogging/server
/logDefinitions/logDefinition[@baseFileName='%COMPUTERNAME%-Server']" -name 
enabled -value false


# Enable Advanced Logging

Set-WebConfigurationProperty -Filter system.webServer/advancedLogging/server 
-PSPath machine/webroot/apphost -Name enabled -Value true


# Move to location to store iis logs

cd C:\inetpub\Logs\Advancedlogs


# Create Folders if required

Foreach($site in $websites)
{
    $FoldersPresent = Test-Path "C:\inetpub\logs\AdvancedLogs\$site"
    if($FoldersPresent -eq $true)
        { 
            Write-host "Folder exsists"
        }
        else
        {
            New-Item -ItemType directory $site
        }

}



# Create hash table

$list = @{'Win32Status'='sc-win32-status';'URI-Stem'='cs-uri-stem';
'URI-Querystring'='cs-uri-query';'Time-UTC'='time';'Time-Local'='time-
local';'Take Taken'='TimeTakenMS';'Substatus'='sc-substatus';'Status'='sc-
status';'Site Name'='s-sitename';'Server-IP'='s-ip';'Server Port'='s-port';
'Server Name'='s-computername';'Protocol Version'='cs-version';'Protocol'='c-
protocol';'Method'='cs-method';'Date-UTC'='date';'Date-Local'='date-local';
'ContentPath'='s-contentpath';'Client-IP'='c-ip';'EndRequest-UTC'='EndRequest-
UTC';'Bytes Sent'='sc-bytes';'Bytes Received'='cs-bytes';'BeginRequest-
UTC'='BeginRequest-UTC';'UserName'='cs-username';'User Agent'='cs(User-
Agent)';'Referer'='cs(Referer)';'Proxy'='s-proxy';'Host'='cs(Host)';
'Cookie'='cs(Cookie)'}


# Set the log file fields

foreach($site in $Websites)
{
    $AppName = $site
    $appdrive = "C:\inetpub\logs\AdvancedLogs\$site"
    $LogRotateFreq = "Daily"

    Add-WebConfiguration /system.webServer/advancedLogging/Server
/logDefinitions IIS:\ -Location $AppName -value 
@{baseFileName="%COMPUTERNAME%-$AppName";enabled="true";
logRollOption="Schedule";schedule=$LogRotateFreq;publishLogEvent="false"}
    $list.GetEnumerator() | foreach {
                                        Add-WebConfiguration "/system.webServer/advancedLogging/Server/logDefinitions/logDefinition[@baseFileName=""%COMPUTERNAME%-$AppName""]/selectedFields" IIS:\ -Location $AppName -value @{defaultValue="";required="false";logHeaderName=$_.Name;id=$_.Value;}
                                        Set-WebConfiguration "/system.applicationHost/Sites/Site[@name=""$AppName""]/advancedLogging" -value @{directory="$appdrive"}
                                    }
}


# Start the websites

foreach($site in $websites)
{
    Start-Website $site
}

用于指导的链接:

http://forums.iis.net/t/1185802.aspx?No+log+files+created+by+Advanced+Logging http://forums.iis.net/t/1193633.aspx?Configure+Advanced+Logging+through+powershellhttp://www.antonyfrancis.net/2014/01/automated-installation-and.html

0 个答案:

没有答案