Adding a public IP address to an existing VM in Azure

Category: azure scripting and command line tools

Question

dbutch1976 on Thu, 05 Apr 2018 20:27:47


Hi guys,

I'm stuck with this one.  I know how to add an public IP address to a VM in the portal, I even know how to add a public IP to a VM in PowerShell, but I am not able to add a public IP address to an existing VM within Powershell.  Can anyone assist?  I'm thinking it's something along the lines of the following, but this does not work.

    $nic = Get-AzureRmNetworkInterface -Name DCNW16KTEST1nic1 -ResourceGroupName RG-TOBEDELETED
    $nic.IpConfigurations[0].PublicIpAllocationMethod = "Dynamic"
    Set-AzureRmNetworkInterface -NetworkInterface $nic

Replies

jrv on Thu, 05 Apr 2018 21:04:52


I moved this to the Azure PowerShell/Scripting forum as this is a product specific issue.

CHEEKATLAPRADEEP-MSFT on Fri, 06 Apr 2018 08:35:42


You may use the following steps to accomplish your scenario:

# Create New IP

New-AzureRmPublicIpAddress -Name “mynew-ip” -ResourceGroupName “myRG” -AllocationMethod Static -Location CentralUS

# Set the variables

$Nic = Get-AzureRmNetworkInterface -ResourceGroupName "myRG" -Name "myNIC"

$pip = Get-AzureRmPublicIpAddress -ResourceGroupName “myRG” -Name “mynew-ip”

$Nic.IpConfigurations[0].PublicIpAddress=$pip

# Finally set the IP address against the NIC

Set-AzureRmNetworkInterface -NetworkInterface $Nic

-----------------------------------------------------------------------------------------------

Do click on "Mark as Answer" and “Vote as Helpful” on the post that helps you, this can be beneficial to other community members.

dbutch1976 on Mon, 09 Apr 2018 12:51:22


Fantastic, worked perfectly.  Thanks for your assistance.

CHEEKATLAPRADEEP-MSFT on Tue, 10 Apr 2018 04:13:13


Glad to hear that your issue has been resolved.

-----------------------------------------------------------------------------------------------

If this answer was helpful, click “Mark as Answer” or “Up-Vote”. To provide additional feedback on your forum experience, click here