Hi Jeff
Thanks for asking you questions!
You can achieve by fetching the priorities first and check-in if the priority exists.
$resourceGroup = "yourResourceGroup"
$nsgName = "yourNsgName"
$newRuleName = "newRuleName"
$newPriority = 200
$nsg = Get-AzNetworkSecurityGroup -ResourceGroupName $resourceGroup -Name $nsgName
# Get the priorities of existing rules
$priorities = $nsg.SecurityRules | ForEach-Object { $_.Priority }
# Check if the priority is already used
if ($priorities -contains $newPriority) {
Write-Host "Priority $newPriority is already in use. Finding the next available priority."
# Find the next available priority
$newPriority = $newPriority + 10
while ($priorities -contains $newPriority) {
$newPriority = $newPriority + 10
}
Write-Host "New priority set to $newPriority."
}