Skip to content

Instantly share code, notes, and snippets.

@pwahlmueller
Created January 8, 2019 16:01
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 pwahlmueller/57cbfd5c8822f733acbfa1c7bcb8fad5 to your computer and use it in GitHub Desktop.
Save pwahlmueller/57cbfd5c8822f733acbfa1c7bcb8fad5 to your computer and use it in GitHub Desktop.
###
### Remove deleted ConfigItems 45 days after deletion
###
### v1.0.0.0 Wahlpatr
###
$hostname=$env:COMPUTERNAME
$deleted=invoke-command -computername scsmserver -scriptblock {
try {
import-module smlets
$class = Get-SCSMClass ^system.configitem$
$delEnumId=(Get-SCSMEnumeration System.ConfigItem.ObjectStatusEnum.PendingDelete).id
$objectsToDelete = Get-SCSMObject -class $class -filter "objectstatus -eq '$delenumid' -and LastModified -lt '$((Get-Date).Adddays(-45))'"
$objectsToDelete | Remove-SCSMObject -Force
$objectsToDelete.count | select @{n='DeletedItems';e={$_}}
}
catch
{
Write-EventLog -LogName Application -Source SCSMDeleteObjects -Message "Remove Deleted ConfigItems failed on $env:ComputerName started from $Using:hostname. ErrorMessage:$_" -EventID 1003 -EntryType Error
}
}
$deleted | % {$d=$_.deletedItems;$s=$_.PSComputerName; Write-EventLog -LogName Application -Source SCSMDeleteObjects -Message "Removed $d Deleted Items from $s ConfigItems." -EventID 8 -EntryType Information }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment