当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell 在 SharePoint 中存档 ULS 和 IIS 日志文件

作者:精品下载站 日期:2024-12-14 14:38:49 浏览:18 分类:玩电脑

使用 PowerShell 在 SharePoint 中存档 ULS 和 IIS 日志文件


要求: 归档IIS和ULS日志的日志文件。

根据您的配置和使用情况,SharePoint 中的日志文件(IIS 日志和 ULS 日志)可能会增长并填满磁盘空间。各种博客和论坛中有许多可用的脚本,这是我在 SharePoint 服务器上存档日志文件的脚本。

[玩转系统] 使用 PowerShell 在 SharePoint 中存档 ULS 和 IIS 日志文件

PowerShell 在 SharePoint 中存档日志文件:

在配置参数部分设置日志文件路径和存档位置并运行脚本。该脚本执行以下操作:

  • 将最后一次修改时间是从今天起 7 天或更早之前的所有日志文件复制到文件夹中。
  • 从包含要存档的日志文件的文件夹中创建 Zip 文件。
  • 删除旧的日志文件。

Add-Type -Assembly System.IO.Compression.FileSystem

#Configuration Parameters
$LogFilesPath="C:\inetpub\logs\LogFiles\W3SVC3"
$ArchivePath="C:\LogArchive"

#Get All Log Files Modified before Last Week
$LogFilesToArchive = Get-ChildItem -Path $LogFilesPath -recurse | where-object {$_.LastWriteTime -le (Get-Date).addDays(-7) }
Write-host "Total Log Files to Archive:"$LogFilesToArchive.count -f Yellow
if($LogFilesToArchive.count -eq 0) { Break }

#Create a Temp Folder
$LogFileDate = Get-Date -Format "yyyyMMdd"
$TempFolder = Join-path -path $ArchivePath $LogFileDate
If (!(Test-Path $TempFolder)) 
{  
    New-Item $TempFolder -type Directory | Out-Null
}

#Copy Log Files to Temp Folder and Remove them
Foreach ($Logfile in $LogFilesToArchive)
{
    #Copy Log file to the Temp Directory
    Copy-Item -Path $Logfile.FullName -Destination $TempFolder
    
    #Delete the copied Log file from the source
    Remove-Item $Logfile.FullName -Confirm:$False
}

#Archive the Temp Folder
$ZipFileName= "$($ArchivePath)$($LogFileDate).zip"
If (Test-Path $ZipFileName){ Remove-Item $ZipFileName }

$CompressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
[System.IO.Compression.ZipFile]::CreateFromDirectory($TempFolder, $ZipFileName, $CompressionLevel, $False)

#Remove the Temp Folder
Remove-Item $TempFolder -Recurse -Force -Confirm:$False

Write-host "$($LogFilesToArchive.count) Log Files Archived to '$($ZipFileName)'" -f Green

在 Windows 任务计划程序中安排它以自动执行脚本。您还可以将脚本包装在可重用函数中,以从多个位置归档日志文件。此脚本删除所有超过 30 天的日志文件。您可以更改硬编码值以满足您的要求。


Add-Type -Assembly System.IO.Compression.FileSystem

Function Archive-Logs($LogFilesPath, $ArchivePath)
{
    #Get All Log Files Modified older than a Last Week
    $LogFilesToArchive = Get-ChildItem -Path $LogFilesPath -recurse | where-object {$_.LastWriteTime -le (Get-Date).addDays(-30) }
    Write-host "Total Log Files to Archive:"$LogFilesToArchive.count -f Yellow
    if($LogFilesToArchive.count -eq 0) { Return }

    #Create a Temp Folder
    $LogFileDate = Get-Date -Format "yyyyMMdd-HHMMss"
    $TempFolder = Join-path -path $ArchivePath $LogFileDate
    If (!(Test-Path $TempFolder)) 
    {  
        New-Item $TempFolder -type Directory | Out-Null
    }

    #Copy Log Files to Temp Folder and Remove them
    Foreach ($Logfile in $LogFilesToArchive)
    {
        #Copy Log file to the Temp Directory
        Copy-Item -Path $Logfile.FullName -Destination $TempFolder
    
        #Delete the copied Log file from the source
        Remove-Item $Logfile.FullName -Confirm:$False
    }

    #Archive the Temp Folder
    $ZipFileName= "$($ArchivePath)$($LogFileDate).zip"
    If (Test-Path $ZipFileName){ Remove-Item $ZipFileName }

    $CompressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
    [System.IO.Compression.ZipFile]::CreateFromDirectory($TempFolder, $ZipFileName, $CompressionLevel, $False)

    #Remove the Temp Folder
    Remove-Item $TempFolder -Recurse -Force -Confirm:$False

    Write-host "$($LogFilesToArchive.count) Log Files Archived to '$($ZipFileName)'" -f Green
}

#Call The function
Archive-Logs -LogFilesPath "C:\inetpub\logs\LogFiles\W3SVC3232" -ArchivePath "C:\LogArchive"
Archive-Logs -LogFilesPath "C:\inetpub\logs\LogFiles\W3SVC6245" -ArchivePath "C:\LogArchive"
Archive-Logs -LogFilesPath "D:\SPLogs\ULS" -ArchivePath "C:\LogArchive"

PowerShell 归档旧文件

在另一种情况下,我们将具有相同文件夹、子文件夹结构的所有旧文件移动到另一个文件夹。


Function Archive-OldFiles
{
 param
    (
        [Parameter(Mandatory=$true)] [string] $SourcePath,
        [Parameter(Mandatory=$true)] [string] $ArchivePath,
        [Parameter(Mandatory=$true)] [string] $LogFile,
        [Parameter(Mandatory=$false)] [Int] $Days = -30 #Number of Days to check beyyond - based on last modified by date
    )

    Try {
        Add-content $Logfile -value "`n------------------- Archive Files Script Started: $(Get-date -format 'dd/MM/yyy hh:mm:ss tt')----------------" 

        #Get All Files from the Source - matching given condition
        $SourceFiles = Get-ChildItem -Path $SourcePath -Recurse -File | Where {$_.LastWriteTime -lt (Get-Date).AddDays($Days)}
        Write-host "Total Number of Files matching in the Source: $($SourceFiles.Count)"
        Add-content $Logfile -value "Total Number of Files matching in the Source: $($SourceFiles.Count)" 
        
        $Counter = 1     
        #Move Files to Archive Folder
        $SourceFiles | ForEach-Object {
            #Get the File name and Folder of the File
            $FileName = $_.FullName
            $FolderPath = $_.Directory.FullName
            $TargetFolder = $FolderPath.Replace($SourcePath, $ArchivePath)

            #Display Progress bar
            $Status  = "Archiving '" + $FileName + "' to " + $TargetFolder +" ($($Counter) of $($SourceFiles.Count))"
            Write-Progress -Activity "Archiving Files..." -Status $Status -PercentComplete (($Counter / $SourceFiles.Count) * 100)

            Try {
                #Create Folder in the destination, if it doesn't exist
                If(!(Test-Path $TargetFolder))
                {
                    New-Item -ItemType Directory -Force -Path $TargetFolder | Out-Null -ErrorAction stop
                }

                #Move Files to Archive
                Move-Item $_.FullName -Destination $TargetFolder -Force -ErrorAction Stop
                Write-Host "Successfully moved $FileName to $TargetFolder$($_.Name)" -f Green
                Add-Content $LogFile -Value "Successfully moved $FileName to $TargetFolder$($_.Name)" 
            }
            Catch {  
                Write-host -f Red "Error Moving $FileName to $TargetFolder, $($_.Exception.Message)"  
                Add-Content $LogFile -Value "Error moving $FileName $($_.Exception.Message)"
            }
            $Counter++       
        }
    }
    catch {
        Write-host -f Red "Error Moving $FileName to $TargetFolder, $($_.Exception.Message)"  
        Add-Content $LogFile -Value "Error moving $FileName $($_.Exception.Message)"
    }
    Finally {
        Add-content $Logfile -value "---------------------- Archive Files Script Completed: $(Get-date -format 'dd/MM/yyy hh:mm:ss tt')-----------------"
    }    
}
#Parameters
$SourcePath = "E:\Reports"
$ArchivePath = "E:\Reports-Archive"
$Days = "-365"
$LogFile = "E:\Reports\Log.txt"
Archive-OldFiles -SourcePath $SourcePath -ArchivePath $ArchivePath -Days $Days -LogFile $LogFile

使用 PowerShell 归档 IIS 日志是一种简单而有效的方法,可以释放服务器上的存储空间并确保 IIS 日志井井有条且易于访问。按照这些步骤,您可以使用 PowerShell 轻松归档 IIS 日志,从而节省时间和精力。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯