Created
July 23, 2019 23:47
-
-
Save steviecoaster/f1eb6eee84a2886eb339a80d5710621b to your computer and use it in GitHub Desktop.
Remove a chocolatey log that has grown unruly in size
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Remove-ChocoLog { | |
<# | |
.SYNOPSIS | |
Remove a chocolatey log from a system | |
.DESCRIPTION | |
Based on size threshold, will remove a chocolatey.log file from the specified system(s). | |
.PARAMETER LogThresholdMB | |
Size in MB to determine whether or not to prune a log | |
.PARAMETER Computername | |
The remote computer you wish to query against | |
.EXAMPLE | |
Remove-ChocoLog -LogThresholdMB 12 | |
.EXAMPLE | |
Remove-ChocoLog -LogThresholdMB 5 -Computername Wkstn02 | |
#> | |
[cmdletBinding(SupportsShouldProcess,ConfirmImpact="High")] | |
param( | |
[Parameter(Mandatory)] | |
[Int] | |
$LogThresholdMB, | |
[Parameter()] | |
[String[]] | |
$Computername = $env:COMPUTERNAME | |
) | |
function Get-ChocoLogSize { | |
[cmdletBinding()] | |
param( | |
[Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName)] | |
[String[]] | |
$Computername = $env:COMPUTERNAME | |
) | |
$params = @{ | |
Computername = $Computername | |
Scriptblock = { [Math]::Round(((Get-ChildItem "$env:ChocolateyInstall\logs\chocolatey.log").Length /1MB),2) } | |
} | |
Invoke-Command @params | |
} | |
$logsize = Get-ChocoLogSize | |
if($logsize -gt $LogThresholdMB){ | |
If($PSCmdlet.ShouldProcess("Removing log from $($Computername)")){ | |
$removalParams = @{ | |
Computername = $Computername | |
Scriptblock = { Remove-Item $env:ChocolateyInstall\logs\chocolatey.log } | |
} | |
Invoke-Command @removalParams | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment