Skip to content

Commit 5f19e78

Browse files
New cmdlet Invoke-AzVmPatchAssessment (#12658)
* adding for new cmdlet and adding/updating some fuctions in ComputeTestCommon.ps1 * package versions * Fixed bug * change log * move changelog entry to Upcoming Release * cmdlet name changed to invoke-azvmpatchassessment * Revert "cmdlet name changed to invoke-azvmpatchassessment" This reverts commit 3dfec44. * a * changed name to Invoke-AzVmPatchAssessment * Az.Compute.md
1 parent e6c9641 commit 5f19e78

File tree

11 files changed

+5548
-3
lines changed

11 files changed

+5548
-3
lines changed

src/Compute/Compute.Test/ScenarioTests/ComputeTestCommon.ps1

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,9 @@ function Get-ResourceProviderLocation
670670

671671
function Get-ComputeVMLocation
672672
{
673-
Get-Location "Microsoft.Compute" "virtualMachines" "East US";
673+
$result = Get-Location "Microsoft.Compute" "virtualMachines" "East US";
674+
$result = $result.Replace(' ', '');
675+
return $result
674676
}
675677

676678
function Get-ComputeAvailabilitySetLocation
@@ -777,3 +779,20 @@ function Get-VMImageVersion {
777779
-Offer $offer `
778780
-Skus $sku | Sort-Object -Descending Version | select -First 1).Version
779781
}
782+
783+
784+
<#
785+
.SYNOPSIS
786+
Gets a sku that is available for the location, subscription, and resourceType
787+
#>
788+
function Get-AvailableSku
789+
{
790+
param([string] $location, [string] $resourceType)
791+
792+
$res = get-azcomputeresourcesku $location | where-object ResourceType -match $resourceType
793+
$res = $res.where({$_.restrictions.count -eq 0})
794+
if ($resourceType -match "virtualmachine"){
795+
$res = $res.where({$_.Name -notmatch "Standard_B"})
796+
}
797+
return $res[0].Name
798+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
2+
using Xunit;
3+
4+
5+
namespace Microsoft.Azure.Commands.Compute.Test.ScenarioTests
6+
{
7+
public class VirtualMachinePatchTests : ComputeTestRunner
8+
{
9+
public VirtualMachinePatchTests(Xunit.Abstractions.ITestOutputHelper output)
10+
: base(output)
11+
{
12+
}
13+
14+
[Fact]
15+
[Trait(Category.AcceptanceType, Category.CheckIn)]
16+
public void TestInvokeAzVmPatchAssessment()
17+
{
18+
TestRunner.RunTestScript("Test-InvokeAzVmPatchAssessment");
19+
}
20+
}
21+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# ----------------------------------------------------------------------------------
2+
#
3+
# Copyright Microsoft Corporation
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
# ----------------------------------------------------------------------------------
14+
15+
<#
16+
.SYNOPSIS
17+
Test Invoke-AzVmPatchAssessment cmdlet
18+
#>
19+
function Test-InvokeAzVmPatchAssessment
20+
{
21+
# Setup
22+
$rgname = Get-ComputeTestResourceName
23+
24+
try
25+
{
26+
# Common
27+
$loc = Get-ComputeVMLocation;
28+
$loc = $loc.Replace(' ', '');
29+
30+
New-AzResourceGroup -Name $rgname -Location $loc -Force;
31+
32+
# VM Profile & Hardware
33+
$vmsize = Get-AvailableSku $loc "virtualMachine"
34+
$vmname = 'vm' + $rgname;
35+
36+
$p = New-AzVMConfig -VMName $vmname -VMSize $vmsize -Priority 'Low' -MaxPrice 0.1538;
37+
38+
# NRP
39+
$subnet = New-AzVirtualNetworkSubnetConfig -Name ('subnet' + $rgname) -AddressPrefix "10.0.0.0/24";
40+
$vnet = New-AzVirtualNetwork -Force -Name ('vnet' + $rgname) -ResourceGroupName $rgname -Location $loc -AddressPrefix "10.0.0.0/16" -Subnet $subnet;
41+
$vnet = Get-AzVirtualNetwork -Name ('vnet' + $rgname) -ResourceGroupName $rgname;
42+
$subnetId = $vnet.Subnets[0].Id;
43+
$nic = New-AzNetworkInterface -Force -Name ('nic' + $rgname) -ResourceGroupName $rgname -Location $loc -SubnetId $subnetId
44+
$nic = Get-AzNetworkInterface -Name ('nic' + $rgname) -ResourceGroupName $rgname;
45+
$nicId = $nic.Id;
46+
47+
$p = Add-AzVMNetworkInterface -VM $p -Id $nicId;
48+
49+
# OS & Image
50+
$user = "Foo2";
51+
$password = $PLACEHOLDER;
52+
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force;
53+
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword);
54+
$computerName = 'test';
55+
56+
$p = Set-AzVMOperatingSystem -VM $p -Windows -ComputerName $computerName -Credential $cred;
57+
58+
$imgRef = Get-DefaultCRPImage -loc $loc;
59+
$p = ($imgRef | Set-AzVMSourceImage -VM $p);
60+
61+
# Create a Virtual Machine
62+
New-AzVM -ResourceGroupName $rgname -Location $loc -VM $p;
63+
64+
$patchResult = invoke-azvmpatchAssessment -resourcegroupname $rgname -vmname $vmname
65+
66+
Assert-NotNull $patchResult;
67+
Assert-AreEqual "Succeeded" $patchResult.Status;
68+
Assert-NotNull $patchResult.StartDateTime;
69+
70+
}
71+
finally
72+
{
73+
# Cleanup
74+
Clean-ResourceGroup $rgname
75+
}
76+
}

src/Compute/Compute.Test/SessionRecords/Microsoft.Azure.Commands.Compute.Test.ScenarioTests.VirtualMachinePatchTests/TestInvokeAzVmPatchAssessment.json

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

src/Compute/Compute/Az.Compute.psd1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,14 @@ CmdletsToExport = 'Remove-AzAvailabilitySet', 'Get-AzAvailabilitySet',
168168
'Remove-AzHost', 'New-AzDiskEncryptionSetConfig',
169169
'New-AzDiskEncryptionSet', 'Get-AzDiskEncryptionSet',
170170
'Remove-AzDiskEncryptionSet', 'Update-AzDiskEncryptionSet',
171-
'Set-AzVmssOrchestrationServiceState'
171+
'Set-AzVmssOrchestrationServiceState', 'Invoke-AzVmPatchAssessment'
172172

173173
# Variables to export from this module
174174
# VariablesToExport = @()
175175

176176
# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
177177
AliasesToExport = 'Get-AzVmssDiskEncryptionStatus',
178-
'Get-AzVmssVMDiskEncryptionStatus', 'Repair-AzVmssServiceFabricUD'
178+
'Get-AzVmssVMDiskEncryptionStatus', 'Repair-AzVmssServiceFabricUD', 'Invoke-AzVmAssessPatch', 'Invoke-AzVmPatchAssess'
179179

180180
# DSC resources to export from this module
181181
# DscResourcesToExport = @()

src/Compute/Compute/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* Added '-EncryptionAtHost' parameter to New-AzVm, New-AzVmss, New-AzVMConfig, New-AzVmssConfig, Update-AzVM, and Update-AzVmss
2323
* Added 'SecurityProfile' to Get-AzVM and Get-AzVmss return object
2424
* Added the '-InstanceView' switch as optional parameter to Get-AzHostGroup
25+
* New cmdlet 'Invoke-AzVmPatchAssessment'
2526

2627
## Version 4.2.1
2728
* Added warning when using `New-AzVmss` without "latest" image version

src/Compute/Compute/Generated/Models/ComputeAutoMapperProfile.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ private static void Initialize()
180180
.ForMember(c => c.Type, o => o.MapFrom(r => r.Type1));
181181
cfg.CreateMap<TO.PSVirtualMachineScaleSetExtension, FROM.VirtualMachineScaleSetExtension>()
182182
.ForMember(c => c.Type1, o => o.MapFrom(r => r.Type));
183+
cfg.CreateMap<FROM.VirtualMachineAssessPatchesResult, TO.PSVirtualMachinePatchAssessmentResult>();
184+
cfg.CreateMap<TO.PSVirtualMachinePatchAssessmentResult, FROM.VirtualMachineAssessPatchesResult>();
183185
});
184186
_mapper = config.CreateMapper();
185187
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
16+
using Microsoft.Azure.Commands.Compute.Common;
17+
using Microsoft.Azure.Commands.Compute.Models;
18+
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
19+
using Microsoft.Azure.Commands.Compute.Automation.Models;
20+
using Microsoft.Azure.Management.Compute.Models;
21+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
22+
using System;
23+
using System.Collections;
24+
using System.Collections.Generic;
25+
using System.Management.Automation;
26+
using Microsoft.Azure.Management.Compute;
27+
28+
namespace Microsoft.Azure.Commands.Compute.Automation
29+
{
30+
[Cmdlet(VerbsLifecycle.Invoke, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "VMPatchAssessment", SupportsShouldProcess = true, DefaultParameterSetName = DefaultParameterSet )]
31+
[OutputType(typeof(PSVirtualMachinePatchAssessmentResult))]
32+
[Alias("Invoke-" + ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "VMPatchAssess", "Invoke-" + ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "VMAssessPatch")]
33+
34+
public partial class InvokeAzureVMPatchAssessmentCommand : ComputeAutomationBaseCmdlet
35+
{
36+
private const string DefaultParameterSet = "DefaultParameterSet";
37+
private const string InputObjectParameterSet = "InputObjectParameterSet";
38+
private const string ResourceIDParameterSet = "ResourceIDParameterSet";
39+
40+
[Parameter(
41+
Mandatory = true,
42+
Position = 0,
43+
ParameterSetName = DefaultParameterSet,
44+
ValueFromPipelineByPropertyName = true,
45+
HelpMessage = "The resource group name.")]
46+
[ResourceGroupCompleter]
47+
[ValidateNotNullOrEmpty]
48+
public string ResourceGroupName { get; set; }
49+
50+
[Parameter(
51+
Mandatory = true,
52+
Position = 1,
53+
ParameterSetName = DefaultParameterSet,
54+
ValueFromPipelineByPropertyName = true,
55+
HelpMessage = "Virtual Machine name")]
56+
[ResourceNameCompleter("Microsoft.Compute/VirtualMachines", "ResourceGroupName")]
57+
public string VMName { get; set; }
58+
59+
[Parameter(
60+
Mandatory = true,
61+
Position = 0,
62+
ParameterSetName = ResourceIDParameterSet,
63+
ValueFromPipelineByPropertyName = true,
64+
HelpMessage = "Resource ID for your virtual machine.")]
65+
[ResourceIdCompleter("Microsoft.Compute/virtualMachines")]
66+
public string ResourceId { get; set; }
67+
68+
[Alias("VMProfile")]
69+
[Parameter(
70+
Mandatory = true,
71+
Position = 0,
72+
ParameterSetName = InputObjectParameterSet,
73+
ValueFromPipeline = true,
74+
ValueFromPipelineByPropertyName = true,
75+
HelpMessage = "PowerShell Virtual Machine Object")]
76+
[ValidateNotNullOrEmpty]
77+
public PSVirtualMachine VM { get; set; }
78+
79+
[Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background")]
80+
public SwitchParameter AsJob { get; set; }
81+
82+
public override void ExecuteCmdlet()
83+
{
84+
base.ExecuteCmdlet();
85+
ExecuteClientAction(() =>
86+
{
87+
if (ShouldProcess(this.VMName, VerbsLifecycle.Invoke))
88+
{
89+
string resourceGroupName;
90+
string vmName;
91+
switch (this.ParameterSetName)
92+
{
93+
case ResourceIDParameterSet:
94+
resourceGroupName = GetResourceGroupName(this.ResourceId);
95+
vmName = GetResourceName(this.ResourceId, "Microsoft.Compute/virtualmachines");
96+
break;
97+
case InputObjectParameterSet:
98+
resourceGroupName = GetResourceGroupName(this.VM.Id);
99+
vmName = GetResourceName(this.VM.Id, "Microsoft.Compute/virtualmachines");
100+
break;
101+
default:
102+
resourceGroupName = this.ResourceGroupName;
103+
vmName = this.VMName;
104+
break;
105+
}
106+
107+
108+
var result = VirtualMachinesClient.AssessPatches(resourceGroupName, vmName);
109+
var psObject = new PSVirtualMachinePatchAssessmentResult();
110+
ComputeAutomationAutoMapperProfile.Mapper.Map<VirtualMachineAssessPatchesResult, PSVirtualMachinePatchAssessmentResult>(result, psObject);
111+
WriteObject(psObject);
112+
}
113+
});
114+
}
115+
}
116+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// Copyright (c) Microsoft and contributors. All rights reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
//
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
using System;
18+
using System.Collections.Generic;
19+
using Microsoft.Azure.Management.Compute.Models;
20+
21+
22+
namespace Microsoft.Azure.Commands.Compute.Automation.Models
23+
{
24+
public class PSVirtualMachinePatchAssessmentResult
25+
{
26+
public string Status { get; set; }
27+
public string AssessmentActivityId { get; set; }
28+
public bool? RebootPending { get; set; }
29+
public int? CriticalAndSecurityPatchCount { get; set; }
30+
public int? OtherPatchCount { get; set; }
31+
public DateTime? StartDateTime { get; set; }
32+
public IList<VirtualMachineSoftwarePatchProperties> Patches { get; set; }
33+
public ApiError Error { get; set; }
34+
}
35+
}

src/Compute/Compute/help/Az.Compute.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ Grants an access to a disk.
209209
### [Grant-AzSnapshotAccess](Grant-AzSnapshotAccess.md)
210210
Grants an access to a snapshot.
211211

212+
### [Invoke-AzVMPatchAssessment](Invoke-AzVMPatchAssessment.md)
213+
Assess patch state of a virtual machine.
214+
212215
### [Invoke-AzVMReimage](Invoke-AzVMReimage.md)
213216
Reimage an Azure virtual machine.
214217

0 commit comments

Comments
 (0)