0
votes

I would like to delete SB topic and their subscription through power-shell script.

I'm able to delete using C# code which available within Microsoft.ServiceBus namespace and NamespaceManager class.

public void DeleteSubscription(string topicPath, string name);

But looking for power-shell script?

enter image description here

I got below script which delete entire SB namespace.

Remove-SBNamespace –Name 'ServiceBusDefaultNamespace' -Force

enter image description here

Please suggest script which can delete individual Topic and their subscription. Thanks!

I thought to use some Azure version command, here is update,

I m using command Remove-AzureRmServiceBusTopic -NamespaceName SB-Example1 -TopicName SB-Topic_exampl1, it's is asking for -ResourceGroup. Now for on prem Service Bus I don't think we can pass any `ResourceGroup, please suggest

1

1 Answers

1
votes

Login using the appropriate method and Select your desired Subscription and ResourceGroup and then proceed with the SB cmdlet.

Login-Subscription-ResourceGroup for powerShell

Write-Host -ForegroundColor Yellow "Login with your Azure Account"
Add-AzureRmAccount
Write-Host -ForegroundColor Yellow "Your available Subscriptions"
Get-AzureRmSubscription | Format-List -Property Name, Id
$subscriptions = @(Get-AzureRmSubscription | Select-Object -ExpandProperty Id)
$flag = $true
while ($flag) {
    Write-Host -ForegroundColor Green 'Enter target Subscription Id: ' -NoNewline
    $subscriptionId = Read-Host
    if ($subscriptions -contains $subscriptionId) {
        Select-AzureRmSubscription -SubscriptionId $subscriptionId
        Write-Output "yes"
        $flag = $false
        break
    }
    elseif ($flag -eq $true) {
        Write-Host "Enter valid Subscription Id"
    }
}
    Write-Host -ForegroundColor Yellow `r`n'Your Available Resource Groups'
    Get-AzureRmResourceGroup | Select-Object -ExpandProperty ResourceGroupName
    $resourceGroups = @(Get-AzureRmResourceGroup | Select-Object -ExpandProperty ResourceGroupName)
    $flag = $true
    while ($flag) {
        Write-Host -ForegroundColor Green `r`n'Enter target Resource Group name: ' -NoNewline
        $resourceGroupName = Read-Host
        if ($resourceGroups -contains $resourceGroupName) {
            Set-AzureRmResourceGroup -Name $resourceGroupName -Tag @{}
            $flag = $false
            break
        }
        elseif ($flag -eq $true) {
            Write-Host `r`n"Enter valid Resource Group name"
        }
    }

For removing Topics use Remove-AzureRmServiceBusTopic you can read about it here and for removing Subscription you can use Remove-AzureRmServiceBusSubscription and there is docs available for that too here.

Note : Make sure you have AzureRm.ServiceBus module installed in the list of Modules, if you dont have then run this from Admin Powershell Install-Module -Name AzureRM.ServiceBus