GinShell.Azure/Public/Enable-GsAzureSnapshotPublicAccess.ps1
|
function Enable-GsAzureSnapshotPublicAccess { <# .SYNOPSIS Enables public network access on an Azure snapshot. .PARAMETER Snapshot The Azure snapshot object to modify. .EXAMPLE Enable-GsAzureSnapshotPublicAccess -Snapshot $snap #> [CmdletBinding(SupportsShouldProcess)] param ( [Parameter(Mandatory)] [object]$Snapshot ) if ($Snapshot.PublicNetworkAccess -eq 'Enabled') { Write-GsLog -Message "Public network access is already enabled for snapshot '$($Snapshot.Name)'" -Type Success return } if (-not $PSCmdlet.ShouldProcess($Snapshot.Name, "Enable public network access")) { return } try { Write-GsLog -Message "Enabling public network access for snapshot '$($Snapshot.Name)'" -Type Action $uri = "/subscriptions/$($Snapshot.Id.Split('/')[2])/resourceGroups/$($Snapshot.ResourceGroupName)/providers/Microsoft.Compute/snapshots/$($Snapshot.Name)?api-version=2022-03-02" $payload = @{ properties = @{ PublicNetworkAccess = 'Enabled' NetworkAccessPolicy = 'AllowAll' } } | ConvertTo-Json -Depth 5 Invoke-AzRestMethod -Method PATCH -Path $uri -Payload $payload -ErrorAction Stop | Out-Null Write-GsLog -Message "Public access successfully enabled for snapshot '$($Snapshot.Name)'" -Type Success } catch { Write-GsLog -Message "Failed to enable public access: $($_.Exception.Message)" -Type Error throw } } |