Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Using if after if in my situation

$
0
0

Hello, guys,

I wrote script to get items and remove it older than 180 days. Actually after that, it forms another log file.

$CurrentTime = "["+(Get-Date)+"]`t"
$directory = "E:\IISLogs\W3SVC1" #type directory on which log files will be removed
$logdate = Get-date
$days="180"
$LastWrite = $logdate.AddDays(-$Days)

$forlogs = Get-ChildItem -Path $directory -recurse

foreach ($files in $forlogs)
   
   {
   
    if ($files.LastWriteTime -lt $LastWrite)
     
    
        {
              
         Remove-item $files.FullName
     
         Out-file -FilePath "E:\IISLogs\W3SVC1\ISSLogRemoval.log" -InputObject "$CurrentTime Log file named: $files succesfully removed on machine > $env:computername" -Append #log file dedicated to monitor logs deletion process.

             
        }
    }

How actually i can use If after if ($files.LastWriteTime -lt $LastWrite) to remove my created log file then it reaches 100mb.?

Its something like that, but how i can reuse it after my script in if or after if. :

$SizeMb="{0:N2}" -f ($size.sum / 1MB) + "MB"

$SizeMax = 100
$Size = (Get-ChildItem $directory| Measure-Object -property length -sum)
$SizeMb="{0:N2}" -f ($size.sum / 1MB) + "MB"
 
if ($sizeMb -ge $sizeMax)

{
 
Get-ChildItem $directory -Recurse | Remove-Item –Force
 
}


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>