Skip to content

Conversation

@audreyttt
Copy link
Member

Description

Adding four new parameters to New-AzVmssConfig and Update-AzVmss, as requested in this design: https://github.com/Azure/azure-powershell-cmdlet-review-pr/issues/1510

Mandatory Checklist

  • SHOULD update ChangeLog.md file(s) appropriately
    • Update src/{{SERVICE}}/{{SERVICE}}/ChangeLog.md.
      • A snippet outlining the change(s) made in the PR should be written under the ## Upcoming Release header in the past tense.
    • Should not change ChangeLog.md if no new release is required, such as fixing test case only.
  • SHOULD regenerate markdown help files if there is cmdlet API change. Instruction
  • SHOULD have proper test coverage for changes in pull request.
  • SHOULD NOT adjust version of module manually in pull request

@azure-client-tools-bot-prd
Copy link

Thanks for your contribution! The pull request validation has started. Please revisit this comment for updated status.

Copilot AI review requested due to automatic review settings December 1, 2025 19:51
Copilot finished reviewing on behalf of audreyttt December 1, 2025 19:57
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds Automatic Zone Placement functionality for Virtual Machine Scale Sets (VMSS) by introducing four new parameters to New-AzVmssConfig, Update-AzVmss, and New-AzVmss cmdlets. The feature enables automatic distribution of VMSS instances across availability zones with configurable constraints, addressing the requirements outlined in the linked design document.

Key changes:

  • Added ZonePlacementPolicy parameter to control automatic zone selection (values: "Any" for VMs, "Auto" for VMSS)
  • Added MaxZoneCount parameter to limit the maximum number of zones used when policy is "Auto"
  • Added EnableMaxInstancePercentPerZone switch and ValueMaxInstancePercentPerZone to control the percentage distribution of VMs per zone
  • Implemented validation to ensure Overprovision is false when ZonePlacementPolicy is specified

Reviewed changes

Copilot reviewed 9 out of 14 changed files in this pull request and generated 10 comments.

Show a summary per file
File Description
src/Compute/Compute/ChangeLog.md Added changelog entry documenting the new parameters, but with incorrect casing
src/Compute/Compute/help/New-AzVmss.md Added documentation for ZonePlacementPolicy parameter in New-AzVmss cmdlet
src/Compute/Compute/help/New-AzVmssConfig.md Added documentation for all four new parameters in New-AzVmssConfig cmdlet
src/Compute/Compute/help/Update-AzVmss.md Added documentation for all four new parameters in Update-AzVmss cmdlet
src/Compute/Compute/Generated/VirtualMachineScaleSet/Config/NewAzureRmVmssConfigCommand.cs Implemented parameter definitions and zone placement logic for config cmdlet, with nested value parameter handling
src/Compute/Compute/Generated/VirtualMachineScaleSet/VirtualMachineScaleSetUpdateMethod.cs Implemented parameter definitions and zone placement logic for update cmdlet with critical bugs in parameter type and object assignment
src/Compute/Compute/Generated/Models/PSVirtualMachineScaleSet.cs Added Placement property to the PowerShell model object
src/Compute/Compute/Manual/PSVirtualMachineScaleSet.cs Added convenience ZonePlacementPolicy string property for simple parameter set
src/Compute/Compute/Manual/VirtualMachineScaleSetCreateOrUpdateMethod.cs Added ZonePlacementPolicy parameter to New-AzVmss simple parameter set
src/Compute/Compute/Strategies/ComputeRp/VirtualMachineScaleSetStrategy.cs Updated VMSS creation strategy to include Placement object with ZonePlacementPolicy
src/Compute/Compute.Management.Sdk/Generated/Models/VirtualMachineScaleSetUpdate.cs Added Placement property to SDK update model
src/Compute/Compute.Test/ScenarioTests/VirtualMachineScaleSetTests.ps1 Added comprehensive test covering creation and update scenarios with zone placement parameters
src/Compute/Compute.Test/ScenarioTests/VirtualMachineScaleSetTests.cs Added test entry point for new zone placement test

You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.

* Added `-InstantAccessDurationMinutes` parameter to New-AzSnapshotConfig.
* Added `SecureVMGuestStateSAS` parameter to `Grant-AzSnapshotAccess`.
* Updated SDK to use 2025-04-01 version of the ComputeRP API for Compute related cmdlets.
* Added new parameters `ZonePlacementPolicy`, `maxZoneCount`, `enableMaxInstancePercentPerZone`, and `valueMaxInstancePercentPerZone` to `New-AzVmssConfig` and `Update-AzVmss` cmdlets for VMSS Automatic Zone Placement.
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameter names in the ChangeLog entry should use PascalCase to match the actual cmdlet parameter names. The correct names are:

  • MaxZoneCount (not maxZoneCount)
  • EnableMaxInstancePercentPerZone (not enableMaxInstancePercentPerZone)
  • ValueMaxInstancePercentPerZone (not valueMaxInstancePercentPerZone)

PowerShell parameters are case-insensitive, but the ChangeLog should use the canonical PascalCase naming as defined in the code for consistency with other entries.

Suggested change
* Added new parameters `ZonePlacementPolicy`, `maxZoneCount`, `enableMaxInstancePercentPerZone`, and `valueMaxInstancePercentPerZone` to `New-AzVmssConfig` and `Update-AzVmss` cmdlets for VMSS Automatic Zone Placement.
* Added new parameters `ZonePlacementPolicy`, `MaxZoneCount`, `EnableMaxInstancePercentPerZone`, and `ValueMaxInstancePercentPerZone` to `New-AzVmssConfig` and `Update-AzVmss` cmdlets for VMSS (Virtual Machine Scale Set) Automatic Zone Placement.

Copilot uses AI. Check for mistakes.
Mandatory = false,
ValueFromPipelineByPropertyName = true,
HelpMessage = "Specifies whether maxInstancePercentPerZonePolicy should be enabled on the virtual machine scale set.")]
public bool EnableMaxInstancePercentPerZone { get; set; }
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameter type for EnableMaxInstancePercentPerZone is inconsistent between cmdlets:

  • In NewAzureRmVmssConfigCommand.cs (line 426): Uses SwitchParameter (correct for PowerShell boolean flags)
  • In VirtualMachineScaleSetUpdateMethod.cs (line 483): Uses bool (incorrect)

The parameter should be consistently defined as SwitchParameter in both cmdlets. With a bool type, users would have to specify -EnableMaxInstancePercentPerZone $true instead of the more natural -EnableMaxInstancePercentPerZone switch syntax.

Change line 483 from:

public bool EnableMaxInstancePercentPerZone { get; set; }

to:

public SwitchParameter EnableMaxInstancePercentPerZone { get; set; }

Also, update the usage on line 1589 from:

this.VirtualMachineScaleSetUpdate.ResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy.Enabled = this.EnableMaxInstancePercentPerZone;

to:

this.VirtualMachineScaleSetUpdate.ResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy.Enabled = this.EnableMaxInstancePercentPerZone.IsPresent;

And similarly on line 2500 for consistency with the config cmdlet pattern.

Suggested change
public bool EnableMaxInstancePercentPerZone { get; set; }
public SwitchParameter EnableMaxInstancePercentPerZone { get; set; }

Copilot uses AI. Check for mistakes.
Comment on lines +2490 to +2510
this.VirtualMachineScaleSetUpdate.ResiliencyPolicy.ZoneAllocationPolicy.MaxZoneCount = this.MaxZoneCount;
}

if (this.IsParameterBound(c => c.EnableMaxInstancePercentPerZone))
{
InitializeZoneAllocationPolicy();
if (this.VirtualMachineScaleSetUpdate.ResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy == null)
{
this.VirtualMachineScaleSetUpdate.ResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy = new MaxInstancePercentPerZonePolicy();
}
this.VirtualMachineScaleSetUpdate.ResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy.Enabled = this.EnableMaxInstancePercentPerZone;
}

if (this.IsParameterBound(c => c.ValueMaxInstancePercentPerZone))
{
InitializeZoneAllocationPolicy();
if (this.VirtualMachineScaleSetUpdate.ResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy == null)
{
this.VirtualMachineScaleSetUpdate.ResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy = new MaxInstancePercentPerZonePolicy();
}
this.VirtualMachineScaleSetUpdate.ResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy.Value = this.ValueMaxInstancePercentPerZone;
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the BuildPutObject method, line 2490 incorrectly assigns MaxZoneCount to VirtualMachineScaleSetUpdate instead of VirtualMachineScaleSet. This is inconsistent with the pattern used for ZonePlacementPolicy (line 2473) which correctly uses VirtualMachineScaleSet.Placement.

Change line 2490 from:

this.VirtualMachineScaleSetUpdate.ResiliencyPolicy.ZoneAllocationPolicy.MaxZoneCount = this.MaxZoneCount;

to:

this.VirtualMachineScaleSet.ResiliencyPolicy.ZoneAllocationPolicy.MaxZoneCount = this.MaxZoneCount;

The same issue affects lines 2496-2500 and 2506-2510 where VirtualMachineScaleSetUpdate should be VirtualMachineScaleSet.

Suggested change
this.VirtualMachineScaleSetUpdate.ResiliencyPolicy.ZoneAllocationPolicy.MaxZoneCount = this.MaxZoneCount;
}
if (this.IsParameterBound(c => c.EnableMaxInstancePercentPerZone))
{
InitializeZoneAllocationPolicy();
if (this.VirtualMachineScaleSetUpdate.ResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy == null)
{
this.VirtualMachineScaleSetUpdate.ResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy = new MaxInstancePercentPerZonePolicy();
}
this.VirtualMachineScaleSetUpdate.ResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy.Enabled = this.EnableMaxInstancePercentPerZone;
}
if (this.IsParameterBound(c => c.ValueMaxInstancePercentPerZone))
{
InitializeZoneAllocationPolicy();
if (this.VirtualMachineScaleSetUpdate.ResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy == null)
{
this.VirtualMachineScaleSetUpdate.ResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy = new MaxInstancePercentPerZonePolicy();
}
this.VirtualMachineScaleSetUpdate.ResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy.Value = this.ValueMaxInstancePercentPerZone;
this.VirtualMachineScaleSet.ResiliencyPolicy.ZoneAllocationPolicy.MaxZoneCount = this.MaxZoneCount;
}
if (this.IsParameterBound(c => c.EnableMaxInstancePercentPerZone))
{
InitializeZoneAllocationPolicy();
if (this.VirtualMachineScaleSet.ResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy == null)
{
this.VirtualMachineScaleSet.ResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy = new MaxInstancePercentPerZonePolicy();
}
this.VirtualMachineScaleSet.ResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy.Enabled = this.EnableMaxInstancePercentPerZone;
}
if (this.IsParameterBound(c => c.ValueMaxInstancePercentPerZone))
{
InitializeZoneAllocationPolicy();
if (this.VirtualMachineScaleSet.ResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy == null)
{
this.VirtualMachineScaleSet.ResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy = new MaxInstancePercentPerZonePolicy();
}
this.VirtualMachineScaleSet.ResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy.Value = this.ValueMaxInstancePercentPerZone;

Copilot uses AI. Check for mistakes.
[Parameter(
Mandatory = false,
ValueFromPipelineByPropertyName = true,
HelpMessage = "The configuration parameters used to limit the number of virtual machines per availability zone in the virtual machine scale set.")]
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The help message for the -ValueMaxInstancePercentPerZone parameter is too vague and doesn't clearly explain what the parameter does. It should explain:

  1. What the percentage value represents (e.g., "The maximum percentage of virtual machines that can be placed in any single availability zone")
  2. The valid range (e.g., 0-100)
  3. The behavior when not specified

The current description "The configuration parameters used to limit the number of virtual machines per availability zone in the virtual machine scale set" is unclear - it sounds like it describes a count rather than a percentage, and doesn't explain how the limit works.

Compare with other percentage parameters in the codebase which provide clearer explanations.

Suggested change
HelpMessage = "The configuration parameters used to limit the number of virtual machines per availability zone in the virtual machine scale set.")]
HelpMessage = "Specifies the maximum percentage (0-100) of virtual machines that can be placed in any single availability zone within the virtual machine scale set. If not specified, no limit is applied.")]

Copilot uses AI. Check for mistakes.
Comment on lines +6190 to +6214
$vmssConfig = New-AzVmssConfig -Location $loc -SkuCapacity 2 -SkuName "Standard_D2s_v3" -ZonePlacementPolicy 'Auto' -MaxZoneCount 2 -EnableMaxInstancePercentPerZone -ValueMaxInstancePercentPerZone 50;

# Configure IP and NIC
$ipCfg = New-AzVmssIpConfig -Name "ipconfig1" -SubnetId $subnet.Id
$vmssConfig = Add-AzVmssNetworkInterfaceConfiguration -VirtualMachineScaleSet $vmssConfig `
-Name "nicConfig" -Primary $true -IPConfiguration $ipCfg;

# Configure OS profile
$vmssConfig = Set-AzVmssOSProfile -VirtualMachineScaleSet $vmssConfig `
-ComputerNamePrefix "test" `
-AdminUsername $adminUsername `
-AdminPassword $password

# Assert the Automatic Zone Placement from the vmssConfig
Assert-AreEqual $vmssConfig.Placement.ZonePlacementPolicy 'Auto';
Assert-AreEqual $vmssConfig.ResiliencyPolicy.ZoneAllocationPolicy.MaxZoneCount 2;
Assert-True { $vmssConfig.ResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy.Enabled }

# Create the vmss using the config
$vmssResult = New-AzVmss -ResourceGroupName $rgname -VMScaleSetName $vmssName -VirtualMachineScaleSet $vmssConfig;

# Assert the Automatic Zone Placement from the vmssResult
Assert-AreEqual $vmssResult.Placement.ZonePlacementPolicy 'Auto';
Assert-AreEqual $vmssResult.ResiliencyPolicy.ZoneAllocationPolicy.MaxZoneCount 2;
Assert-True { $vmssResult.ResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy.Enabled }
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test sets -ValueMaxInstancePercentPerZone 50 on line 6190 but never asserts that this value was properly set on the resulting VMSS configuration or created VMSS.

Add assertions after line 6206 and 6214 to verify:

Assert-AreEqual $vmssConfig.ResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy.Value 50

and

Assert-AreEqual $vmssResult.ResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy.Value 50

This ensures the parameter is being correctly passed through to the API.

Copilot uses AI. Check for mistakes.
Comment on lines +1199 to +1203

if (this.IsParameterBound(c => c.ValueMaxInstancePercentPerZone))
{
vResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy.Value = this.ValueMaxInstancePercentPerZone;
}
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for setting ValueMaxInstancePercentPerZone is inconsistent between New-AzVmssConfig and Update-AzVmss:

  • In New-AzVmssConfig (lines 1200-1203): ValueMaxInstancePercentPerZone is only applied if EnableMaxInstancePercentPerZone is present, meaning users must specify both parameters together.
  • In Update-AzVmss (lines 1592-1600): ValueMaxInstancePercentPerZone can be set independently.

For consistency and better user experience, New-AzVmssConfig should handle ValueMaxInstancePercentPerZone independently, similar to Update-AzVmss. This would allow users to set the value in separate operations and make the cmdlets behave consistently.

Consider extracting the ValueMaxInstancePercentPerZone logic (lines 1200-1203) outside of the EnableMaxInstancePercentPerZone.IsPresent check:

if (this.IsParameterBound(c => c.ValueMaxInstancePercentPerZone))
{
    if (vResiliencyPolicy == null)
    {
        vResiliencyPolicy = new ResiliencyPolicy();
    }
    if (vResiliencyPolicy.ZoneAllocationPolicy == null)
    {
        vResiliencyPolicy.ZoneAllocationPolicy = new ZoneAllocationPolicy();
    }
    if (vResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy == null)
    {
        vResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy = new MaxInstancePercentPerZonePolicy();
    }
    vResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy.Value = this.ValueMaxInstancePercentPerZone;
}
Suggested change
if (this.IsParameterBound(c => c.ValueMaxInstancePercentPerZone))
{
vResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy.Value = this.ValueMaxInstancePercentPerZone;
}
}
if (this.IsParameterBound(c => c.ValueMaxInstancePercentPerZone))
{
if (vResiliencyPolicy == null)
{
vResiliencyPolicy = new ResiliencyPolicy();
}
if (vResiliencyPolicy.ZoneAllocationPolicy == null)
{
vResiliencyPolicy.ZoneAllocationPolicy = new ZoneAllocationPolicy();
}
if (vResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy == null)
{
vResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy = new MaxInstancePercentPerZonePolicy();
}
vResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy.Value = this.ValueMaxInstancePercentPerZone;

Copilot uses AI. Check for mistakes.
```
### -ValueMaxInstancePercentPerZone
The configuration parameters used to limit the number of virtual machines per availability zone in the virtual machine scale set.
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description for the -ValueMaxInstancePercentPerZone parameter is too vague and doesn't follow the pattern used for other percentage parameters in the codebase. It should explain:

  1. What the percentage value represents (e.g., "The maximum percentage of virtual machines that can be placed in any single availability zone")
  2. The valid range (e.g., 0-100)
  3. The behavior when not specified (if applicable)

Compare with other percentage parameters like -MaxBatchInstancePercent (line 651-653 in Update-AzVmss.md) which provides clear explanation of what the percentage controls and its constraints.

The current description "The configuration parameters used to limit the number of virtual machines per availability zone in the virtual machine scale set" is unclear - it sounds like it describes a count rather than a percentage, and doesn't explain how the limit works.

Suggested change
The configuration parameters used to limit the number of virtual machines per availability zone in the virtual machine scale set.
Specifies the maximum percentage of virtual machines that can be placed in any single availability zone within the virtual machine scale set. Valid values are 0 to 100. If not specified, placement is not restricted by zone.

Copilot uses AI. Check for mistakes.
```
### -ValueMaxInstancePercentPerZone
The configuration parameters used to limit the number of virtual machines per availability zone in the virtual machine scale set.
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description for the -ValueMaxInstancePercentPerZone parameter is too vague and doesn't follow the pattern used for other percentage parameters in the codebase. It should explain:

  1. What the percentage value represents (e.g., "The maximum percentage of virtual machines that can be placed in any single availability zone")
  2. The valid range (e.g., 0-100)
  3. The behavior when not specified (if applicable)

Compare with other percentage parameters like -RegularPriorityPercentage (line 922 in New-AzVmssConfig.md) which provides clear explanation of what the percentage controls.

The current description "The configuration parameters used to limit the number of virtual machines per availability zone in the virtual machine scale set" is unclear - it sounds like it describes a count rather than a percentage, and doesn't explain how the limit works.

Suggested change
The configuration parameters used to limit the number of virtual machines per availability zone in the virtual machine scale set.
Specifies the maximum percentage of virtual machines (VMs) that can be placed in any single availability zone within the scale set. Valid values are from 0 to 100. If not specified, placement is not restricted by percentage per zone.

Copilot uses AI. Check for mistakes.
[Parameter(
Mandatory = false,
ValueFromPipelineByPropertyName = true,
HelpMessage = "The configuration parameters used to limit the number of virtual machines per availability zone in the virtual machine scale set.")]
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The help message for the -ValueMaxInstancePercentPerZone parameter is too vague and doesn't clearly explain what the parameter does. It should explain:

  1. What the percentage value represents (e.g., "The maximum percentage of virtual machines that can be placed in any single availability zone")
  2. The valid range (e.g., 0-100)
  3. The behavior when not specified

The current description "The configuration parameters used to limit the number of virtual machines per availability zone in the virtual machine scale set" is unclear - it sounds like it describes a count rather than a percentage, and doesn't explain how the limit works.

Compare with other percentage parameters in the codebase which provide clearer explanations.

Suggested change
HelpMessage = "The configuration parameters used to limit the number of virtual machines per availability zone in the virtual machine scale set.")]
HelpMessage = "Specifies the maximum percentage (0-100) of virtual machines that can be placed in any single availability zone in the scale set. Valid values are 0 to 100. If not specified, there is no limit on the percentage of instances per zone.")]

Copilot uses AI. Check for mistakes.
Comment on lines +6148 to 6226
<#
.SYNOPSIS
Test Virtual Machine Scale Set Automatic Zone Placement feature
#>
function Test-VirtualMachineScaleSetAutomaticZonePlacement
{
# Setup
$rgname = Get-ComputeTestResourceName

try
{
# Common
$loc = "eastus2euap";
$vmssName = "vmssAutoZonePlacement" + $rgname;
$vnetName = "vnetAutoZonePlacement" + $rgname;
$subnetName = "subnetAutoZonePlacement" + $rgname;
$adminUsername = Get-ComputeTestResourceName;
$password = Get-PasswordForVM | ConvertTo-SecureString -AsPlainText -Force;
$cred = New-Object System.Management.Automation.PSCredential ($adminUsername, $password);
$linuxImage = "Canonical:0001-com-ubuntu-server-jammy:22_04-lts:latest"
$domainNameLabel1 = "d1" + $rgname;

# Create resource group
New-AzResourceGroup -Name $rgname -Location $loc -Force


# Create using simple parameter and New-AzVmss
$vmss = New-AzVmss -ResourceGroupName $rgname -Location $loc -Credential $cred -VMScaleSetName 'testvmss2' -DomainNameLabel $domainNameLabel1 -Image $linuxImage -ZonePlacementPolicy 'Auto'

# Verify ZonePlacementPolicy successfully set
Assert-AreEqual $vmss.Placement.ZonePlacementPolicy 'Auto'

# Create VNet and Subnet
$vnetAddressPrefix = "10.0.0.0/16";
$subnetAddressPrefix = "10.0.0.0/24";
$subnetConfig = New-AzVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix $subnetAddressPrefix;
$vnet = New-AzVirtualNetwork -Name $vnetName -ResourceGroupName $rgname -Location $loc -AddressPrefix $vnetAddressPrefix -Subnet $subnetConfig;

# Get subnet object
$subnet = Get-AzVirtualNetwork -Name $vnetName -ResourceGroupName $rgname | Get-AzVirtualNetworkSubnetConfig -Name $subnetName

# VMSS Config
$vmssConfig = New-AzVmssConfig -Location $loc -SkuCapacity 2 -SkuName "Standard_D2s_v3" -ZonePlacementPolicy 'Auto' -MaxZoneCount 2 -EnableMaxInstancePercentPerZone -ValueMaxInstancePercentPerZone 50;

# Configure IP and NIC
$ipCfg = New-AzVmssIpConfig -Name "ipconfig1" -SubnetId $subnet.Id
$vmssConfig = Add-AzVmssNetworkInterfaceConfiguration -VirtualMachineScaleSet $vmssConfig `
-Name "nicConfig" -Primary $true -IPConfiguration $ipCfg;

# Configure OS profile
$vmssConfig = Set-AzVmssOSProfile -VirtualMachineScaleSet $vmssConfig `
-ComputerNamePrefix "test" `
-AdminUsername $adminUsername `
-AdminPassword $password

# Assert the Automatic Zone Placement from the vmssConfig
Assert-AreEqual $vmssConfig.Placement.ZonePlacementPolicy 'Auto';
Assert-AreEqual $vmssConfig.ResiliencyPolicy.ZoneAllocationPolicy.MaxZoneCount 2;
Assert-True { $vmssConfig.ResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy.Enabled }

# Create the vmss using the config
$vmssResult = New-AzVmss -ResourceGroupName $rgname -VMScaleSetName $vmssName -VirtualMachineScaleSet $vmssConfig;

# Assert the Automatic Zone Placement from the vmssResult
Assert-AreEqual $vmssResult.Placement.ZonePlacementPolicy 'Auto';
Assert-AreEqual $vmssResult.ResiliencyPolicy.ZoneAllocationPolicy.MaxZoneCount 2;
Assert-True { $vmssResult.ResiliencyPolicy.ZoneAllocationPolicy.MaxInstancePercentPerZonePolicy.Enabled }

# Update vmss
$vmssUpdate = Update-AzVmss -ResourceGroupName $rgname -Name $vmssName -MaxZoneCount 3

Assert-AreEqual $vmssUpdate.ResiliencyPolicy.ZoneAllocationPolicy.MaxZoneCount 3;
}
finally
{
# Cleanup
Clean-ResourceGroup $rgname
}
}
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test should include validation of the overprovision constraint that prevents using ZonePlacementPolicy with Overprovision = true. The implementation throws an ArgumentException when both are specified (lines 1160-1163 in NewAzureRmVmssConfigCommand.cs and lines 1565-1568, 2476-2479 in VirtualMachineScaleSetUpdateMethod.cs).

Consider adding a negative test case that verifies this validation, such as:

# Test that overprovision validation works
Assert-ThrowsContains { 
    New-AzVmssConfig -Location $loc -ZonePlacementPolicy 'Auto' -Overprovision $true 
} "Overprovision must be false"

This ensures the validation logic is working correctly and documents the constraint for future maintainers.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant