Functions/Private/Add-AHMAddyIPToContainerRegistry.ps1
<# .Synopsis Adds the current public IP address to the container registry network rules. .DESCRIPTION Adds the current public IP address to the container registry network rules. .EXAMPLE Add-AHMyIPToContainerRegistry -Id '/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyResourceGroup/providers/Microsoft.ContainerRegistry/registries/MyContainerRegistry' #> Function Add-AHMyIPToContainerRegistry { [CmdletBinding()] param ( [Parameter()] [string] $Id ) $CR = Get-AzResource -Id $Id If (-not $?) { #The resource no longer exists return } #Validate the IP doesn't already exist otherwise there will be duplicates. If ($CR.Properties.NetworkRuleSet.IpRules.length -gt 0 -and $CR.Properties.NetworkRuleSet.IpRules.Value.Contains($Script:MyPublicIPAddress)) { Write-Verbose "The IP $Script:MyPublicIPAddress was already allowed on $($CR.ResourceName) in $($CR.ResourceGroupName)." } Else { # write-host "My IP: $($Script:MyPublicIPAddress) " $rules = @() ForEach ($rule in $CR.Properties.networkRuleSet.ipRules) { $rules += New-AzContainerRegistryNetworkRule -IPRule -IPAddressOrRange $rule.Value -Action $rule.Action } $rules += New-AzContainerRegistryNetworkRule -IPRule -IPAddressOrRange $Script:MyPublicIPAddress -Action 'Allow' $ruleSet = Set-AzContainerRegistryNetworkRuleSet -NetworkRule $rules -DefaultAction $CR.Properties.NetworkRuleSet.DefaultAction Update-AzContainerRegistry -ResourceId $Id -NetworkRuleSet $ruleSet } } |