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.
You 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?
Now the script looks as follows
1###########################################################################
2# #
3# The script zips all log4Net files and allows users to download the zip. #
4# It will show errors for logs currently opened by Sitecore for writing. #
5# #
6###########################################################################
7
8#
9# The ZipFiles function is based on noam's answer
10# on the following Stack Overflow's page: http://bit.ly/PsZip
11#
12function ZipFiles( $zipArchive, $sourcedir )
13{
14 [System.Reflection.Assembly]::Load("WindowsBase,Version=3.0.0.0, `
15 Culture=neutral, PublicKeyToken=31bf3856ad364e35") | Out-Null
16 $ZipPackage=[System.IO.Packaging.ZipPackage]::Open($zipArchive, `
17 [System.IO.FileMode]::OpenOrCreate, [System.IO.FileAccess]::ReadWrite)
18 $in = gci $sourceDir | select -expand fullName
19 [array]$files = $in -replace "C:","" -replace "\\","/"
20 ForEach ($file In $files) {
21 $fileName = [System.IO.Path]::GetFileName($file);
22 $partName=New-Object System.Uri($file, [System.UriKind]::Relative)
23 $part=$ZipPackage.CreatePart("/$fileName", "application/zip", `
24 [System.IO.Packaging.CompressionOption]::Maximum)
25 Try{
26 $bytes=[System.IO.File]::ReadAllBytes($file)
27 }Catch{
28 $_.Exception.ErrorRecord.Exception
29 }
30 $stream=$part.GetStream()
31 $stream.Write($bytes, 0, $bytes.Length)
32 $stream.Close()
33 }
34 $ZipPackage.Close()
35}
36
37# Get Sitecore folders and format the zip file name
38$dateTime = Get-Date -format "yyyy-MM-d_hhmmss"
39$dataFolder = [Sitecore.Configuration.Settings]::DataFolder
40$logsFolder = [Sitecore.Configuration.Settings]::LogFolder
41$myZipFile = "$dataFolder\logs-$datetime.zip"
42
43# Warn that the used log files will fail zipping
44Write-Host -f Yellow "Zipping files locked by Sitecore will fail." -n
45Write-Host -f Yellow "Files listed below were used."
46
47# Zip the log files
48ZipFiles $myZipFile $LogsFolder
49
50#Download the zipped logs
51Get-File -FullName $myZipFile | Out-Null
52
53#Delete the zipped logs from the server
54Remove-Item $myZipFile
PS. The hardest part of the blog was to find and theme a nice image for it
Comments