ค้นหาเว็บไซต์

เก็บถาวรไฟล์บันทึก ULS และ IIS ใน SharePoint โดยใช้ PowerShell


ข้อกำหนด: เก็บไฟล์บันทึกของ IIS และ ULS Log

ไฟล์บันทึกใน SharePoint (ทั้งบันทึก IIS และบันทึก ULS) สามารถขยายและเติมเต็มพื้นที่ดิสก์ตามการกำหนดค่าและการใช้งานของคุณ มีสคริปต์มากมายในบล็อกและบอร์ดต่างๆ และนี่คือสคริปต์ของฉันสำหรับเก็บถาวรไฟล์บันทึกบนเซิร์ฟเวอร์ SharePoint

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

การเก็บถาวรบันทึก IIS โดยใช้ PowerShell เป็นวิธีที่ง่ายและมีประสิทธิภาพในการเพิ่มพื้นที่จัดเก็บข้อมูลบนเซิร์ฟเวอร์ของคุณ และรับรองว่าบันทึก IIS ของคุณได้รับการจัดระเบียบและเข้าถึงได้ง่าย ทำตามขั้นตอนเหล่านี้ คุณสามารถเก็บบันทึก IIS ได้อย่างง่ายดายโดยใช้ PowerShell ซึ่งช่วยประหยัดเวลาและความพยายาม

บทความที่เกี่ยวข้อง