Moving and archiving IIS Logs

Welcome Forums General PowerShell Q&A Moving and archiving IIS Logs

This topic contains 1 reply, has 2 voices, and was last updated by

3 years, 9 months ago.

  • Author
  • #24150

    Points: 0
    Rank: Member

    I want to write a function that would move IIS logs to a remote server, then verify the move, log the errors if any, send email alerts, and zip the files if move is successful. I would then call the function using another script that would be populated with a CSV file with the servernames and IPs and all other necessary parameters. So, I broke down the function into multiple parts and wrote these functions and cmdlets so far:


    Function MoveFile






    Helpmessage='which server would you like to target?')]






    [string]$logname = 'C:\errors.txt'


    begin {

    write-verbose "Deleting Logname"

    del $logname -ErrorAction SilentlyContinue


    process {

    Write-verbose "Copying IIS Logs"

    foreach ($item in (Get-ChildItem $Servername\$sourcefile)) {

    copy-Item $item.Fullname $DestinationFile -Force




    Compare-Object -ReferenceObject $(get-content $Servername\$sourcefile) -DifferenceObject $(get-content $Destinationfile) | Where-Object sideindicator -Match "^<=.*" | foreach-object {"content mistmatch"} 3. function send-notification($subject, $detail) { $message = New-Object System.Net.Mail.MailMessage $message.Subject = $subject $message.Body = $detail $message.To.Add("") $message.From = "" $client = New-Object System.Net.Mail.SMTPClient -ArgumentList "" $client.Send($message) } 4. function zip-files { param($Sourcedirectory, $zipFilename, $zipFilepath, $zipFile) #Prepare zip file if(-not (test-path($zipFile))) { set-content $zipFile ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) (dir $zipFile).IsReadOnly = $false } $shellApplication = new-object -com shell.application $zipPackage = $shellApplication.NameSpace($zipFile) $files = Get-ChildItem -Path $srcdir | where{! $_.PSIsContainer} foreach($file in $files) { $zipPackage.CopyHere($file.FullName) #using this method, sometimes files can be 'skipped' #this 'while' loop checks each file is added before moving to the next while($zipPackage.Items().Item($ -eq $null){ Start-sleep -seconds 1 } } } I do not know how to include all of these functions and cmdlets in 1 function and make them work for my use-case. I would like someone to help me out with this.

  • #24190

    Points: 1,785
    Helping HandTeam Member
    Rank: Community Hero

    If you put all the functions into a script, you can then run the functions at the bottom of that script.

The topic ‘Moving and archiving IIS Logs’ is closed to new replies.