Created
April 8, 2019 12:36
-
-
Save AlexFilipin/c02f5ad9fccecff896b3b4b1fe05670b to your computer and use it in GitHub Desktop.
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
#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-Workflows.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-Workflows.log") | |
$LogPath = "D:\Cleanup-Workflows.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 WFs" | |
$WFs = Search-Resources -XPath "/WorkflowDefinition" -ExpectedObjectType WorkflowDefinition | |
Write-Log -Level Info -Path $LogPath -Message ("Checking usage of " + ($WFs | Measure-Object).Count + " WFs") | |
$Unused_WFs = foreach($WF in $WFs){ | |
$Usage = $false | |
if($MPRs.ActionWorkflowDefinition.Value -contains $WF.ObjectID.Value){ | |
$Usage = $true | |
} | |
if($MPRs.AuthenticationWorkflowDefinition.Value -contains $WF.ObjectID.Value){ | |
$Usage = $true | |
} | |
if($MPRs.AuthorizationWorkflowDefinition.Value -contains $WF.ObjectID.Value){ | |
$Usage = $true | |
} | |
if(-not $Usage){ | |
Write-Log -Level Info -Path $LogPath -Message ("Found unused WF: " + $WF.DisplayName) | |
$WF | |
} | |
} | |
Write-Log -Level Info -Path $LogPath -Message ("Found " + ($Unused_WFs | Measure-Object).Count + " unused WFs") | |
Write-Output "Please select the unused workflows to be deleted." | |
$Delete_WFs = $Unused_WFs | Select-Object -Property DisplayName,Description,CreatedTime,RequestPhase,RunOnPolicyUpdate,ObjectID |Out-GridView -PassThru -Title "Select WFs to delete" | |
Write-Log -Level Info -Path $LogPath -Message ("Selected: " + ($Delete_WFs | Measure-Object).Count + " unused WFs 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_WFs | Measure-Object).Count + " WFs") | |
foreach($WF in $Delete_WFs){ | |
Remove-Resource -ID $WF.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