New-AzVirtualNetworkGateway - Public IP address referecne is required

Occasional Contributor

Hi all,

I'm currently trying to create an AVD environment with nearly all required infrastructure via PowerShell: VNETs, Host Pools, etc. Right now I'm stuck at the creation step of the VPN Gateway. Feel free to take a look:

## Networking
# SubNets
$subnetDEVconfig = @{
    Name = 'DevSubnet'
    AddressPrefix = ''

$subnetPRODconfig = @{
    Name = 'ProdSubnet'
    AddressPrefix = ''

$subnetGatewayconfig = @{
    Name = 'GatewaySubnet'
    AddressPrefix = ''

$vnetconfig = @{
    Name = 'VNET-AVD'
    ResourceGroupName = $RGWVDEnv
    Location = $locationGen
    AddressPrefix = ''    
    Subnet = $subnetDEVconfig,$subnetPRODconfig,$subnetGatewayconfig

Write-Host -ForegroundColor Green "Creating VNET"$vnetconfig.Name"with subnets"$subnetDEVconfig.Name","$subnetPRODconfig.Name"and"$subnetGatewayconfig.Name
$virtualNetwork = New-AzVirtualNetwork @vnetconfig
$virtualNetwork = Get-AzVirtualNetwork -Name $vnetconfig.Name -ResourceGroupName $RGWVDENV

## VPN
# Public IP
Write-Host -ForegroundColor Green "Creating VPN Gateway"
$gwpipconfig = @{
    Name = 'Gateway-PIP-001'
    ResourceGroupName = $RGGeneral
    location = $locationGen
    AllocationMethod = 'static'

New-AzPublicIpAddress @gwpipconfig

$gwvnet = Get-AzVirtualNetwork -Name $vnetconfig.Name -ResourceGroupName $RGWVDEnv
$gwsubnet = Get-AzVirtualNetworkSubnetConfig -Name $subnetGatewayconfig.Name -VirtualNetwork $gwvnet
$gwpipassign = New-AzVirtualNetworkGatewayIpConfig -Name 'GW-PIP-Config-001' -SubnetId $gwvnet.subnets[2].Id -PublicIpAddressId $gwpipconfig.Id

# Gateway
$gwconfig = @{
    Name = 'AVD-VPN-Gateway-001'
    ResourceGroupName = $RGGeneral
    Location = $locationGen
    IpConfigurations = $gwpipassign
    GatewayType = 'VPN'
    VpnType = 'RouteBased'
    GatewaySku = 'Basic'

New-AzVirtualNetworkGateway @gwconfig


Running this results in the following error message I can't explain:

New-AzVirtualNetworkGateway: Public IP address reference is required for gateway IP configration /subscriptions/<<SUB-ID>>/resourceGroups/AVD-General-Services/providers/Microsoft.Network/virtualNetworkGateways/AVD-VPN-Gateway-001.
StatusCode: 400
ReasonPhrase: Bad Request
ErrorCode: PublicIpForGatewayIsRequired
ErrorMessage: Public IP address reference is required for gateway IP configration /subscriptions/<<SUB-ID>>/resourceGroups/AVD-General-Services/providers/Microsoft.Network/virtualNetworkGateways/AVD-VPN-Gateway-001.
OperationID : 710f7274-8875-4efe-9bfc-a940f0eda4a6


Does anyone can give me a hint, what I may have missed? As a source for the above snippet, I also used this: 

2 Replies
best response confirmed by Florian Adler (Occasional Contributor)
Looks quite familiar with the Microoft Docs reference.

Meanwhile I found the missing part: Instead of New-AzPublicIpAddress @gwpipconfig I had to type $gwpipconfig = New-AzPublicIpAddress @gwpipconfig. Otherwise any new parameter would get lost and not be available in the variable. Small mistake but bit issue.