I have a need to compress some of the data I am exporting using PowerShell so I figured why not use PowerShell to do it? I did not want to have a dependency on the Community Extensions even though they have solved this problem already. Luckily in newer versions of .Net there is the System.IO.Compression class.
Here is my code which is based off of the Microsoft example for the class. It should work for simple cases or perhaps serve as a starting point for your enhancements.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
| # TODO: Use buffering to see if performance can be improved.
function Gzip-FileSimple
{
param
(
[String]$inFile = $(throw "Gzip-File: No filename specified"),
[String]$outFile = $($inFile + ".gz"),
[switch]$delete # Delete the original file
)
trap
{
Write-Host "Received an exception: $_. Exiting."
break
}
if (! (Test-Path $inFile))
{
"Input file $inFile does not exist."
exit 1
}
Write-Host "Compressing $inFile to $outFile."
$input = New-Object System.IO.FileStream $inFile, ([IO.FileMode]::Open), ([IO.FileAccess]::Read), ([IO.FileShare]::Read)
$buffer = New-Object byte[]($input.Length)
$byteCount = $input.Read($buffer, 0, $input.Length)
if ($byteCount -ne $input.Length)
{
$input.Close()
Write-Host "Failure reading $inFile."
exit 2
}
$input.Close()
$output = New-Object System.IO.FileStream $outFile, ([IO.FileMode]::Create), ([IO.FileAccess]::Write), ([IO.FileShare]::None)
$gzipStream = New-Object System.IO.Compression.GzipStream $output, ([IO.Compression.CompressionMode]::Compress)
$gzipStream.Write($buffer, 0, $buffer.Length)
$gzipStream.Close()
$output.Close()
if ($delete)
{
Remove-Item $inFile
}
} |
If you are anywhere around Ohio and interested in Linux you may want to check out Ohio LinuxFest 2009. It is being held September 25th & 26th in Columbus Ohio.
This will be my first year attending, but I have heard good things about the conference and am looking forward to talking with some other Open Source enthusiasts in the area.
I just finished attending my first PyOhio and I have to say that I enjoyed it. It was interesting to see so many people involved enough in their interests to spend a weekend talking about code. There were sessions geared to all levels of Python knowledge and a wealth of knowledge to be shared.
I am a Python novice so my ability to hang in on the deeper side discussions was limited, but I came away with much more information and a bunch of areas to dig into deeper.
If you are in the Ohio area and even slightly interested in Python I would recommend you keep an eye out for PyOhio 2010.
As part of our auditing process I need validate guest disk persistence settings. Here is a simple snippet to get the needed data.
1
2
3
4
| Get-VM | ForEach-Object {
$vmname = $_.Name
$_.HardDisks | Select-Object @{Name="VM"; Expression={$vmname}}, Filename, Persistence
} |
I am fleshing out my automated monitoring/reporting system and I wanted to include some performance data exports. As part of this I always wanted my weekly exports to start on a certain day of the week no matter when the script was run. I also needed to make sure that I received a weeks worth of data.
To help out I wrote this function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
| # Find the date of a previous weekday. Originally conceived to export performance
# from a give day of the week no matter when it was called.
function Get-PreviousWeekdayDate
{
param
(
[DateTime]$date = $(Get-Date),
[System.DayOfWeek]$weekDay = $([System.DayOfWeek]::Sunday), # Full weekday names work here.
[switch]$fullWeek # The return date must be a full week ago.
)
Set-Variable -Name DAYS_IN_WEEK -Value 7 -Option Constant
$dayDelta = 0 # The difference in days between $date and the target day.
# If we are further in the week then we just go back x days.
if ([Int32]$date.DayOfWeek -ge [Int32]$weekDay)
{
$dayDelta = [Int32]$date.DayOfWeek - [Int32]$weekDay
}
# Otherwise we need wrap around the week
else
{
$dayDelta = $DAYS_IN_WEEK + [Int32]$date.DayOfWeek - [Int32]$weekDay
}
$date = $date.AddDays(-$dayDelta)
# Go back 7 days
if ($fullWeek)
{
$date = $date.AddDays(-$DAYS_IN_WEEK)
}
$date
} |