$maximumTTL =  [System.TimeSpan]::FromDays(14)  # "P14D"

Import-Module Az.Accounts
Import-Module Az.Automation
Import-Module Az.ServiceBus
Import-Module Az.Resources

$connectionName = "AzureRunAsConnection"
try
{
    # Get the connection "AzureRunAsConnection "
    $servicePrincipalConnection = Get-AutomationConnection -Name $connectionName       
    $account = Connect-AzAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
    Write-Output ("Connected with Automation Account [{0}]" -f $account.Name)
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
}

$subscriptions = Get-AzSubscription

foreach ($subscription in $subscriptions) 
{
    $context = Set-AzContext -SubscriptionId $subscription.Id
    Write-Output ("Checking service bus TTL in Subscription [{0}] with Id [{1}]" -f $subscription.Name, $subscription.Id)

    $servicebusses = Get-AzResource -ResourceType Microsoft.ServiceBus/namespaces
    foreach ($servicebus in $servicebusses) {
       
        Write-Output ("Checking service bus TTL for service bus [{0}] in Subscription [{1}] with Id [{2}]" -f $servicebus.Name, $subscription.Name, $subscription.Id)

        #topics
        $topics = Get-AzServiceBusTopic -Namespace $servicebus.Name -ResourceGroupName $servicebus.ResourceGroupName 

        foreach ($topic in $topics) {

            $currentTTL = [System.Xml.XmlConvert]::ToTimeSpan($topic.DefaultMessageTimeToLive)
            
            if ($currentTTL -gt $maximumTTL) {            
                Write-Output ("Updating TTL on topic [{0}] from {1} to {2}" -f $topic.Name, $currentTTL, $maximumTTL)
                $topic.DefaultMessageTimeToLive = [System.Xml.XmlConvert]::ToString($maximumTTL)
                Set-AzServiceBusTopic -ResourceGroupName $servicebus.ResourceGroupName -Namespace $servicebus.Name -Name $topic.Name -InputObject $topic 
            }

            # topic subscriptions
            $topicSubs = Get-AzServiceBusSubscription -Namespace $servicebus.Name -ResourceGroupName $servicebus.ResourceGroupName -Topic $topic.Name 

            foreach ($topicSub in $topicSubs) {
                
                $currentTTL = [System.Xml.XmlConvert]::ToTimeSpan($topic.DefaultMessageTimeToLive)
                
                if ($currentTTL -gt $maximumTTL) {  
                    Write-Output ("Updating TTL on topic subscription [{0}\{1}] from {2} to {3}" -f $topic.Name, $topicSub.Name, $currentTTL, $maximumTTL)
                    $topicSub.DefaultMessageTimeToLive = [System.Xml.XmlConvert]::ToString($maximumTTL)
                    Set-AzServiceBusSubscription -ResourceGroupName $servicebus.ResourceGroupName -Namespace $servicebus.Name -Topic $topic.Name -Name $topicSub.Name -InputObj $topicSub
                }
            }
        }

        # queues
        $queues = Get-AzServiceBusQueue -Namespace $servicebus.Name -ResourceGroupName $servicebus.ResourceGroupName
        
        foreach ($queue in $queues) {
            
            $currentTTL = [System.Xml.XmlConvert]::ToTimeSpan($queue.DefaultMessageTimeToLive)       

            if ($currentTTL -gt $maximumTTL) {
                Write-Output ("Updating TTL on queue [{0}] from {1} to {2}" -f $queue.Name, $currentTTL, $maximumTTL)
                $queue.DefaultMessageTimeToLive = [System.Xml.XmlConvert]::ToString($maximumTTL)
                Set-AzServiceBusQueue -ResourceGroupName $servicebus.ResourceGroupName -Namespace $servicebus.Name -Name $queue.Name -InputObject $queue
            }

        }
    }
}
