There was a breaking change in the Console 2.1, if you’re using version 2.1 or newer use Download-File commandlet instad of the “Get-File” as shown in the code below.

LogFilesYou might have have found yourself hunting around in the Sitecore interface for something that would allow you to download all the the log files in a fast and convenient way once time or another. Have you found one? Me neither? but luckily I had the PowerShell console installed on my server so I started looking for a script to zip all files in a folder and luckily because we have a full PowerShell power in the box I could stand on the shoulders of giants and get the zipping part from Stack Overflow ? again (which is the majority of my script. The rest was super easy ? just call the function and download the file?

The only meaningful lines other than the copied function that I needed to use was calling it (?ZipFiles? – naturally) and then calling the new commandlet Get-File (that was added in the version 2.0 of the console). Obviously it?s good to let the user know what?s going on and cleaning up after yourself ? hence the furniture code around those.

Easy peasy? 

Cognifide?s PowerShell Console 2.0 available on Sitecore Marketplace. Go get your copy and? Happy scripting!

 

Now the script looks as follows

###########################################################################
#                                                                         #
# The script zips all log4Net files and allows users to download the zip. #
# It will show errors for logs currently opened by Sitecore for writing.  #
#                                                                         #
###########################################################################

#
# The ZipFiles function is based on noam's answer
# on the following Stack Overflow's page: http://bit.ly/PsZip
#
function ZipFiles( $zipArchive, $sourcedir )
{
    [System.Reflection.Assembly]::Load("WindowsBase,Version=3.0.0.0, `
        Culture=neutral, PublicKeyToken=31bf3856ad364e35") | Out-Null
    $ZipPackage=[System.IO.Packaging.ZipPackage]::Open($zipArchive, `
        [System.IO.FileMode]::OpenOrCreate, [System.IO.FileAccess]::ReadWrite)
    $in = gci $sourceDir | select -expand fullName
    [array]$files = $in -replace "C:","" -replace "\\","/"
    ForEach ($file In $files) {
        $fileName = [System.IO.Path]::GetFileName($file);
            $partName=New-Object System.Uri($file, [System.UriKind]::Relative)
            $part=$ZipPackage.CreatePart("/$fileName", "application/zip", `
                [System.IO.Packaging.CompressionOption]::Maximum)
            Try{
                $bytes=[System.IO.File]::ReadAllBytes($file)
            }Catch{
                $_.Exception.ErrorRecord.Exception
            }
            $stream=$part.GetStream()
            $stream.Write($bytes, 0, $bytes.Length)
            $stream.Close()
    }
    $ZipPackage.Close()
}

# Get Sitecore folders and format the zip file name
$dateTime = Get-Date -format "yyyy-MM-d_hhmmss"
$dataFolder = [Sitecore.Configuration.Settings]::DataFolder
$logsFolder = [Sitecore.Configuration.Settings]::LogFolder
$myZipFile = "$dataFolder\logs-$datetime.zip"

# Warn that the used log files will fail zipping
Write-Host -f Yellow "Zipping files locked by Sitecore will fail." -n
Write-Host -f Yellow "Files listed below were used."

# Zip the log files
ZipFiles $myZipFile $LogsFolder

#Download the zipped logs
Get-File -FullName $myZipFile | Out-Null

#Delete the zipped logs from the server
Remove-Item $myZipFile

PS. The hardest part of the blog was to find and theme a nice image for it Puszczam oczko

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...



This entry (Permalink) was posted on Monday, July 8th, 2013 at 9:00 am and is filed under .Net Framework, Open Source, PowerShell, Sitecore, Software Development, Web applications. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response , or trackback from your own site.