From e24f39baa85547212475c60489a513c7e707f253 Mon Sep 17 00:00:00 2001 From: PSCmdAssistant Date: Thu, 17 Apr 2025 19:52:40 +0000 Subject: [PATCH 1/2] Autogen code --- .../VirtualMachine/InvokeAzureVMInstallPatch.cs | 16 ++++++++++++---- .../PSVirtualMachineInstallPatchesResult.cs | 8 ++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/Compute/Compute/Generated/VirtualMachine/InvokeAzureVMInstallPatch.cs b/src/Compute/Compute/Generated/VirtualMachine/InvokeAzureVMInstallPatch.cs index 34bc79958a84..2b7422ba58cb 100644 --- a/src/Compute/Compute/Generated/VirtualMachine/InvokeAzureVMInstallPatch.cs +++ b/src/Compute/Compute/Generated/VirtualMachine/InvokeAzureVMInstallPatch.cs @@ -1,10 +1,9 @@ -// ---------------------------------------------------------------------------------- +// ---------------------------------------------------------------------------------- // // Copyright Microsoft Corporation // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// http://www.apache.org/licenses/LICENSE-2.0 +// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -233,6 +232,10 @@ public partial class InvokeAzureVMInstallPatch : ComputeAutomationBaseCmdlet [PSArgumentCompleter("Critical", "Security", "Other")] public string[] ClassificationToIncludeForLinux { get; set; } + [Parameter( + Mandatory = false, + HelpMessage = "This is used to install patches that were published on or before this given max published date.")] + public string MaxPatchPublishDate { get; set; } [Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background")] public SwitchParameter AsJob { get; set; } @@ -278,6 +281,11 @@ public override void ExecuteCmdlet() vmInstallPatchesParameters.MaximumDuration = this.MaximumDuration; vmInstallPatchesParameters.RebootSetting = this.RebootSetting; + if (this.IsParameterBound(c => c.MaxPatchPublishDate)) + { + vmInstallPatchesParameters.MaxPatchPublishDate = this.MaxPatchPublishDate; + } + // divde linux and windows if (this.Windows.IsPresent) { @@ -331,4 +339,4 @@ public override void ExecuteCmdlet() }); } } -} +} \ No newline at end of file diff --git a/src/Compute/Compute/Manual/PSVirtualMachineInstallPatchesResult.cs b/src/Compute/Compute/Manual/PSVirtualMachineInstallPatchesResult.cs index b57dd90b471e..67f18e223312 100644 --- a/src/Compute/Compute/Manual/PSVirtualMachineInstallPatchesResult.cs +++ b/src/Compute/Compute/Manual/PSVirtualMachineInstallPatchesResult.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) Microsoft and contributors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -6,8 +6,7 @@ // You may obtain a copy of the License at // http://www.apache.org/licenses/LICENSE-2.0 // -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, +// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // // See the License for the specific language governing permissions and @@ -35,5 +34,6 @@ public class PSVirtualMachineInstallPatchesResult public IList Patches { get; set; } public DateTime? StartDateTime { get; set; } public ApiError Error { get; set; } + public string MaxPatchPublishDate { get; set; } } -} +} \ No newline at end of file From b70835b1937630c8ba14811641c472e01d0835d4 Mon Sep 17 00:00:00 2001 From: PSCmdAssistant Date: Thu, 17 Apr 2025 19:53:16 +0000 Subject: [PATCH 2/2] Test generation and changelog update --- .../ScenarioTests/VirtualMachineTests.cs | 9 ++- .../ScenarioTests/VirtualMachineTests.ps1 | 70 ++++++++++++++++++- src/Compute/Compute/ChangeLog.md | 2 + 3 files changed, 79 insertions(+), 2 deletions(-) diff --git a/src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.cs b/src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.cs index 4d23f017bad7..2a920dabfa53 100644 --- a/src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.cs +++ b/src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.cs @@ -695,5 +695,12 @@ public void TestVirtualMachinePlacement() { TestRunner.RunTestScript("Test-VirtualMachinePlacement"); } - } + + [Fact] + [Trait(Category.AcceptanceType, Category.CheckIn)] + public void testgeninvokeazvminstallpatch() + { + TestRunner.RunTestScript("TestGen-invokeazvminstallpatch"); + } + } } diff --git a/src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.ps1 b/src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.ps1 index 87134d2ee720..c4aac335bf08 100644 --- a/src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.ps1 +++ b/src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.ps1 @@ -7945,4 +7945,72 @@ function Test-VirtualMachinePlacement # Cleanup Clean-ResourceGroup $rgname; } -} \ No newline at end of file +} +function TestGen-invokeazvminstallpatch +{ + # Setup + $rgname = Get-ComputeTestResourceName; + $loc = Get-Location; + + try + { + New-AzResourceGroup -Name $rgname -Location $loc -Force; + + # VM Profile & Hardware + $vmname = 'vm' + $rgname; + $vmsize = "Standard_B1s"; + $domainNameLabel = "d1" + $rgname; + $stnd = "Standard"; + + # Creating a VM + $p = New-AzVmConfig -VMName $vmname -vmsize $vmsize -SecurityType $stnd; + + $publisherName = "MicrosoftWindowsServer" + $offer = "WindowsServer" + $sku = "2019-DataCenter" + $p = Set-AzVMSourceImage -VM $p -PublisherName $publisherName -Offer $offer -Skus $sku -Version 'latest' + + # NRP + $subnet = New-AzVirtualNetworkSubnetConfig -Name ('subnet' + $rgname) -AddressPrefix "10.0.0.0/24"; + $vnet = New-AzVirtualNetwork -Force -Name ('vnet' + $rgname) -ResourceGroupName $rgname -Location $loc -AddressPrefix "10.0.0.0/16" -Subnet $subnet; + $vnet = Get-AzVirtualNetwork -Name ('vnet' + $rgname) -ResourceGroupName $rgname; + $subnetId = $vnet.Subnets[0].Id; + $pubip = New-AzPublicIpAddress -Force -Name ('pubip' + $rgname) -ResourceGroupName $rgname -Location $loc -AllocationMethod Dynamic -DomainNameLabel $domainNameLabel; + $pubip = Get-AzPublicIpAddress -Name ('pubip' + $rgname) -ResourceGroupName $rgname; + $pubipId = $pubip.Id; + $nic = New-AzNetworkInterface -Force -Name ('nic' + $rgname) -ResourceGroupName $rgname -Location $loc -SubnetId $subnetId -PublicIpAddressId $pubip.Id; + $nic = Get-AzNetworkInterface -Name ('nic' + $rgname) -ResourceGroupName $rgname; + $nicId = $nic.Id; + + $p = Add-AzVMNetworkInterface -VM $p -Id $nicId; + + # OS & Image + $user = "Foo12"; + $password = $PLACEHOLDER; + $securePassword = ConvertTo-SecureString $password -AsPlainText -Force; + $cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword); + $computerName = 'test'; + + $p = Set-AzVMOperatingSystem -VM $p -Windows -ComputerName $computerName -Credential $cred; + + $vm = New-AzVM -ResourceGroupName $rgname -Location $loc -Vm $p + $vm = Get-AzVM -ResourceGroupName $rgname -Name $vmname + + Assert-NotNull $vm; + + # Test case 1: Invoke-AzVMInstallPatch without maxPatchPublishDate + $patchResult = Invoke-AzVMInstallPatch -VM $vm -Windows -RebootSetting 'Never' -MaximumDuration PT1H -ClassificationToIncludeForWindows critical + Assert-AreEqual 'Succeeded' $patchResult.Status + + # Test case 2: Invoke-AzVMInstallPatch with maxPatchPublishDate + $maxPatchPublishDate = (Get-Date).AddDays(-30).ToString("yyyy-MM-ddTHH:mm:ss") + $patchResultWithDate = Invoke-AzVMInstallPatch -VM $vm -Windows -RebootSetting 'Never' -MaximumDuration PT1H -ClassificationToIncludeForWindows critical -MaxPatchPublishDate $maxPatchPublishDate + Assert-AreEqual 'Succeeded' $patchResultWithDate.Status + + } + finally + { + # Cleanup + Remove-AzResourceGroup -Name $rgname -Force -ErrorAction SilentlyContinue; + } +} diff --git a/src/Compute/Compute/ChangeLog.md b/src/Compute/Compute/ChangeLog.md index e7c5a20c78f3..71f0ce662fb8 100644 --- a/src/Compute/Compute/ChangeLog.md +++ b/src/Compute/Compute/ChangeLog.md @@ -20,6 +20,8 @@ --> ## Upcoming Release +* Added new cmdlet `Invoke-AzVMInstallPatch` to install patches on Azure VMs. + - Introduced optional parameter `maxPatchPublishDate` to specify the maximum publish date for patches, accepting a dateTime parsable string. * Added new cmdlets `Add-AzVmssSkuProfileVMSize` and `Remove-AzVmssSkuProfileVMSize` to add and remove VM sizes to and from the VMSS SkuProfile. * Added new parameters `ZonePlacementPolicy`, `IncludeZone`, `ExcludeZone`, and `AlignRegionalDisksToVMZone` to cmdlets `New-AzVM` and `New-AzVmConfig` * Added new parameter `AlignRegionalDisksToVMZone` to cmdlet `Update-AzVM`.