delete files in windows 2008,powershell 2

The script below reads a file that contains a list of (a server and a list of paths for that server) for a file deletion routine.

All works except time doesn't show in the email.Any help on this please
$Extensions = "*.txt", "*.doc"

$StopWatch = New-Object Diagnostics.StopWatch

$f = [System.IO.File]::OpenText("c:\\temp\\2.csv")
while (! $f.EndOfStream) 
{
        $StopWatch.Reset()
        $StopWatch.Start()

        $BytesDeleted = 0

        $tokens = $f.ReadLine().split(",")
        $server = $tokens[0]
        
        foreach ($folder in $tokens[1].split(";")) 
        {
                $folder = $folder.Replace(':','$')      
                $AdminPath = "\\$Server\$folder"

               Get-ChildItem "$AdminPath\*" -recurse -Include $Extensions | ?{ $_.LastWriteTime -lt (Get-Date).AddDays(-30) } | %{
        
                # Add current size to total size
                $BytesDeleted += $_.Length
        
                # Delete the item
                Remove-Item $_.FullName
        
                }
        
        }

        $StopWatch.Stop()
}

$f.Close()

Open in new window

LVL 3
uma_Asked:
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.

x-menIT super heroCommented:
how are you sending the $StopWatch object to the email body?
0
x-menIT super heroCommented:
$StopWatch.Elapsed
0
uma_Author Commented:
Hi X-men,

Can u please post the entire code...

Thanks,
akiles
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

x-menIT super heroCommented:
here:
$objEmail = New-Object -ComObject "CDO.Message"
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "Your SMTP Server IP Address" 
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendemailaddress") = "Your Senders e-mail Address"
$objEmail.Configuration.Fields.Update
$objEmail.From = "myName@mydomain.some"
$objEmail.To = "to@domain.some"
$objEmail.Subject = "My Subject"
$objEmail.Textbody = " here a bit of texst followed by the stopwath`n"+$StopWatch.Elapsed
$objEmail.Send

Open in new window

0
uma_Author Commented:
see the code i tried and the mail is missing the time.


# Files to delete
$Extensions = "*.bak", "*.tmp"

# Create a StopWatch
$StopWatch = New-Object Diagnostics.StopWatch

# Open the input file
$Report = Import-CSV "C:\tempdelete\psdelete.csv" | %{

# Start the stopwatch
$StopWatch.Reset()
$StopWatch.Start()

$Server = $_.ServerName

# Reset the total size
$BytesDeleted = 0

# Get individual paths and convert to administrative share paths
$_.Paths.Split(";") | %{
$AdminPath = "\\$Server\$($_ -Replace ':\\', '$\')"

# Get files not written for the last 30 days (-30 days from today)
Get-ChildItem "$AdminPath\*" -recurse -Include $Extensions | ?{ $_.LastWriteTime -lt (Get-Date).AddDays(-7) } | %{

# Add current size to total size
$BytesDeleted += $_.Length

# Delete the item
Remove-Item $_.FullName -whatif

}
}

# Stop the stopwatch
$StopWatch.Stop()

# Add an entry to the report

$StopWatch | Select-Object @{n='ServerName';e={ $Server }}, TotalSeconds, @{n='BytesDeleted';e={ $BytesDeleted }}
}

# Convert the report to HTML. This can be made very pretty if required.
$HtmlBody = [String]($Report | ConvertTo-Html)

# E-mail the report as HTML (PowerShell 2.0 CmdLet)
Send-MailMessage -To "support@xxx.com" -From "support@xxx.com" -Subject "Server file deletion report" -Body $HtmlBody -BodyAsHtml -SmtpServer "xxxs-xx-01-mail.xxxxx.local"

Open in new window

mail.JPG
0
x-menIT super heroCommented:
you need to pass the $StopWatch.Elapsed (Elapsed property of the $StopWath object).

PS X:\> $stopWatch | Format-List -Property *
IsRunning           : False
Elapsed             : 00:00:00
ElapsedMilliseconds : 0
ElapsedTicks        : 0

PS X:\> $stopWatch.Start()

PS X:\> $stopWatch | Format-List -Property *
IsRunning           : True
Elapsed             : 00:00:03.3291730
ElapsedMilliseconds : 3329
ElapsedTicks        : 5977771407

PS X:\> $stopWatch.Stop()

PS X:\> $stopWatch | Format-List -Property *
IsRunning           : False
Elapsed             : 00:00:19.5677803
ElapsedMilliseconds : 19567
ElapsedTicks        : 35134732368

PS X:\> $stopWatch.Elapsed
Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 19
Milliseconds      : 567
Ticks             : 195677803
TotalDays         : 0.000226478938657407
TotalHours        : 0.00543549452777778
TotalMinutes      : 0.326129671666667
TotalSeconds      : 19.5677803
TotalMilliseconds : 19567.7803
0
uma_Author Commented:
Can u please tell me how to change my code?
0
x-menIT super heroCommented:
change lines 38-41 to:

# Add an entry to the report 

$TotalSeconds=$StopWatch.ElapsedMilliseconds/1000

$StopWatch | Select-Object @{n='ServerName';e={ $Server }}, @{n='TotalSeconds';e={ $TotalSeconds }}, @{n='BytesDeleted';e={ $BytesDeleted }} 
}

Open in new window

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
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.