Skip to content

Instantly share code, notes, and snippets.

@AlexFilipin
Created April 8, 2019 12:36
Show Gist options
  • Save AlexFilipin/99ff7c888608c5163658db683a5fcaf1 to your computer and use it in GitHub Desktop.
Save AlexFilipin/99ff7c888608c5163658db683a5fcaf1 to your computer and use it in GitHub Desktop.
#region functions
function Write-Log
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[Alias("LogContent")]
[string]$Message,
[Parameter(Mandatory=$false)]
[Alias('LogPath')]
[string]$Path='D:\Cleanup-Sets.log',
[Parameter(Mandatory=$false)]
[ValidateSet("Error","Warn","Info")]
[string]$Level="Info",
[Parameter(Mandatory=$false)]
[switch]$NoClobber
)
Begin
{
# Set VerbosePreference to Continue so that verbose messages are displayed.
$VerbosePreference = 'Continue'
}
Process
{
# If the file already exists and NoClobber was specified, do not write to the log.
if ((Test-Path $Path) -AND $NoClobber) {
Write-Error "Log file $Path already exists, and you specified NoClobber. Either delete the file or specify a different name."
Return
}
# If attempting to write to a log file in a folder/path that doesn't exist create the file including the path.
elseif (!(Test-Path $Path)) {
Write-Verbose "Creating $Path."
$NewLogFile = New-Item $Path -Force -ItemType File
}
else {
# Nothing to see here yet.
}
# Format Date for our Log File
$FormattedDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
# Write message to error, warning, or verbose pipeline and specify $LevelText
switch ($Level) {
'Error' {
Write-Error $Message
$LevelText = 'ERROR:'
}
'Warn' {
Write-Warning $Message
$LevelText = 'WARNING:'
}
'Info' {
Write-Verbose $Message
$LevelText = 'INFO:'
}
}
# Write log entry to $Path
"$FormattedDate $LevelText $Message" | Out-File -FilePath $Path -Append
}
End
{
}
}
#endregion
#region variables
#$LogPath = ($PSScriptRoot + "\Cleanup-Sets.log")
$LogPath = "D:\Cleanup-Sets.log"
Write-Log -Level Info -Path $LogPath -Message "Starting Workflow Cleanup"
#endregion
#region main
Write-Log -Level Info -Path $LogPath -Message "Searching all MPRs"
$MPRs = Search-Resources -XPath "/ManagementPolicyRule" -ExpectedObjectType ManagementPolicyRule
Write-Log -Level Info -Path $LogPath -Message "Searching all Sets"
$Sets = Search-Resources -XPath "/Set" -ExpectedObjectType Set
Write-Log -Level Info -Path $LogPath -Message "Searching all Groups"
$Groups = Search-Resources -XPath "/Group[starts-with(Filter, '%')]" -ExpectedObjectType Group
Write-Log -Level Info -Path $LogPath -Message "Searching all WFs"
$WFs = Search-Resources -XPath "/WorkflowDefinition" -ExpectedObjectType WorkflowDefinition
Write-Log -Level Info -Path $LogPath -Message ("Checking usage of " + ($Sets | Measure-Object).Count + " Sets")
$Unused_Sets = foreach($Set in $Sets){
$Usage = $false
if($MPRs.ResourceCurrentSet.Value -contains $Set.ObjectID.Value){
$Usage = $true
}
if($MPRs.ResourceFinalSet.Value -contains $Set.ObjectID.Value){
$Usage = $true
}
if($MPRs.PrincipalSet.Value -contains $Set.ObjectID.Value){
$Usage = $true
}
foreach($Filter in $Groups.Filter){
if($Filter.Contains($Set.ObjectID.Value)){
$Usage = $true
}
}
foreach($Filter in $Sets.Filter){
if($Filter){
if($Filter.Contains($Set.ObjectID.Value)){
$Usage = $true
}
}
}
foreach($XOML in $WFs.XOML){
if($XOML.Contains($Set.ObjectID.Value)){
$Usage = $true
}
}
if(-not $Usage){
Write-Log -Level Info -Path $LogPath -Message ("Found unused Set: " + $Set.DisplayName)
$Set
}
}
Write-Log -Level Info -Path $LogPath -Message ("Found " + ($Unused_Sets | Measure-Object).Count + " unused Sets")
Write-Output "Please select the unused Sets to be deleted."
$Delete_Sets = $Unused_Sets | Select-Object -Property DisplayName,Description,CreatedTime,ObjectID |Out-GridView -PassThru -Title "Select Sets to delete"
Write-Log -Level Info -Path $LogPath -Message ("Selected: " + ($Delete_Sets | Measure-Object).Count + " unused Sets to be deleted")
$choice = ""
while ($choice -notmatch "[y|n]"){
$choice = Read-Host "Please confirm the deletion? (Y/N)"
}
if ($choice -eq "y"){
Write-Log -Level Info -Path $LogPath -Message ("Deleting: " + ($Delete_Sets | Measure-Object).Count + " Sets")
foreach($Set in $Delete_Sets){
Remove-Resource -ID $Set.ObjectID.Value
}
}else{
Write-Log -Level Info -Path $LogPath -Message ("Deletion refused")
}
Write-Log -Level Info -Path $LogPath -Message ("Finished")
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment