Skip to content

Commit 17d9a8b

Browse files
authored
Set-AzVMOperatingSystem: EnableAutoUpdate no longer overwrites existing value (#15419)
* still & issue * test works * changelog * TimeZone fix too * test work in progress * commented out fix I think * enacting fix.
1 parent 7f81eb1 commit 17d9a8b

File tree

5 files changed

+500
-10
lines changed

5 files changed

+500
-10
lines changed

src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,5 +416,12 @@ public void TestVirtualMachineAssessmentMode()
416416
{
417417
TestRunner.RunTestScript("Test-VirtualMachineAssessmentMode");
418418
}
419+
420+
[Fact]
421+
[Trait(Category.AcceptanceType, Category.CheckIn)]
422+
public void TestVirtualMachineEnableAutoUpdate()
423+
{
424+
TestRunner.RunTestScript("Test-VirtualMachineEnableAutoUpdate");
425+
}
419426
}
420427
}

src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.ps1

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5011,4 +5011,50 @@ function Test-VirtualMachineAssessmentMode
50115011
# Cleanup
50125012
Clean-ResourceGroup $rgname;
50135013
}
5014-
}
5014+
}
5015+
5016+
<#
5017+
.SYNOPSIS
5018+
Windows machine test ensuring the EnableAutoUpdate value on the
5019+
provided VM is not overwritten.
5020+
#>
5021+
function Test-VirtualMachineEnableAutoUpdate
5022+
{
5023+
# Setup
5024+
$rgname = Get-ComputeTestResourceName;
5025+
$loc = Get-ComputeVMLocation;
5026+
5027+
try
5028+
{
5029+
New-AzResourceGroup -Name $rgname -Location $loc -Force;
5030+
5031+
# VM Profile & Hardware
5032+
$vmname = 'vm' + $rgname;
5033+
$vmsize = "Standard_B1s";
5034+
$domainNameLabel = "d1" + $rgname;
5035+
$computerName = "v" + $rgname;
5036+
5037+
# VM Credential
5038+
$user = "usertest";
5039+
$password = "Testing1234567";
5040+
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force;
5041+
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword);
5042+
5043+
# Creating a VM
5044+
$vmConfig = New-AzVmConfig -VMName $vmname -vmsize $vmsize;
5045+
5046+
$vmSet = Set-AzVMOperatingSystem -VM $vmConfig -Windows -ComputerName $computerName -Credential $cred -provisionVMAgent -EnableAutoUpdate:$false;
5047+
Assert-AreEqual $vmSet.OSProfile.WindowsConfiguration.EnableAutomaticUpdates $false;
5048+
5049+
$vmSet2 = Set-AzVMOperatingSystem -VM $vmSet -Windows -ComputerName $computerName -Credential $cred -provisionVMAgent -EnableAutoUpdate;
5050+
Assert-AreEqual $vmSet2.OSProfile.WindowsConfiguration.EnableAutomaticUpdates $true;
5051+
5052+
$vmSet3 = Set-AzVMOperatingSystem -VM $vmSet2 -Windows -ComputerName $computerName -Credential $cred -provisionVMAgent;
5053+
Assert-AreEqual $vmSet3.OSProfile.WindowsConfiguration.EnableAutomaticUpdates $true;
5054+
}
5055+
finally
5056+
{
5057+
# Cleanup
5058+
Clean-ResourceGroup $rgname;
5059+
}
5060+
}

src/Compute/Compute.Test/SessionRecords/Microsoft.Azure.Commands.Compute.Test.ScenarioTests.VirtualMachineTests/TestVirtualMachineEnableAutoUpdate.json

Lines changed: 417 additions & 0 deletions
Large diffs are not rendered by default.

src/Compute/Compute/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
-->
2222
## Upcoming Release
2323
* Fixed the warning in `New-AzVM` cmdlet stating the sku of the VM is being defaulted even if a sku size is provided by the user. Now it only occurs when the user does not provide a sku size.
24+
* Edited `Set-AzVmOperatingSystem` cmdlet to no longer overwrite any existing EnableAutomaticUpdates value on the passed in virtual machine if it exists.
2425

2526
## Version 4.15.0
2627
* Added optional parameter `-OrchestrationMode` to `New-AzVmss` and `New-AzVmssConfig`

src/Compute/Compute/VirtualMachine/Config/SetAzureVMOperatingSystemCommand.cs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -304,14 +304,27 @@ public class SetAzureVMOperatingSystemCommand : Microsoft.Azure.Commands.Resourc
304304

305305
public override void ExecuteCmdlet()
306306
{
307-
this.VM.OSProfile = new OSProfile
307+
if (this.VM.OSProfile == null)
308308
{
309-
ComputerName = this.ComputerName,
310-
AdminUsername = this.Credential.UserName,
311-
AdminPassword = ConversionUtilities.SecureStringToString(this.Credential.Password),
312-
CustomData = string.IsNullOrWhiteSpace(this.CustomData) ? null : Convert.ToBase64String(Encoding.UTF8.GetBytes(this.CustomData)),
313-
};
314-
309+
this.VM.OSProfile = new OSProfile
310+
{
311+
ComputerName = this.ComputerName,
312+
AdminUsername = this.Credential.UserName,
313+
AdminPassword = ConversionUtilities.SecureStringToString(this.Credential.Password),
314+
CustomData = string.IsNullOrWhiteSpace(this.CustomData) ? null : Convert.ToBase64String(Encoding.UTF8.GetBytes(this.CustomData)),
315+
};
316+
}
317+
// These two checks below are present to allow users to change the OS type in the VM object.
318+
// This behavior may change in the future.
319+
else if ((this.ParameterSetName == LinuxParamSet) & this.VM.OSProfile.WindowsConfiguration != null)
320+
{
321+
this.VM.OSProfile.WindowsConfiguration = null;
322+
}
323+
else if ((this.ParameterSetName == WindowsParamSet) & this.VM.OSProfile.LinuxConfiguration != null)
324+
{
325+
this.VM.OSProfile.LinuxConfiguration = null;
326+
}
327+
315328
if (this.ParameterSetName == LinuxParamSet)
316329
{
317330
if (this.VM.OSProfile.WindowsConfiguration != null)
@@ -408,9 +421,15 @@ public override void ExecuteCmdlet()
408421
this.VM.OSProfile.WindowsConfiguration.ProvisionVMAgent = false;
409422
}
410423

411-
this.VM.OSProfile.WindowsConfiguration.EnableAutomaticUpdates = this.EnableAutoUpdate.IsPresent;
424+
if (this.IsParameterBound(c => c.EnableAutoUpdate))
425+
{
426+
this.VM.OSProfile.WindowsConfiguration.EnableAutomaticUpdates = this.EnableAutoUpdate;
427+
}
412428

413-
this.VM.OSProfile.WindowsConfiguration.TimeZone = this.TimeZone;
429+
//adam tmp removal, if (this.IsParameterBound(c => c.TimeZone))
430+
//{
431+
this.VM.OSProfile.WindowsConfiguration.TimeZone = this.TimeZone;
432+
//}
414433

415434
this.VM.OSProfile.WindowsConfiguration.WinRM =
416435
!(this.WinRMHttp.IsPresent || this.WinRMHttps.IsPresent)

0 commit comments

Comments
 (0)