<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Powershell Log File Zipper&#8230;&#8230;.</title>
	<atom:link href="http://www.sporticia.com/blog/microsoft/windows/powershell-log-file-zipper/feed" rel="self" type="application/rss+xml" />
	<link>http://www.sporticia.com/blog/microsoft/windows/powershell-log-file-zipper</link>
	<description>stuff I know, stuff I discover, stuff I see.......just stuff really</description>
	<lastBuildDate>Wed, 01 Feb 2012 11:21:21 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: scottb</title>
		<link>http://www.sporticia.com/blog/microsoft/windows/powershell-log-file-zipper/comment-page-1#comment-15478</link>
		<dc:creator>scottb</dc:creator>
		<pubDate>Thu, 29 Sep 2011 11:09:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.sporticia.com/blog/?p=784#comment-15478</guid>
		<description>Cool !! I left the company shortly after I put that script in, so I never got back round to revising it. Will have a play when I get some free time. Thanks ;op</description>
		<content:encoded><![CDATA[<p>Cool !! I left the company shortly after I put that script in, so I never got back round to revising it. Will have a play when I get some free time. Thanks ;op</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Frank Gallagher</title>
		<link>http://www.sporticia.com/blog/microsoft/windows/powershell-log-file-zipper/comment-page-1#comment-15448</link>
		<dc:creator>Frank Gallagher</dc:creator>
		<pubDate>Wed, 28 Sep 2011 18:26:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.sporticia.com/blog/?p=784#comment-15448</guid>
		<description>We modified the script sleep setting based on the information we found here at the URL below. It checks to make sure the the file is present in the zip before moving on to the next file. This change cut the runtime of the script in half for us.

&quot; http://superuser.com/questions/290461/powershell-zip-file-synchronously

In a nutshell, the idea behind this is testing the output zip archive to see if the file has been added.  If not, sleep for a second and try again.  If the filename does not occur in the archive, then we assume the zipping is still occurring.


This seems to work well.  Maybe to be even more “sure”  we can add one more second for cleanup to occur after the file has been added.&quot;

Our version of the script:

#declare functions here
 function new-zipfile {
 param ($zipfile)
 if (! $zipfile.endswith(‘.zip’)) {$zipfile += ‘.zip’}
set-content $zipfile (“PK” + [char]5 + [char]6 + (“$([char]0)” * 18))
 (dir $zipfile).IsReadOnly = $false
 }
 
#define variables here
#some strings and numbers we will need
$ShellApplication = New-Object -com Shell.Application

$thismonthint = get-date -f “MM”
$prevmonthint = (get-date).addmonths(-1).tostring(“MM”)
 
$thisyearlongint = get-date -f “yyyy”
 
$thislogdir = ‘E:\weblogs\W3SVC2\’
$thiszipfile = $thislogdir + $thisyearlongint + $prevmonthint + ‘.zip’
$zipexists = test-path $thiszipfile
 
#start program here
#first pass, check for .zip files of previous months. if exists exit. if not exist, create empty .zip file
 if (! $zipexists)
 {
 echo ‘zip file does not exist, creating zip file’
new-zipfile $thiszipfile
 }
 else
 {
 return
 }

# move all log files where the month number matches the month number of the .zip file
# Jan = 01, Feb = 02, Mar = 03 etc. etc.
foreach ($file in Get-ChildItem $thislogdir)
 {

# exclude the .zip files already in the directory (just in case we get a random month match in their filename)
 if (! $file.name.endswith(“.zip”))
 {

# if the
 if ($file.Name.substring(6,2) -match $prevmonthint)
 {
 $zipfile = $ShellApplication.NameSpace($thiszipfile)
 $zipfile.MoveHere($file.fullname)
while($zipfile.Items().Item($file.name) -Eq $null)
{
  start-sleep -seconds 1
}
}
}
}</description>
		<content:encoded><![CDATA[<p>We modified the script sleep setting based on the information we found here at the URL below. It checks to make sure the the file is present in the zip before moving on to the next file. This change cut the runtime of the script in half for us.</p>
<p>&#8221; <a href="http://superuser.com/questions/290461/powershell-zip-file-synchronously" rel="nofollow">http://superuser.com/questions/290461/powershell-zip-file-synchronously</a></p>
<p>In a nutshell, the idea behind this is testing the output zip archive to see if the file has been added.  If not, sleep for a second and try again.  If the filename does not occur in the archive, then we assume the zipping is still occurring.</p>
<p>This seems to work well.  Maybe to be even more “sure”  we can add one more second for cleanup to occur after the file has been added.&#8221;</p>
<p>Our version of the script:</p>
<p>#declare functions here<br />
 function new-zipfile {<br />
 param ($zipfile)<br />
 if (! $zipfile.endswith(‘.zip’)) {$zipfile += ‘.zip’}<br />
set-content $zipfile (“PK” + [char]5 + [char]6 + (“$([char]0)” * 18))<br />
 (dir $zipfile).IsReadOnly = $false<br />
 }</p>
<p>#define variables here<br />
#some strings and numbers we will need<br />
$ShellApplication = New-Object -com Shell.Application</p>
<p>$thismonthint = get-date -f “MM”<br />
$prevmonthint = (get-date).addmonths(-1).tostring(“MM”)</p>
<p>$thisyearlongint = get-date -f “yyyy”</p>
<p>$thislogdir = ‘E:\weblogs\W3SVC2\’<br />
$thiszipfile = $thislogdir + $thisyearlongint + $prevmonthint + ‘.zip’<br />
$zipexists = test-path $thiszipfile</p>
<p>#start program here<br />
#first pass, check for .zip files of previous months. if exists exit. if not exist, create empty .zip file<br />
 if (! $zipexists)<br />
 {<br />
 echo ‘zip file does not exist, creating zip file’<br />
new-zipfile $thiszipfile<br />
 }<br />
 else<br />
 {<br />
 return<br />
 }</p>
<p># move all log files where the month number matches the month number of the .zip file<br />
# Jan = 01, Feb = 02, Mar = 03 etc. etc.<br />
foreach ($file in Get-ChildItem $thislogdir)<br />
 {</p>
<p># exclude the .zip files already in the directory (just in case we get a random month match in their filename)<br />
 if (! $file.name.endswith(“.zip”))<br />
 {</p>
<p># if the<br />
 if ($file.Name.substring(6,2) -match $prevmonthint)<br />
 {<br />
 $zipfile = $ShellApplication.NameSpace($thiszipfile)<br />
 $zipfile.MoveHere($file.fullname)<br />
while($zipfile.Items().Item($file.name) -Eq $null)<br />
{<br />
  start-sleep -seconds 1<br />
}<br />
}<br />
}<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: scottb</title>
		<link>http://www.sporticia.com/blog/microsoft/windows/powershell-log-file-zipper/comment-page-1#comment-14140</link>
		<dc:creator>scottb</dc:creator>
		<pubDate>Fri, 16 Sep 2011 06:54:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.sporticia.com/blog/?p=784#comment-14140</guid>
		<description>Glad it helped someone :o)

Must confess I never managed to get it run run properly via the Windows task scheduler though ?!!?? I just used to fire it off on the 1st of each month and let it run :o/</description>
		<content:encoded><![CDATA[<p>Glad it helped someone :o)</p>
<p>Must confess I never managed to get it run run properly via the Windows task scheduler though ?!!?? I just used to fire it off on the 1st of each month and let it run :o/</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Casper</title>
		<link>http://www.sporticia.com/blog/microsoft/windows/powershell-log-file-zipper/comment-page-1#comment-13777</link>
		<dc:creator>Casper</dc:creator>
		<pubDate>Mon, 12 Sep 2011 17:45:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.sporticia.com/blog/?p=784#comment-13777</guid>
		<description>Just what I was looking for!! I incorporated this into a cleanup script that cleans files on multiple servers. By placing it into an if-section that checks if the Microsoft Exchange Information Store Service is running it only zips to iis-logs on that server.</description>
		<content:encoded><![CDATA[<p>Just what I was looking for!! I incorporated this into a cleanup script that cleans files on multiple servers. By placing it into an if-section that checks if the Microsoft Exchange Information Store Service is running it only zips to iis-logs on that server.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mel</title>
		<link>http://www.sporticia.com/blog/microsoft/windows/powershell-log-file-zipper/comment-page-1#comment-7898</link>
		<dc:creator>mel</dc:creator>
		<pubDate>Wed, 09 Mar 2011 02:13:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.sporticia.com/blog/?p=784#comment-7898</guid>
		<description>thank you!!!</description>
		<content:encoded><![CDATA[<p>thank you!!!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

