Change Product Key of Windows Server 2022 using Ansible and/or PowerShell script

Erik Burmester 0 Reputation points
2024-04-30T07:44:01.64+00:00

Hi there,

I am trying to automate the process of provisioning a Windows Server based Virtual Machine using Azure and Ansible.

My company has some Windows Server product keys available through the Microsoft Partner Program.

My goal is to use one of these product keys instead of the ones, which comes preconfigured, when creating a new Windows Server based VM in Azure (which results in higher costs, for leasing the operating system). I have found that there is actually an option in the web-based user interface to use a custom product key.

Is there a way to automate this with Ansible? Would it be possible to write a PowerShell-Script, pass a custom product key and change the product key configured by the operating system?

Will Azure recognie the change? Will this effect the costs of the VM?

Thanks for your help!

Kind regards,

Erik

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,256 questions
Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,268 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,402 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,135 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Nehruji R 2,741 Reputation points Microsoft Vendor
    2024-04-30T10:24:05.2366667+00:00

    Hello Erik Burmester,

    Greetings! Welcome to Microsoft Q&A Platform.

    Automating the provisioning of a Windows Server-based Virtual Machine in Azure using Ansible and applying a custom product key is indeed possible.

    You can create an Ansible playbook to provision the VM in Azure. The playbook would include tasks to create a resource group, virtual network, subnet, public IP, network security group, and the VM itself -

    https://learn.microsoft.com/en-us/azure/developer/ansible/vm-configure-windows?tabs=ansible

    If you have Windows Server license, you can use the same key in an Azure VM by selecting the Hybrid Benefit option while creating the Azure VM, this will save up to 49% with a license if you already have a Windows license.

    After the VM is provisioned, you can use a PowerShell script to change the product key. The Set-WindowsProductKey cmdlet from the DISM PowerShell module can be used to set the product key for the Windows image. Alternatively, you can use the slmgr.vbs script to change the product key.

    To automate this process with Ansible, you can use the azure_rm_virtualmachineextension module to run the PowerShell script as a custom script extension on the VM.Please ensure you have the necessary permissions and that your Ansible control node is set up correctly to communicate with Azure VMs using WinRM.

    As for the costs, using your own product key with the Azure Hybrid Benefit should indeed affect the costs positively by reducing them, as long as the VM size and usage are eligible for the benefit. Always use the Azure pricing calculator to estimate and manage costs effectively- https://azure.microsoft.com/en-us/pricing/calculator/.

    Try to create and build Windows/Unix servers using an Azure DevTest Labs. Azure DevTest Labs is a service that helps developers and testers quickly create environments in Azure while minimizing waste and controlling costs. You can use Azure DevTest Labs to create and manage virtual machines (VMs) running Windows or Linux, and you can also use it to create custom images that can be used to create new VMs.

    Ref: https://learn.microsoft.com/en-us/azure/devtest-labs/devtest-lab-overview

    https://learn.microsoft.com/en-us/azure/devtest-labs/devtest-lab-add-vm

    To manage other target hosts using Ansible and PowerShell scripting, you can set up an Ansible controller on one of your VMs in Azure DevTest Labs, and use PowerShell to manage other target hosts. Once you have set up your Ansible controller, you can use PowerShell to manage other target hosts.

    Hope this answer helps! Please let us know if you have any further queries. I’m happy to assist you further.


    Please "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.