• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 519
  • Last Modified:

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

0
uma_
Asked:
uma_
  • 5
  • 3
1 Solution
 
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
SMB Security Just Got a Layer Stronger

WatchGuard acquires Percipient Networks to extend protection to the DNS layer, further increasing the value of Total Security Suite.  Learn more about what this means for you and how you can improve your security with WatchGuard today!

 
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

Featured Post

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.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now