Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Configure Azure VM Source Image Using PowerShell on Windows

In the context of managing Azure virtual machines (VMs), the Set-AzVMSourceImage cmdlet is used to specify the source image for a VM. This is particularly important when you need to create VMs from specific images, whether they are custom images or images from the Azure Marketplace. However, it's important to note that Set-AzVMSourceImage is a cmdlet specific to the Azure PowerShell module and not directly related to the Windows operating system itself.

For Windows users who manage Azure infrastructure, understanding how to use Azure PowerShell to configure VM source images is crucial. This article will guide you through the process of using Set-AzVMSourceImage with practical examples and provide insights into how you can manage Azure VMs effectively from a Windows environment.

Examples:

  1. Install Azure PowerShell Module: Before you can use Set-AzVMSourceImage, you need to install the Azure PowerShell module. Open PowerShell as an administrator and run the following command:

    Install-Module -Name Az -AllowClobber -Scope CurrentUser
  2. Connect to Your Azure Account: You need to authenticate to your Azure account. Use the following command to log in:

    Connect-AzAccount
  3. Create a New Resource Group: To organize your resources, create a new resource group:

    New-AzResourceGroup -Name MyResourceGroup -Location "EastUS"
  4. Define VM Configuration: Define the configuration for your new VM, including the source image. Here, we will use a Windows Server 2019 image from the Azure Marketplace:

    $vmName = "MyVM"
    $resourceGroupName = "MyResourceGroup"
    $location = "EastUS"
    $vmSize = "Standard_DS1_v2"
    $imagePublisher = "MicrosoftWindowsServer"
    $imageOffer = "WindowsServer"
    $imageSku = "2019-Datacenter"
    $imageVersion = "latest"
    
    $vmConfig = New-AzVMConfig -VMName $vmName -VMSize $vmSize
    $image = Get-AzVMImage -Location $location -PublisherName $imagePublisher -Offer $imageOffer -Skus $imageSku -Version $imageVersion
    $vmConfig = Set-AzVMSourceImage -VM $vmConfig -Id $image.Id
  5. Add Other VM Components: Add the necessary components like network interface, OS disk, and admin credentials to the VM configuration:

    $cred = Get-Credential -Message "Enter the username and password for the VM."
    
    $nic = New-AzNetworkInterface -ResourceGroupName $resourceGroupName -Location $location -Name "MyNic" -SubnetId $subnet.Id -PublicIpAddressId $publicIp.Id
    
    $vmConfig = Set-AzVMOperatingSystem -VM $vmConfig -Windows -ComputerName $vmName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate
    $vmConfig = Add-AzVMNetworkInterface -VM $vmConfig -Id $nic.Id
  6. Create the VM: Finally, create the VM with the specified configuration:

    New-AzVM -ResourceGroupName $resourceGroupName -Location $location -VM $vmConfig

By following these steps, you can configure and deploy a new Azure VM using a specified source image through PowerShell on a Windows environment.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.