Skip to content

Instantly share code, notes, and snippets.

@nmackenzie nmackenzie/one-VM.ps1
Last active Oct 22, 2015

What would you like to do?
Create an Azure VM with a public instance-level IP address
$testName = "lower-case-unique-name"
$resourceGroupName = $testName
$location = "West US"
$publisher = "MicrosoftWindowsServer"
$offer = "WindowsServer"
$sku = "2012-R2-Datacenter"
$version = "latest"
$subnetName = "Subnet-1"
New-AzureResourceGroup -Name $resourceGroupName -Location $location
New-AzureStorageAccount -ResourceGroupName $resourceGroupName `
-Name $testName -Location $location -Type Standard_LRS
$subnet = New-AzureVirtualNetworkSubnetConfig -Name $subnetName `
-AddressPrefix ""
$vnet = New-AzureVirtualNetwork -Name "VNET" `
-ResourceGroupName $resourceGroupName `
-Location $location -AddressPrefix "" -Subnet $subnet
$subnet = Get-AzureVirtualNetworkSubnetConfig -Name $subnetName -VirtualNetwork $vnet
$pip = New-AzurePublicIpAddress -ResourceGroupName $resourceGroupName -Name "vip1" `
-Location $location -AllocationMethod Dynamic -DomainNameLabel $testName
$nic = New-AzureNetworkInterface -ResourceGroupName $resourceGroupName `
-Name "nic1" -Subnet $subnet -Location $location -PublicIpAddress $pip -PrivateIpAddress ""
New-AzureAvailabilitySet -ResourceGroupName $resourceGroupName `
-Name "AVSet" -Location $location
$avset = Get-AzureAvailabilitySet -ResourceGroupName $resourceGroupName -Name "AVSet"
$cred = Get-Credential
$vmConfig = New-AzureVMConfig -VMName "$testName-w1" -VMSize "Standard_A1" `
-AvailabilitySetId $avSet.Id |
Set-AzureVMOperatingSystem -Windows -ComputerName "contoso-w1" `
-Credential $cred -ProvisionVMAgent -EnableAutoUpdate |
Set-AzureVMSourceImage -PublisherName $publisher -Offer $offer -Skus $sku `
-Version $version |
Set-AzureVMOSDisk -Name "$testName-w1" -VhdUri "https://$$testName-w1-os.vhd" `
-Caching ReadWrite -CreateOption fromImage |
Add-AzureVMNetworkInterface -Id $nic.Id
New-AzureVM -ResourceGroupName $resourceGroupName -Location $location `
-VM $vmConfig
(Get-AzurePublicIpAddress -ResourceGroupName $resourceGroupName).IpAddress

This comment has been minimized.

Copy link

rajinders commented Jul 18, 2015

Thanks for posting this sample. I needed to create an ARM template where I had to create a VM with instance IP. I was able to use your script to create a VM, use azure resource explorer to view the settings and create my template.


This comment has been minimized.

Copy link

mjnip commented Oct 22, 2015

Hi thanks for this.. getting the following on.. any ideas?

New-AzurePublicIpAddress -ResourceGroupName $ResourceGrpName -AllocationMethod Dynamic

New-AzurePublicIpAddress : Value cannot be null.
Parameter name: publicIpAddressName
At line:1 char:1

  • New-AzurePublicIpAddress
  • - CategoryInfo          : CloseError: (:) [New-AzurePublicIpAddress], ArgumentNullException
    - FullyQualifiedErrorId : Microsoft.Azure.Commands.Network.NewAzurePublicIpAddressCommand
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.