【问题标题】:Need to find the name of the current logfile being used in IIS需要查找当前在 IIS 中使用的日志文件的名称
【发布时间】:2019-11-27 04:31:49
【问题描述】:

我需要编写一个脚本来自动从运行了几个月的系统中移动所有 IIS 日志文件。该系统由许多 Web 服务器组成。

我成功地将日志目录移动到新位置,然后我的脚本会将其他日志文件从旧目录移动到新位置,但当前文件除外。它无法移动,因为目标中存在同名文件。 (当前日志文件。)我想重命名它,但不想重命名目录中的所有文件。我知道我可以使用通配符,但我更愿意只重命名那个文件。

我可以使用什么命令来查找文件名,而不是目录或路径?我从这里和网络上找到的其他较小的请求拼凑而成。

Import-Module WebAdministration
$LogPath = "e:\Logs\IISlogs"
foreach($WebSite in $(get-website))
    {
    $logFile="$($Website.logFile.directory)\w3svc$($website.id)".replace("%SystemDrive%",$env:SystemDrive)
    } 
New-Item $LogPath -ItemType Directory


Set-ItemProperty “IIS:\Sites\Default Web Site” -name logFile.directory -value $LogPath
$path = $logpath+"\W3SVC1"
$Timeout = 60
$timer = [Diagnostics.Stopwatch]::StartNew()
while (($timer.Elapsed.TotalSeconds -lt $Timeout) -and (-not (Test-Path -Path $path  ))) 
{

    Start-Sleep -Seconds 1
    $tot = $timer.Elapsed.Seconds
    Write-Output -Message ("Still waiting for action to complete after " + $tot + " seconds upto " + $Timeout)
}
$timer.Stop()

Move "$($logfile)\*.*" $path

【问题讨论】:

    标签: powershell iis


    【解决方案1】:

    所有文件都有时间和日期戳。过滤文件名,按时间戳降序排列,选择第一个。

    例如:

    $dir = "e:\Logs\IISlogs"
    ($latest = Get-ChildItem -Path $dir | 
    Sort-Object LastAccessTime -Descending | 
    Select-Object -First 1)
    "Current log file details:  $($latest.name)"
    Get-Content $latest
    

    如果您说日志可以使用不同的名称,那么您需要在路径规范中指定通配符匹配。

    【讨论】:

    • 我试过了,但通常 IIS 日志有两个具有相同的修改日期。
    • 一直到时间戳,而不仅仅是日期。在同一个目录中,一个文件几乎不可能有完全相同的日期和时间。
    • 不用担心。我有几个需要这种努力的自动化任务。所以,我感受到了你正在经历的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    • 1970-01-01
    • 2012-02-06
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    • 2013-08-12
    相关资源
    最近更新 更多