Link to home
Start Free TrialLog in
Avatar of uma_
uma_

asked on

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

Avatar of x-men
x-men
Flag of Portugal image

how are you sending the $StopWatch object to the email body?
$StopWatch.Elapsed
Avatar of uma_
uma_

ASKER

Hi X-men,

Can u please post the entire code...

Thanks,
akiles
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

Avatar of uma_

ASKER

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
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
Avatar of uma_

ASKER

Can u please tell me how to change my code?
ASKER CERTIFIED SOLUTION
Avatar of x-men
x-men
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial