Powershell Pipeline Out-File/Log File

Hello,
I need assistance creating/logging to an outfile via a powershell pipeline. The code I've written should parse the child folders in $path var to an array, then delete nested folders from $path$dir older than x which in this instance is 60 days. What I need help with is creating a log file to keep track of what was deleted. With my limited powershell knowledge I am unable to figure out a proper way to output 1. the path of the deleted folders and 2. getting output for all events taking place in the loop/array using the $log_file var.


$date = (get-date -format yyyyMMdd)
$log_file = "backup_cleanup_$(get-date -format yyyyMMdd).log"
$path = "C:\Program Files\Backup\"
$file_year = get-date -format yyyy\"_*"
$arr = Get-ChildItem $path | 
       Where-Object {$_.PSIsContainer} | 
       Foreach-Object {$_.Name}
	   
	ForEach ($dir in $arr)
	{
	Get-ChildItem $path$dir"\" | Where {$_.PSIsContainer -and `
	$_.LastWriteTime -le (get-date).adddays(-60)} | Remove-Item -recurse -whatif
	}

Open in new window


Thanks in advance!
dc3sopsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
Try the following.  I assume you've got some other purpose for $date and $file_year so I left them in there.
$date = (get-date -format yyyyMMdd)
$log_file = "backup_cleanup_$(get-date -format yyyyMMdd).log"
$path = "C:\Program Files\Backup\"
$file_year = get-date -format yyyy\"_*"
Get-ChildItem $path | 
 Where-Object {$_.PSIsContainer} | 
 Foreach-Object {$_.FullName} |
 ForEach-Object {
	Get-ChildItem $_ | Where {$_.PSIsContainer -and `
	$_.LastWriteTime -le (Get-Date).adddays(-60)}
 } |
 Select -expandProperty FullName |
 Tee-Object -FilePath $log_file |
 Remove-Item -recurse -whatif

Open in new window

0
dc3sopsAuthor Commented:
Thanks, footech. That worked beautifully.

One more question, is there a way I can add a header message, something like "---- folders removed ----" at the top of the log before the input from remove-item?
0
footechCommented:
If you had PS 3.0 would be easiest, then we could make use of the -append parameter for Tee-Object, and we would use a Set-Content command to write the header at the beginning of the script.  Here's a way we could do it with PS 2.0.
$date = (get-date -format yyyyMMdd)
$log_file = "backup_cleanup_$(get-date -format yyyyMMdd).log"
$path = "C:\Program Files\Backup\"
$file_year = get-date -format yyyy\"_*"
Set-Content "---- folders removed ----" -path $log_file
Get-ChildItem $path | 
 Where-Object {$_.PSIsContainer} | 
 Foreach-Object {$_.FullName} |
 ForEach-Object {
	Get-ChildItem $_ | Where {$_.PSIsContainer -and `
	$_.LastWriteTime -le (Get-Date).adddays(-60)}
 } |
 Select -expandProperty FullName |
 Tee-Object -Variable out |
 Remove-Item -recurse -whatif
$out | Add-Content $log_file

Open in new window

0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

dc3sopsAuthor Commented:
I have PS 3.0. I've done something similar as you suggest. The only problem is that I get NUL chars in between text chars in the log file.

$current_time = (get-date -format yyyyMMdd\_HHmm)
$log_file = "backup_cleanup_$(get-date -format yyyyMMdd).log"
$path = "C:\Program Files\Backup\"
Set-Content "--- Folders Removed from $($path) on $($current_time) ----" -path $log_file
Get-ChildItem $path | 
 Where-Object {$_.PSIsContainer} | 
 Foreach-Object {$_.FullName} |
 ForEach-Object {
	Get-ChildItem $_ | Where {$_.PSIsContainer -and `
	$_.LastWriteTime -le (Get-Date).adddays(-30)}
 } |
 Select -expandProperty FullName |
 Tee-Object -FilePath $log_file -append |
 Remove-Item -recurse -whatif

Open in new window

0
footechCommented:
Can you post an example?
0
dc3sopsAuthor Commented:
Attached is a screenshot. I'm using notepad++ to view the log but with notepad there is a space between each char. Also attached.
nul-chars.png
nul-char2.png
0
footechCommented:
Interesting!  I get that when I use the -append parameter, but not when I use the code in http:#a39500138
I'll see if I can find a reason for it, but if not then the alternative works well.
0
footechCommented:
Appears to be an issue with encoding.  Add the -encoding parameter to Set-Content and set it to Unicode.
Haven't fully investigated but here's my understanding:
 By default PS uses Unicode internally.  The default encoding for Set-Content is ASCII.  By the time we got to Tee-Object, it was outputting Unicode, hence the odd result.  If I'd used Out-File instead of Set-Content we wouldn't have ever seen this since its default is Unicode.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dc3sopsAuthor Commented:
Excellent! That solved the formatting issue. Thanks so much for your help on this. I've increased the points because you've been such a huge help!
0
dc3sopsAuthor Commented:
Stuck with the problem until all issues with PS 3.0 script were resolved even though he provided an optional working solution for PS 2.0.
0
footechCommented:
Thanks for the feedback.
Cheers!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.