Skip to content

Instantly share code, notes, and snippets.

@steviecoaster
Created July 23, 2019 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steviecoaster/f1eb6eee84a2886eb339a80d5710621b to your computer and use it in GitHub Desktop.
Save steviecoaster/f1eb6eee84a2886eb339a80d5710621b to your computer and use it in GitHub Desktop.
Remove a chocolatey log that has grown unruly in size
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