?
Solved

Powershell if statement ending

Posted on 2009-04-14
5
Medium Priority
?
1,060 Views
Last Modified: 2012-05-06
I have written a script which uses and if statement and a "foreach" statement.  In that process I sending output to a file.  At the end of the script I want to delete the file but the script says the file is still in use and cannot be deleted.
I am new to powershell (I have worked with perl in the past) and would like to know how to end the foreach process and close output to the file so that it can be deleted.  I am including a snipit? of the code so you can see where I am at and tell me how to close the output file.
The last line is where the file open error occurs.

I am emailing that .txt file to a group first and then I need to delete it.  I only included the attachment part of the email scripting.
"Starting PowerLogRoll @ $(get-date)" | out-file "C:\Log Rollup\rollup-log.txt"
 
$logs = get-childitem C:\VoiceNet\Log
 
$year = get-date -f "yyyy"
$month = get-date -f "MM"
$day = get-date -f "dd"
 
foreach ($file in $logs) {
    $file | out-file "C:\Log Rollup\rollup-log.txt" -append
    if ($file.CreationTime -lt ($(Get-Date).AddMonths(-1))) {
    c:\7z\7za a "c:\log rollup\$month-$day-$year.zip" $file
    remove-item -force $file
}
}
$mailmessage.Attachments.Add('C:\Log Rollup\rollup-log.txt')
 
remove-item -force "C:\Log Rollup\rollup-log.txt"

Open in new window

0
Comment
Question by:Mhorrocks1962
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 5

Expert Comment

by:xtravagan
ID: 24144066
If you remove the mailMessage attachment line, do you still get the locked error?
0
 

Author Comment

by:Mhorrocks1962
ID: 24144302
Yes.  I took out all the mail lines and still get the error that the file is in use.  I believe the file has to be closed somewhere in the script, but don't know how to do that in powershell.  Once it is closed it will allow me to delete, just need to know how to close the file in the script.

Thanks
0
 
LVL 9

Accepted Solution

by:
Sander Stad earned 2000 total points
ID: 24145032
I changed it a little and I get no errors that the file is loched:

"Starting PowerLogRoll @ $(get-date)" | out-file "C:\Test\Powershell\XX_03\Log Rollup\rollup-log.txt"
 
$logs = get-childitem "C:\Test\Powershell\XX_03\Logs"
 
$year = get-date -f "yyyy"
$month = get-date -f "MM"
$day = get-date -f "dd"
 
function out-zip {
  Param([string]$path)
 
  if (-not $path.EndsWith('.zip')) {$path += '.zip'}
 
  if (-not (test-path $path)) {
    set-content $path ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
  }
  $ZipFile = (new-object -com shell.application).NameSpace($path)
  $input | foreach {$zipfile.CopyHere($_.fullname)}
} 
 
foreach ($file in $logs) {
    $file | out-file "C:\Test\Powershell\XX_03\Log Rollup\rollup-log.txt" -append
    if ($file.CreationTime -lt ($(Get-Date).AddMonths(-1))) {
    	$file | out-zip "C:\Test\Powershell\XX_03\Log Rollup\$month-$day-$year.zip"
    	remove-item $file -force 
	}
}
 
$mailmessage.Attachments.Add('C:\Test\Powershell\XX_03\Log Rollup\rollup-log.txt')
 
remove-item -force "C:\Test\Powershell\XX_03\Log Rollup\rollup-log.txt"

Open in new window

0
 

Author Comment

by:Mhorrocks1962
ID: 24146110
I will get this tested and get back to you.  Looks like it will work though.

I appreciate your assistance on this and will let you know tomorrow night after I test.
0
 

Author Closing Comment

by:Mhorrocks1962
ID: 31570242
Excellent.  Even though my background was more perl, It was easy to take what you provided and apply it to my powershell script and make it work.
Thank you very much.  When asked for a full solution, you obviously know what is being asked and you definitely deserve the points.
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question