Created
January 8, 2019 16:01
-
-
Save pwahlmueller/57cbfd5c8822f733acbfa1c7bcb8fad5 to your computer and use it in GitHub Desktop.
This file contains 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
### | |
### 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