Using PowerShell in Azure to assign a new virtual machine to an existing virtual network!

%3CLINGO-SUB%20id%3D%22lingo-sub-2045419%22%20slang%3D%22en-US%22%3EUsing%20PowerShell%20in%20Azure%20to%20assign%20a%20new%20virtual%20machine%20to%20an%20existing%20virtual%20network!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2045419%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%20Azure%20friends%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20used%20the%20PowerShell%20ISE%20for%20this%20configuration.%20But%20you%20are%20also%20very%20welcome%20to%20use%20Visual%20Studio%20Code%2C%20just%20as%20you%20wish.%26nbsp%3BPlease%20start%20with%20the%20following%20steps%20to%20begin%20the%20deployment%20(the%20Hashtags%20are%20comments)%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23The%20first%20two%20lines%20have%20nothing%20to%20do%20with%20the%20configuration%2C%20but%20make%20some%20space%20below%20in%20the%20blue%20part%20of%20the%20ISE%3C%2FP%3E%3CP%3E%3CSTRONG%3ESet-Location%20C%3A%5CTemp%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EClear-Host%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23So%20that%20you%20can%20carry%20out%20the%20configuration%2C%20you%20need%20the%20necessary%20cmdlets%2C%20these%20are%20contained%20in%20the%20module%20Az%20(is%20the%20higher-level%20module%20from%20a%20number%20of%20submodules)%3C%2FP%3E%3CP%3E%3CSTRONG%3EInstall-Module%20-Name%20Az%20-Force%20-AllowClobber%20-Verbose%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23Log%20into%20Azure%3CBR%20%2F%3E%3CSTRONG%3EConnect-AzAccount%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%3E%3CDIV%3E%3CSPAN%3E%23Search%26nbsp%3Bfor%26nbsp%3Bthe%26nbsp%3Bresource%26nbsp%3Bgroups%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3EGet-AzResourceGroup%26nbsp%3B%7C%26nbsp%3BFormat-Table%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23Whats%26nbsp%3Bin%26nbsp%3Ba%26nbsp%3Bspecific%26nbsp%3Bresource%26nbsp%3Bgroup%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3EGet-AzResource%26nbsp%3B-ResourceGroupName%26nbsp%3Btw-azuredemo-rg%26nbsp%3B%7C%26nbsp%3BFormat-Table%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23Some%26nbsp%3Bvariables%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24RGName%26nbsp%3B%3D%26nbsp%3B%22tw-azuredemo-rg%22%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24VnetName%26nbsp%3B%3D%26nbsp%3B%22tw-vnet-workload%22%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24Location%26nbsp%3B%3D%26nbsp%3B%22westeurope%22%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24VMName%26nbsp%3B%3D%26nbsp%3B%22twsrv2021%22%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24credential%26nbsp%3B%3D%26nbsp%3BGet-Credential%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23We%26nbsp%3Bneed%26nbsp%3Ball%26nbsp%3Binfos%26nbsp%3Babout%26nbsp%3Bthe%26nbsp%3Bvirtual%26nbsp%3Bnetwork%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24VirtualNetwork%26nbsp%3B%3D%26nbsp%3B(Get-AzVirtualNetwork%26nbsp%3B-Name%26nbsp%3B%24VnetName%26nbsp%3B-ResourceGroupName%26nbsp%3B%24RGName)%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23Let's%26nbsp%3Bhave%26nbsp%3Ba%26nbsp%3Blook%26nbsp%3Bat%26nbsp%3Bthe%26nbsp%3Bvariable%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24VirtualNetwork%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23Create%26nbsp%3Ba%26nbsp%3Bnetwork%26nbsp%3Binterface%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24nic%26nbsp%3B%3D%26nbsp%3BNew-AzNetworkInterface%26nbsp%3B%60%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B-ResourceGroupName%26nbsp%3B%24RGName%26nbsp%3B%60%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B-Name%26nbsp%3B%22twsrv2021-nic%22%26nbsp%3B%60%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B-Location%26nbsp%3B%24Location%26nbsp%3B%60%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B-SubnetId%26nbsp%3B%24VirtualNetwork.Subnets%5B0%5D.Id%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23Define%26nbsp%3Byour%26nbsp%3BVM%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24vmConfig%26nbsp%3B%3D%26nbsp%3BNew-AzVMConfig%26nbsp%3B-VMName%26nbsp%3B%24VMName%26nbsp%3B-VMSize%26nbsp%3B%22Standard_D2s_v4%22%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23Create%26nbsp%3Bthe%26nbsp%3Brest%26nbsp%3Bof%26nbsp%3Byour%26nbsp%3BVM%26nbsp%3Bconfiguration%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24vmConfig%26nbsp%3B%3D%26nbsp%3BSet-AzVMOperatingSystem%26nbsp%3B-VM%26nbsp%3B%24vmConfig%26nbsp%3B%60%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B-Windows%26nbsp%3B%60%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B-ComputerName%26nbsp%3B%24VMName%26nbsp%3B%60%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B-Credential%26nbsp%3B%24credential%26nbsp%3B%60%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B-ProvisionVMAgent%26nbsp%3B%60%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B-EnableAutoUpdate%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24vmConfig%26nbsp%3B%3D%26nbsp%3BSet-AzVMSourceImage%26nbsp%3B-VM%26nbsp%3B%24vmConfig%26nbsp%3B%60%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B-PublisherName%26nbsp%3B%22MicrosoftWindowsServer%22%26nbsp%3B%60%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B-Offer%26nbsp%3B%22WindowsServer%22%26nbsp%3B%60%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B-Skus%26nbsp%3B%222016-Datacenter%22%26nbsp%3B%60%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B-Version%26nbsp%3B%22latest%22%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23Attach%26nbsp%3Bthe%26nbsp%3Bnetwork%26nbsp%3Binterface%26nbsp%3Bthat%26nbsp%3Byou%26nbsp%3Bpreviously%26nbsp%3Bcreated%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24vmConfig%26nbsp%3B%3D%26nbsp%3BAdd-AzVMNetworkInterface%26nbsp%3B-VM%26nbsp%3B%24vmConfig%26nbsp%3B-Id%26nbsp%3B%24nic.Id%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23Create%26nbsp%3Byour%26nbsp%3BVM%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3ENew-AzVM%26nbsp%3B-VM%26nbsp%3B%24vmConfig%26nbsp%3B-ResourceGroupName%26nbsp%3B%24RGName%26nbsp%3B-Location%26nbsp%3B%24Location%3C%2FSTRONG%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%3E%3CSPAN%3ENow%20you%20have%20used%20the%20PowerShell%20to%20create%20a%20new%20virtual%20machine%20and%20added%20to%20an%20existing%20virtual%20network!%20Congratulations!%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CDIV%3E%3CP%3EI%20hope%20this%20article%20was%20useful.%20Best%20regards%2C%20Tom%20Wechsler%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EP.S.%26nbsp%3BAll%20scripts%20(%23PowerShell%2C%20Azure%20CLI%2C%20%23Terraform%2C%20%23ARM)%20that%20I%20use%20can%20be%20found%20on%20github!%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Ftomwechsler%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Ftomwechsler%3C%2FA%3E%3C%2FP%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2045419%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ECloud%20Services%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ECompute%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EHands-on-Labs%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EVirtual%20Machine%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Frequent Contributor

 

Hi Azure friends,

 

I used the PowerShell ISE for this configuration. But you are also very welcome to use Visual Studio Code, just as you wish. Please start with the following steps to begin the deployment (the Hashtags are comments):

 

#The first two lines have nothing to do with the configuration, but make some space below in the blue part of the ISE

Set-Location C:\Temp
Clear-Host

 

#So that you can carry out the configuration, you need the necessary cmdlets, these are contained in the module Az (is the higher-level module from a number of submodules)

Install-Module -Name Az -Force -AllowClobber -Verbose

 

#Log into Azure
Connect-AzAccount

 

#Search for the resource groups
Get-AzResourceGroup | Format-Table

#Whats in a specific resource group
Get-AzResource -ResourceGroupName tw-azuredemo-rg | Format-Table

#Some variables
$RGName = "tw-azuredemo-rg"
$VnetName = "tw-vnet-workload"
$Location = "westeurope"
$VMName = "twsrv2021"
$credential = Get-Credential

#We need all infos about the virtual network
$VirtualNetwork = (Get-AzVirtualNetwork -Name $VnetName -ResourceGroupName $RGName)

#Let's have a look at the variable
$VirtualNetwork

#Create a network interface
$nic = New-AzNetworkInterface `
    -ResourceGroupName $RGName `
    -Name "twsrv2021-nic" `
    -Location $Location `
    -SubnetId $VirtualNetwork.Subnets[0].Id

#Define your VM
$vmConfig = New-AzVMConfig -VMName $VMName -VMSize "Standard_D2s_v4"

#Create the rest of your VM configuration
$vmConfig = Set-AzVMOperatingSystem -VM $vmConfig `
    -Windows `
    -ComputerName $VMName `
    -Credential $credential `
    -ProvisionVMAgent `
    -EnableAutoUpdate
$vmConfig = Set-AzVMSourceImage -VM $vmConfig `
    -PublisherName "MicrosoftWindowsServer" `
    -Offer "WindowsServer" `
    -Skus "2016-Datacenter" `
    -Version "latest"

#Attach the network interface that you previously created
$vmConfig = Add-AzVMNetworkInterface -VM $vmConfig -Id $nic.Id

#Create your VM
New-AzVM -VM $vmConfig -ResourceGroupName $RGName -Location $Location

 

Now you have used the PowerShell to create a new virtual machine and added to an existing virtual network! Congratulations!
 

I hope this article was useful. Best regards, Tom Wechsler

 

P.S. All scripts (#PowerShell, Azure CLI, #Terraform, #ARM) that I use can be found on github! https://github.com/tomwechsler

0 Replies