Catch if ZIP errors
-
Monday, November 26, 2012 4:30 PM
I am pretty new to powershell scripting and I came across a potential problem in my future. I have created a script that checks to see if a file is older that 3 months old and, if so, zips it and deletes the original folder. The script works perfectly, but I am afraid that if the zip fails, it will still delete the original folder. I was wondering if there is a way to put a catch in there so that if the zip fails, then it wouldn't remove the folder and it would send an email. Here is what I have so far. Thanks.
ForEach ($i in $dirs)
{
if ($i.PSIsContainer)
{
$folderdate = $i.name
if ($i.name -lt $zipdate)
{
Copy-Item $i "zip$zipname" -Recurse
rm $i -Recurse
}
}
}
write zip directory
Write-Zip -level 9 -path "zip$zipname" -OutputPath "$zipname.zip" -IncludeEmptyDirectories
All Replies
-
Monday, November 26, 2012 4:37 PMModeratorWhat is "write-zip" ?
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
-
Monday, November 26, 2012 4:44 PMIt is part of the Powershell Community Extension. It gives the ability to zip a folder in powershell.
-
Monday, November 26, 2012 4:49 PMModerator
PS C:\> help about_CommonParameters ... PS C:\> help about_try_catch_finally ...
Bill
- Proposed As Answer by Bill_StewartMicrosoft Community Contributor, Moderator Tuesday, November 27, 2012 11:02 PM
- Marked As Answer by Bill_StewartMicrosoft Community Contributor, Moderator Monday, December 31, 2012 5:09 PM

