通过powershell获取IIS日志位置?

时间:2014-11-06 16:48:06

标签: powershell iis logging

我正在编写一个脚本,我希望能够在IIS服务器之间轻松移动以分析日志,但这些服务器将日志存储在不同的位置。有些在C:/有些在D:/有些在W3SVC1,有些在W3SVC3。我希望PowerShell可以自己查看这些信息,而不必在每台服务器上手动编辑。 (是的,我是一个懒惰的系统管理员。#automateallthethings。)

如果我可以将域名传递给它,那么PowerShell是否可以使用此信息?

3 个答案:

答案 0 :(得分:12)

我发现这对我有用,因为我想知道所有站点的日志目录。

Import-Module WebAdministration

foreach($WebSite in $(get-website))
    {
    $logFile="$($Website.logFile.directory)\w3svc$($website.id)".replace("%SystemDrive%",$env:SystemDrive)
    Write-host "$($WebSite.name) [$logfile]"
    } 

答案 1 :(得分:3)

Import-Module WebAdministration

$sitename = "mysite.com"
$site = Get-Item IIS:\Sites\$sitename
$id = $site.id
$logdir = $site.logfile.directory + "\w3svc" + $id

感谢Chris Harris将网站ID的想法放在我的脑海中。之后我能够更好地搜索它,它引导我进入WebAdministration模块及其使用示例。

答案 2 :(得分:0)

很好......我稍微更新了你的脚本以询问IIS的日志文件位置。

    param($website = 'yourSite')

Import-Module WebAdministration

$site = Get-Item IIS:\Sites\$website
$id = $site.id
$logdir = $site.logfile.directory + "\w3svc" + $id

$time = (Get-Date -Format "HH:mm:ss"(Get-Date).addminutes(-30))
# Location of IIS LogFile
$File = "$logdir\u_ex$((get-date).ToString("yyMMdd")).log"
# Get-Content gets the file, pipe to Where-Object and skip the first 3 lines.
$Log = Get-Content $File | where {$_ -notLike "#[D,S-V]*" }
# Replace unwanted text in the line containing the columns.
$Columns = (($Log[0].TrimEnd()) -replace "#Fields: ", "" -replace "-","" -replace "\(","" -replace "\)","").Split(" ")
# Count available Columns, used later
$Count = $Columns.Length
# Strip out the other rows that contain the header (happens on iisreset)
$Rows = $Log | where {$_ -like "*500 0 0*"}
# Create an instance of a System.Data.DataTable
#Set-Variable -Name IISLog -Scope Global
$IISLog = New-Object System.Data.DataTable "IISLog"
# Loop through each Column, create a new column through Data.DataColumn and add it to the DataTable
foreach ($Column in $Columns) {
  $NewColumn = New-Object System.Data.DataColumn $Column, ([string])
  $IISLog.Columns.Add($NewColumn)
}
# Loop Through each Row and add the Rows.
foreach ($Row in $Rows) {
  $Row = $Row.Split(" ")
  $AddRow = $IISLog.newrow()
  for($i=0;$i -lt $Count; $i++) {
    $ColumnName = $Columns[$i]
    $AddRow.$ColumnName = $Row[$i]
  }
  $IISLog.Rows.Add($AddRow)
  }
  $IISLog | select @{n="DateTime"; e={Get-Date ("$($_.date) $($_.time)")}},csuristem,scstatus | ? { $_.DateTime -ge $time }