Scripts/Remove-BPAObject.ps1
function Remove-BPAObject { <# .SYNOPSIS Removes an AutoMate BPA object. .DESCRIPTION Remove-BPAObject receives BPA object(s) on the pipeline, or via the parameter $InputObject, and deletes the object(s). .PARAMETER InputObject The object(s) to be deleted. .INPUTS The following objects can be disabled by this function: Workflow Task Condition Process TaskAgent ProcessAgent AgentGroup User UserGroup .EXAMPLE # Deletes agent "agent01" Get-BPAAgent "agent01" | Remove-BPAObject .NOTES Author(s): : David Seibel Contributor(s) : Date Created : 09/12/2016 Date Modified : 05/01/2018 .LINK https://github.com/davidseibel/PoshBPA #> [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='High')] param( [Parameter(ValueFromPipeline = $true)] $InputObject ) BEGIN { $workflowCache = @{} $taskCache = @{} $processCache = @{} $conditionCache = @{} $userCache = @{} $userGroupCache = @{} $agentCache = @{} $agentGroupCache = @{} } PROCESS { foreach ($obj in $InputObject) { Write-Verbose "Processing object Name: '$($obj.Name)' Type: '$($obj.TypeName)'" if (-not $workflowCache.ContainsKey($obj.BPAServer)) { Write-Verbose "Caching workflow objects for server $($obj.BPAServer) for better performance" $workflowCache.Add($obj.BPAServer, (Get-BPAWorkflow -BPAServer $obj.BPAServer)) } # Check if there are any dependencies on the object, and warn user switch ($obj.TypeName) { {($_ -in @("Workflow","Task","Process","Condition"))} { foreach ($workflow in $workflowCache[$obj.BPAServer]) { if (($workflow.Triggers.ConstructID -contains $obj.ID) -or ($workflow.Items.ConstructID -contains $obj.ID)) { Write-Warning "$($obj.TypeName) '$($obj.Name)' is used in workflow '$($workflow.Name)'" } } } {($_ -in @("Agent","AgentGroup"))} { foreach ($workflow in $workflowCache[$obj.BPAServer]) { if (($workflow.Triggers.AgentID -contains $obj.ID) -or ($workflow.Items.AgentID -contains $obj.ID)) { Write-Warning "$($obj.TypeName) '$($obj.Name)' is used in workflow '$($workflow.Name)'" } } } "Folder" { if (-not $taskCache.ContainsKey($obj.BPAServer)) { Write-Verbose "Caching task objects for server $($obj.BPAServer) for better performance" $taskCache.Add($obj.BPAServer, (Get-BPATask -BPAServer $obj.BPAServer)) } if (-not $processCache.ContainsKey($obj.BPAServer)) { Write-Verbose "Caching process objects for server $($obj.BPAServer) for better performance" $processCache.Add($obj.BPAServer, (Get-BPAProcess -BPAServer $obj.BPAServer)) } if (-not $conditionCache.ContainsKey($obj.BPAServer)) { Write-Verbose "Caching condition objects for server $($obj.BPAServer) for better performance" $conditionCache.Add($obj.BPAServer, (Get-BPACondition -BPAServer $obj.BPAServer)) } if (-not $userCache.ContainsKey($obj.BPAServer)) { Write-Verbose "Caching user objects for server $($obj.BPAServer) for better performance" $userCache.Add($obj.BPAServer, (Get-BPAUser -BPAServer $obj.BPAServer)) } if (-not $userGroupCache.ContainsKey($obj.BPAServer)) { Write-Verbose "Caching user group objects for server $($obj.BPAServer) for better performance" $userGroupCache.Add($obj.BPAServer, (Get-BPAUserGroup -BPAServer $obj.BPAServer)) } if (-not $agentCache.ContainsKey($obj.BPAServer)) { Write-Verbose "Caching agent objects for server $($obj.BPAServer) for better performance" $agentCache.Add($obj.BPAServer, (Get-BPAAgent -BPAServer $obj.BPAServer)) } if (-not $agentGroupCache.ContainsKey($obj.BPAServer)) { Write-Verbose "Caching agent group objects for server $($obj.BPAServer) for better performance" $agentGroupCache.Add($obj.BPAServer, (Get-BPAAgentGroup -BPAServer $obj.BPAServer)) } if ($obj.Path -like "\WORKFLOWS*") { foreach ($workflow in $workflowCache[$obj.BPAServer]) { if ($workflow.ParentID -eq $obj.ID) { Write-Warning "Folder '$(Join-Path -Path $obj.Path -ChildPath $obj.Name)' contains workflow '$($workflow.Name)'" } } foreach ($user in $userCache[$obj.BPAServer]) { if ($user.WorkflowFolderID -eq $obj.ID) { Write-Warning "Folder '$(Join-Path -Path $obj.Path -ChildPath $obj.Name)' is the workflow user folder for '$($user.Name)'" } } } if ($obj.Path -like "\TASKS*") { foreach ($task in $taskCache[$obj.BPAServer]) { if ($task.ParentID -eq $obj.ID) { Write-Warning "Folder '$(Join-Path -Path $obj.Path -ChildPath $obj.Name)' contains task '$($task.Name)'" } } foreach ($user in $userCache[$obj.BPAServer]) { if ($user.TaskFolderID -eq $obj.ID) { Write-Warning "Folder '$(Join-Path -Path $obj.Path -ChildPath $obj.Name)' is the task user folder for '$($user.Name)'" } } } if ($obj.Path -like "\CONDITIONS*") { foreach ($condition in $conditionCache[$obj.BPAServer]) { if ($condition.ParentID -eq $obj.ID) { Write-Warning "Folder '$(Join-Path -Path $obj.Path -ChildPath $obj.Name)' contains condition '$($condition.Name)'" } } foreach ($user in $userCache[$obj.BPAServer]) { if ($user.ConditionFolderID -eq $obj.ID) { Write-Warning "Folder '$(Join-Path -Path $obj.Path -ChildPath $obj.Name)' is the condition user folder for '$($user.Name)'" } } } if ($obj.Path -like "\PROCESSES*") { foreach ($process in $processCache[$obj.BPAServer]) { if ($process.ParentID -eq $obj.ID) { Write-Warning "Folder '$(Join-Path -Path $obj.Path -ChildPath $obj.Name)' contains process '$($process.Name)'" } } foreach ($user in $userCache[$obj.BPAServer]) { if ($user.ProcessFolderID -eq $obj.ID) { Write-Warning "Folder '$(Join-Path -Path $obj.Path -ChildPath $obj.Name)' is the process user folder for '$($user.Name)'" } } } if ($obj.Path -like "\USERS*") { foreach ($user in $userCache[$obj.BPAServer]) { if ($user.ParentID -eq $obj.ID) { Write-Warning "Folder '$(Join-Path -Path $obj.Path -ChildPath $obj.Name)' contains user '$($user.Name)'" } } } if ($obj.Path -like "\USERGROUPS*") { foreach ($userGroup in $userGroupCache[$obj.BPAServer]) { if ($userGroup.ParentID -eq $obj.ID) { Write-Warning "Folder '$(Join-Path -Path $obj.Path -ChildPath $obj.Name)' contains user group '$($userGroup.Name)'" } } } if ($obj.Path -like "\TASKAGENTS*") { foreach ($agent in $agentCache[$obj.BPAServer] | Where-Object {$_.AgentType -eq [BPAAgentType]::TaskAgent}) { if ($agent.ParentID -eq $obj.ID) { Write-Warning "Folder '$(Join-Path -Path $obj.Path -ChildPath $obj.Name)' contains agent '$($agent.Name)'" } } } if ($obj.Path -like "\PROCESSAGENTS*") { foreach ($agent in $agentCache[$obj.BPAServer] | Where-Object {$_.AgentType -eq [BPAAgentType]::ProcessAgent}) { if ($agent.ParentID -eq $obj.ID) { Write-Warning "Folder '$(Join-Path -Path $obj.Path -ChildPath $obj.Name)' contains agent '$($agent.Name)'" } } } if ($obj.Path -like "\AGENTGROUPS*") { foreach ($agentGroup in $agentGroupCache[$obj.BPAServer]) { if ($agentGroup.ParentID -eq $obj.ID) { Write-Warning "Folder '$(Join-Path -Path $obj.Path -ChildPath $obj.Name)' contains user group '$($agentGroup.Name)'" } } } } "User" { # Do nothing } "UserGroup" { if ($obj.UserIDs.Count -gt 0) { $systemPermission = $obj | Get-BPASystemPermission if ($null -ne $systemPermission) { if (-not $userCache.ContainsKey($obj.BPAServer)) { Write-Verbose "Caching user objects for server $($obj.BPAServer) for better performance" $userCache.Add($obj.BPAServer, (Get-BPATask -BPAServer $obj.BPAServer)) } Write-Warning "User Group '$($obj.Name)' has a system permission defined!" foreach ($user in $userCache[$obj.BPAServer]) { if ($user.ID -in $obj.UserIDs) { Write-Warning "User Group '$($obj.Name)' contains user '$($user.Name)'" } } } } } default { Write-Error -Message "Unsupported input type '$($obj.TypeName)' encountered!" -TargetObject $obj } } if ($PSCmdlet.ShouldProcess($obj.BPAServer, "Removing $($obj.TypeName): $(Join-Path -Path $obj.Path -ChildPath $obj.Name)")) { Write-Verbose "Deleting object '$($obj.Name) (Type: $($obj.TypeName))'." Invoke-BPARestMethod -Resource "$([BPATypeDictionary]::($obj.TypeName).RestResource)/$($obj.ID)/delete" -RestMethod Post -BPAServer $obj.BPAServer } } } } |