DEV Community

redhcp
redhcp

Posted on

 

Powershell find size logs and delete

This script is for list or delete logsfiles. You can change parameters to adjust code for You need.

Invoke-Comman for many host

$machines = @(
    'COMPUTER_NAME_1',
    'COMPUTER_NAME_2'
)
foreach ($machine in $machines) {
    Invoke-Command -ComputerName $machine -ScriptBlock {

        Write-Host "Host: $env:COMPUTERNAME ($(Get-Date -Format MM/dd/yyyy)) "  -foregroundcolor "green"

        #LIST SIZE
        $path ='C:\logs'
        $days = -7  #numer days you can preserve in negative - last 7 days
        $t = ((Get-Childitem -Path $path -Recurse -Include *.log  | Where { $_.Length / 1gb -gt 0 } | Measure-Object Length -Sum | Where-Object LastWriteTime -LT (Get-Date).AddDays($days) | select-object -ExpandProperty Sum)/1GB).ToString('0.00')
        Write-host "Total Size: $t GB " -NoNewline -foregroundcolor "yellow"
        Write-host "PATH: $path"


        #DELETE
        $path ='C:\logs'
        $days = -7  #numer days you can preserve in negative - last 7 days
        $t = ((Get-Childitem -Path $path -Recurse -Include *.log  | Where { $_.Length / 1gb -gt 0 } | Measure-Object Length -Sum | Where-Object LastWriteTime -LT (Get-Date).AddDays($days) | select-object -ExpandProperty Sum)/1GB).ToString('0.00') | -Remove-Item -recurse
        Write-host "Total Size: $t GB " -NoNewline -foregroundcolor "yellow"
        Write-host "PATH: $path"

    }
}
Enter fullscreen mode Exit fullscreen mode

LOCAL HOST or Enter-PSSession

Write-Host "Host: $env:COMPUTERNAME ($(Get-Date -Format “MM/dd/yyyy”)) "  -foregroundcolor "green"

        #LIST SIZE
        $path ='C:\logs'
        $days = -7  #numer days you can preserve in negative - last 7 days
        $t = ((Get-Childitem -Path $path -Recurse -Include *.log  | Where { $_.Length / 1gb -gt 0 } | Measure-Object Length -Sum | Where-Object LastWriteTime -LT (Get-Date).AddDays($days) | select-object -ExpandProperty Sum)/1GB).ToString('0.00')
        Write-host "Total Size: $t GB " -NoNewline -foregroundcolor "yellow"
        Write-host "PATH: $path"


        #DELETE
        $path ='C:\logs'
        $days = -7  #numer days you can preserve in negative - last 7 days
        $t = ((Get-Childitem -Path $path -Recurse -Include *.log  | Where { $_.Length / 1gb -gt 0 } | Measure-Object Length -Sum | Where-Object LastWriteTime -LT (Get-Date).AddDays($days) | select-object -ExpandProperty Sum)/1GB).ToString('0.00') | -Remove-Item -recurse
        Write-host "Total Size: $t GB " -NoNewline -foregroundcolor "yellow"
        Write-host "PATH: $path"


Enter fullscreen mode Exit fullscreen mode

Top comments (0)

An Animated Guide to Node.js Event Loop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.