Skip to content

Commit 0f2cf8e

Browse files
authored
Add ResourceId parameter to the Get-AzVmss cmdlet (#21922)
* all work * examp cleanup * ex * ex clean * test fix * clean * clean
1 parent b9e5e95 commit 0f2cf8e

File tree

6 files changed

+2906
-5
lines changed

6 files changed

+2906
-5
lines changed

src/Compute/Compute.Test/ScenarioTests/VirtualMachineScaleSetTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,5 +318,12 @@ public void TestVirtualMachineScaleSetOSImageScheduledEvents()
318318
{
319319
TestRunner.RunTestScript("Test-VirtualMachineScaleSetOSImageScheduledEvents");
320320
}
321+
322+
[Fact]
323+
[Trait(Category.AcceptanceType, Category.CheckIn)]
324+
public void TestVirtualMachineScaleSetGetById()
325+
{
326+
TestRunner.RunTestScript("Test-VirtualMachineScaleSetGetById");
327+
}
321328
}
322329
}

src/Compute/Compute.Test/ScenarioTests/VirtualMachineScaleSetTests.ps1

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4363,3 +4363,42 @@ function Test-VirtualMachineScaleSetOSImageScheduledEvents
43634363
Clean-ResourceGroup $rgname;
43644364
}
43654365
}
4366+
4367+
<#
4368+
.SYNOPSIS
4369+
Test the Get-AzVmss ResourceId parameter set.
4370+
This uses an ARM Resource Id to get an existing Vmss.
4371+
#>
4372+
function Test-VirtualMachineScaleSetGetById
4373+
{
4374+
4375+
# Setup
4376+
$rgname = Get-ComputeTestResourceName;
4377+
$loc = Get-ComputeVMLocation;
4378+
4379+
try
4380+
{
4381+
New-AzResourceGroup -Name $rgname -Location $loc -Force;
4382+
4383+
$vmssSize = 'Standard_D4s_v3';
4384+
$vmssName1 = 'vmss1' + $rgname;
4385+
$imageName = "Win2019Datacenter";
4386+
$domainNameLabel = "d1" + $rgname;
4387+
$adminUsername = Get-ComputeTestResourceName;
4388+
$adminPassword = Get-PasswordForVM | ConvertTo-SecureString -AsPlainText -Force;
4389+
$vmCred = New-Object System.Management.Automation.PSCredential($adminUsername, $adminPassword);
4390+
4391+
$result = New-AzVmss -ResourceGroupName $rgname -Credential $vmCred -VMScaleSetName $vmssName1 -ImageName $imageName -DomainNameLabel $domainNameLabel ;
4392+
4393+
$vmss = Get-AzVmss -ResourceGroupName $rgname -VMScaleSetName $vmssName1;
4394+
$vmssId = $vmss.Id;
4395+
$vmssGet = Get-AzVmss -ResourceId $vmssId;
4396+
4397+
Assert-AreEqual $vmss.Id $vmssGet.Id;
4398+
}
4399+
finally
4400+
{
4401+
# Cleanup
4402+
Clean-ResourceGroup $rgname;
4403+
}
4404+
}

src/Compute/Compute.Test/SessionRecords/Microsoft.Azure.Commands.Compute.Test.ScenarioTests.VirtualMachineScaleSetTests/TestVirtualMachineScaleSetGetById.json

Lines changed: 2777 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
* Added useful examples to the `New-AzVMConfig` help doc.
24+
* Added new `ResourceId` parameter to the `Get-AzVmss` cmdlet.
2425

2526
## Version 6.0.0
2627
* Added new switch parameter `OSImageScheduledEventEnabled` and string parameter `OSImageScheduledEventNotBeforeTimeoutInMinutes` to the cmdlets `New-AzVmssConfig` and `Update-AzVmss`.

src/Compute/Compute/Generated/VirtualMachineScaleSet/VirtualMachineScaleSetGetMethod.cs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
2929
using Microsoft.Azure.Management.Compute;
3030
using Microsoft.Azure.Management.Compute.Models;
31+
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
3132
using Microsoft.WindowsAzure.Commands.Utilities.Common;
3233

3334
namespace Microsoft.Azure.Commands.Compute.Automation
@@ -36,9 +37,7 @@ namespace Microsoft.Azure.Commands.Compute.Automation
3637
[OutputType(typeof(PSVirtualMachineScaleSet))]
3738
public partial class GetAzureRmVmss : ComputeAutomationBaseCmdlet
3839
{
39-
protected const string DefaultParameter = "DefaultParameter";
40-
protected const string FriendMethod = "FriendMethod";
41-
protected const string OSUpgradeHistoryMethodParameter = "OSUpgradeHistoryMethodParameter";
40+
protected const string DefaultParameter = "DefaultParameter", ResourceIdParameterSet = "ResourceIdParameterSet", FriendMethod = "FriendMethod", OSUpgradeHistoryMethodParameter = "OSUpgradeHistoryMethodParameter";
4241
private string UserDataExpand = ExpandTypesForGetVMScaleSets.UserData;
4342

4443
public override void ExecuteCmdlet()
@@ -118,6 +117,17 @@ public override void ExecuteCmdlet()
118117
}
119118
WriteObject(TopLevelWildcardFilter(resourceGroupName, vmScaleSetName, psObject), true);
120119
}
120+
else if (this.ParameterSetName.Equals(ResourceIdParameterSet))
121+
{
122+
ResourceIdentifier identifier = new ResourceIdentifier(this.ResourceId);
123+
resourceGroupName = identifier.ResourceGroupName;
124+
vmScaleSetName = identifier.ResourceName;
125+
126+
var result = VirtualMachineScaleSetsClient.Get(resourceGroupName, vmScaleSetName);
127+
var psObject = new PSVirtualMachineScaleSet();
128+
ComputeAutomationAutoMapperProfile.Mapper.Map<VirtualMachineScaleSet, PSVirtualMachineScaleSet>(result, psObject);
129+
WriteObject(psObject);
130+
}
121131
else
122132
{
123133
var result = VirtualMachineScaleSetsClient.ListAll();
@@ -144,13 +154,31 @@ public override void ExecuteCmdlet()
144154

145155
[Parameter(
146156
Position = 0,
157+
ParameterSetName = DefaultParameter,
158+
ValueFromPipelineByPropertyName = true)]
159+
[Parameter(
160+
Position = 0,
161+
ParameterSetName = FriendMethod,
162+
ValueFromPipelineByPropertyName = true)]
163+
[Parameter(
164+
Position = 0,
165+
ParameterSetName = OSUpgradeHistoryMethodParameter,
147166
ValueFromPipelineByPropertyName = true)]
148167
[ResourceGroupCompleter]
149168
[SupportsWildcards]
150169
public string ResourceGroupName { get; set; }
151170

152171
[Parameter(
153172
Position = 1,
173+
ParameterSetName = DefaultParameter,
174+
ValueFromPipelineByPropertyName = true)]
175+
[Parameter(
176+
Position = 1,
177+
ParameterSetName = FriendMethod,
178+
ValueFromPipelineByPropertyName = true)]
179+
[Parameter(
180+
Position = 1,
181+
ParameterSetName = OSUpgradeHistoryMethodParameter,
154182
ValueFromPipelineByPropertyName = true)]
155183
[ResourceNameCompleter("Microsoft.Compute/virtualMachineScaleSets", "ResourceGroupName")]
156184
[SupportsWildcards]
@@ -173,5 +201,12 @@ public override void ExecuteCmdlet()
173201
HelpMessage = "UserData for the Vmss, which will be Base64 encoded. Customer should not pass any secrets in here.",
174202
ValueFromPipelineByPropertyName = true)]
175203
public SwitchParameter UserData { get; set; }
204+
205+
[Parameter(
206+
ParameterSetName = ResourceIdParameterSet,
207+
ValueFromPipelineByPropertyName = true,
208+
HelpMessage = "The ARM resource id specifying the specific virtual machine scale set to return.")]
209+
[ValidateNotNullOrEmpty]
210+
public string ResourceId { get; set; }
176211
}
177212
}

src/Compute/Compute/help/Get-AzVmss.md

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ Get-AzVmss [[-ResourceGroupName] <String>] [[-VMScaleSetName] <String>] [-OSUpgr
3131
[-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
3232
```
3333

34+
### ResourceIdParameterSet
35+
```
36+
Get-AzVmss [-ResourceId <String>] [-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
37+
```
38+
3439
## DESCRIPTION
3540
The **Get-AzVmss** cmdlet gets the model and instance view of a Virtual Machine Scale Set (VMSS).
3641
The model view is the user specified properties of the virtual machine scale set.
@@ -240,6 +245,28 @@ VirtualMachineProfile :
240245

241246
The UserData value must be Base64 encoded. This command assumes you have created a Vmss with a UserData value.
242247

248+
### Example 6: Get a Virtual Machine Scale Set via its ResourceId.
249+
```powershell
250+
$rgname = "ResourceGroupName";
251+
$loc = "eastus";
252+
New-AzResourceGroup -Name $rgname -Location $loc;
253+
254+
$vmssSize = 'Standard_D4s_v3';
255+
$vmssName1 = 'vmss1' + $rgname;
256+
$imageName = "Win2019Datacenter";
257+
$adminUsername = <Username>;
258+
$adminPassword = <Password> | ConvertTo-SecureString -AsPlainText -Force;
259+
$cred = New-Object System.Management.Automation.PSCredential($adminUsername, $adminPassword);
260+
261+
$result = New-AzVmss -ResourceGroupName $rgname -Credential $cred -VMScaleSetName $vmssName1 -ImageName $imageName;
262+
263+
$vmss = Get-AzVmss -ResourceGroupName $rgname -VMScaleSetName $vmssName1;
264+
$vmssId = $vmss.Id;
265+
$vmssGet = Get-AzVmss -ResourceId $vmssId;
266+
```
267+
268+
Make a Vmss then get that same Vmss via its ARM resource Id.
269+
243270
## PARAMETERS
244271

245272
### -DefaultProfile
@@ -292,7 +319,7 @@ Specifies the name of the Resource Group of the VMSS.
292319
293320
```yaml
294321
Type: System.String
295-
Parameter Sets: (All)
322+
Parameter Sets: DefaultParameter, FriendMethod, OSUpgradeHistoryMethodParameter
296323
Aliases:
297324

298325
Required: False
@@ -302,6 +329,21 @@ Accept pipeline input: True (ByPropertyName)
302329
Accept wildcard characters: True
303330
```
304331
332+
### -ResourceId
333+
The ARM resource id specifying the specific virtual machine scale set object you want returned.
334+
335+
```yaml
336+
Type: System.String
337+
Parameter Sets: ResourceIdParameterSet
338+
Aliases:
339+
340+
Required: False
341+
Position: Named
342+
Default value: None
343+
Accept pipeline input: True (ByValue)
344+
Accept wildcard characters: False
345+
```
346+
305347
### -UserData
306348
UserData for the Vmss, which will be base-64 encoded. Customer should not pass any secrets in here.
307349
@@ -322,7 +364,7 @@ Species the name of the VMSS.
322364
323365
```yaml
324366
Type: System.String
325-
Parameter Sets: (All)
367+
Parameter Sets: DefaultParameter, FriendMethod, OSUpgradeHistoryMethodParameter
326368
Aliases: Name
327369

328370
Required: False

0 commit comments

Comments
 (0)