Skip to content

Commit 0148bae

Browse files
author
Maddie Clayton
authored
Merge pull request #8233 from maddieclayton/vmnameps
Make name optional in Id parameter set for Compute
2 parents 45f3305 + 17679ee commit 0148bae

File tree

15 files changed

+225
-26
lines changed

15 files changed

+225
-26
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,19 +164,19 @@ function Test-VirtualMachine
164164
Assert-AreEqual "BGInfo" $vm1.Extensions[0].VirtualMachineExtensionType
165165
Assert-AreEqual "Microsoft.Compute" $vm1.Extensions[0].Publisher
166166

167-
$job = Start-AzureRmVM -Name $vmname -ResourceGroupName $rgname -AsJob;
167+
$job = Start-AzureRmVM -Id $vm1.Id -AsJob;
168168
$result = $job | Wait-Job;
169169
Assert-AreEqual "Completed" $result.State;
170170
$st = $job | Receive-Job;
171171
Verify-PSComputeLongRunningOperation $st;
172172

173-
$job = Restart-AzureRmVM -Name $vmname -ResourceGroupName $rgname -AsJob;
173+
$job = Restart-AzureRmVM -Id $vm1.Id -AsJob;
174174
$result = $job | Wait-Job;
175175
Assert-AreEqual "Completed" $result.State;
176176
$st = $job | Receive-Job;
177177
Verify-PSComputeLongRunningOperation $st;
178178

179-
$job = Stop-AzureRmVM -Name $vmname -ResourceGroupName $rgname -Force -StayProvisioned -AsJob;
179+
$job = Stop-AzureRmVM -Id $vm1.Id -Force -StayProvisioned -AsJob;
180180
$result = $job | Wait-Job;
181181
Assert-AreEqual "Completed" $result.State;
182182
$st = $job | Receive-Job;
@@ -283,7 +283,7 @@ function Test-VirtualMachine
283283
Assert-True { $vm2.ResourceGroupName -eq $rgname }
284284

285285
# Remove
286-
$st = Remove-AzureRmVM -Name $vmname2 -ResourceGroupName $rgname -Force;
286+
$st = Remove-AzureRmVM -Id $vm2.Id -Force;
287287
Verify-PSComputeLongRunningOperation $st;
288288
}
289289
finally
@@ -2721,7 +2721,7 @@ function Test-VirtualMachineRedeploy
27212721
Assert-NotNull $vm2.Location;
27222722

27232723
# Redeploy the VM
2724-
$job = Set-AzureRmVM -ResourceGroupName $rgname -Name $vmname -Redeploy -AsJob;
2724+
$job = Set-AzureRmVM -Id $vm2.Id -Redeploy -AsJob;
27252725
$result = $job | Wait-Job;
27262726
Assert-AreEqual "Completed" $result.State;
27272727

src/Compute/Compute/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- Additional information about change #1
2020
-->
2121
## Upcoming Release
22+
* Name is now optional in ID parameter set for Restart/Start/Stop/Remove/Set-AzVM and Save-AzVMImage
2223
* Updated the description of ID in help files
2324

2425
## Version 1.0.0

src/Compute/Compute/VirtualMachine/Action/RestartAzureVMCommand.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
using Microsoft.Azure.Commands.Compute.Common;
1818
using Microsoft.Azure.Commands.Compute.Models;
1919
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
20+
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
21+
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
2022

2123
namespace Microsoft.Azure.Commands.Compute
2224
{
@@ -65,9 +67,29 @@ public class RestartAzureVMCommand : VirtualMachineBaseCmdlet
6567
[Parameter(
6668
Mandatory = true,
6769
Position = 1,
70+
ParameterSetName = RestartResourceGroupNameParameterSet,
71+
ValueFromPipelineByPropertyName = true,
72+
HelpMessage = "The virtual machine name.")]
73+
[Parameter(
74+
Mandatory = true,
75+
Position = 1,
76+
ParameterSetName = PerformMaintenanceResourceGroupNameParameterSet,
77+
ValueFromPipelineByPropertyName = true,
78+
HelpMessage = "The virtual machine name.")]
79+
[Parameter(
80+
Mandatory = false,
81+
Position = 1,
82+
ParameterSetName = RestartIdParameterSet,
83+
ValueFromPipelineByPropertyName = true,
84+
HelpMessage = "The virtual machine name.")]
85+
[Parameter(
86+
Mandatory = false,
87+
Position = 1,
88+
ParameterSetName = PerformMaintenanceIdParameterSet,
6889
ValueFromPipelineByPropertyName = true,
6990
HelpMessage = "The virtual machine name.")]
7091
[ResourceNameCompleter("Microsoft.Compute/virtualMachines", "ResourceGroupName")]
92+
[CmdletParameterBreakingChange("Name", ChangeDescription = "Name will be removed from the Id parameter sets in an upcoming breaking change release.")]
7193
[ValidateNotNullOrEmpty]
7294
public string Name { get; set; }
7395

@@ -95,6 +117,12 @@ public override void ExecuteCmdlet()
95117
{
96118
base.ExecuteCmdlet();
97119

120+
if (!string.IsNullOrEmpty(Id) && string.IsNullOrEmpty(Name))
121+
{
122+
ResourceIdentifier parsedId = new ResourceIdentifier(Id);
123+
this.Name = parsedId.ResourceName;
124+
}
125+
98126
if (this.ParameterSetName.Equals(RestartIdParameterSet) || this.ParameterSetName.Equals(PerformMaintenanceIdParameterSet))
99127
{
100128
this.ResourceGroupName = GetResourceGroupNameFromId(this.Id);

src/Compute/Compute/VirtualMachine/Action/SaveAzureVMImageCommand.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
using Microsoft.Azure.Commands.Compute.Models;
2020
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
2121
using Microsoft.Azure.Management.Compute.Models;
22+
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
23+
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
2224

2325
namespace Microsoft.Azure.Commands.Compute
2426
{
@@ -30,9 +32,17 @@ public class SaveAzureVMImageCommand : VirtualMachineActionBaseCmdlet
3032
[Parameter(
3133
Mandatory = true,
3234
Position = 1,
35+
ParameterSetName = ResourceGroupNameParameterSet,
36+
ValueFromPipelineByPropertyName = true,
37+
HelpMessage = "The virtual machine name.")]
38+
[Parameter(
39+
Mandatory = false,
40+
Position = 1,
41+
ParameterSetName = IdParameterSet,
3342
ValueFromPipelineByPropertyName = true,
3443
HelpMessage = "The virtual machine name.")]
3544
[ResourceNameCompleter("Microsoft.Compute/virtualMachines", "ResourceGroupName")]
45+
[CmdletParameterBreakingChange("Name", ChangeDescription = "Name will be removed from the Id parameter set in an upcoming breaking change release.")]
3646
[ValidateNotNullOrEmpty]
3747
public string Name { get; set; }
3848

@@ -73,6 +83,12 @@ public override void ExecuteCmdlet()
7383

7484
ExecuteClientAction(() =>
7585
{
86+
if (!string.IsNullOrEmpty(Id) && string.IsNullOrEmpty(Name))
87+
{
88+
ResourceIdentifier parsedId = new ResourceIdentifier(Id);
89+
this.Name = parsedId.ResourceName;
90+
}
91+
7692
var parameters = new VirtualMachineCaptureParameters
7793
{
7894
DestinationContainerName = DestinationContainerName,

src/Compute/Compute/VirtualMachine/Action/SetAzureVMCommand.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
using Microsoft.Azure.Commands.Compute.Common;
1818
using Microsoft.Azure.Commands.Compute.Models;
1919
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
20+
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
21+
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
2022

2123
namespace Microsoft.Azure.Commands.Compute
2224
{
@@ -64,9 +66,29 @@ public class SetAzureVMCommand : VirtualMachineBaseCmdlet
6466
[Parameter(
6567
Mandatory = true,
6668
Position = 1,
69+
ParameterSetName = GeneralizeResourceGroupNameParameterSet,
70+
ValueFromPipelineByPropertyName = true,
71+
HelpMessage = "The virtual machine name.")]
72+
[Parameter(
73+
Mandatory = true,
74+
Position = 1,
75+
ParameterSetName = RedeployResourceGroupNameParameterSet,
76+
ValueFromPipelineByPropertyName = true,
77+
HelpMessage = "The virtual machine name.")]
78+
[Parameter(
79+
Mandatory = false,
80+
Position = 1,
81+
ParameterSetName = GeneralizeIdParameterSet,
82+
ValueFromPipelineByPropertyName = true,
83+
HelpMessage = "The virtual machine name.")]
84+
[Parameter(
85+
Mandatory = false,
86+
Position = 1,
87+
ParameterSetName = RedeployIdParameterSet,
6788
ValueFromPipelineByPropertyName = true,
6889
HelpMessage = "The virtual machine name.")]
6990
[ResourceNameCompleter("Microsoft.Compute/virtualMachines", "ResourceGroupName")]
91+
[CmdletParameterBreakingChange("Name", ChangeDescription = "Name will be removed from the Id parameter sets in an upcoming breaking change release.")]
7092
[ValidateNotNullOrEmpty]
7193
public string Name { get; set; }
7294

@@ -99,6 +121,12 @@ public override void ExecuteCmdlet()
99121
{
100122
base.ExecuteCmdlet();
101123

124+
if (!string.IsNullOrEmpty(Id) && string.IsNullOrEmpty(Name))
125+
{
126+
ResourceIdentifier parsedId = new ResourceIdentifier(Id);
127+
this.Name = parsedId.ResourceName;
128+
}
129+
102130
if (this.ParameterSetName.Equals(GeneralizeIdParameterSet) || this.ParameterSetName.Equals(RedeployIdParameterSet))
103131
{
104132
this.ResourceGroupName = GetResourceGroupNameFromId(this.Id);

src/Compute/Compute/VirtualMachine/Action/StartAzureVMCommand.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
using Microsoft.Azure.Commands.Compute.Common;
1818
using Microsoft.Azure.Commands.Compute.Models;
1919
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
20+
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
21+
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
2022

2123
namespace Microsoft.Azure.Commands.Compute
2224
{
@@ -27,9 +29,17 @@ public class StartAzureVMCommand : VirtualMachineActionBaseCmdlet
2729
[Parameter(
2830
Mandatory = true,
2931
Position = 1,
32+
ParameterSetName = ResourceGroupNameParameterSet,
33+
ValueFromPipelineByPropertyName = true,
34+
HelpMessage = "The virtual machine name.")]
35+
[Parameter(
36+
Mandatory = false,
37+
Position = 1,
38+
ParameterSetName = IdParameterSet,
3039
ValueFromPipelineByPropertyName = true,
3140
HelpMessage = "The virtual machine name.")]
3241
[ResourceNameCompleter("Microsoft.Compute/virtualMachines", "ResourceGroupName")]
42+
[CmdletParameterBreakingChange("Name", ChangeDescription = "Name will be removed from the Id parameter set in an upcoming breaking change release.")]
3343
[ValidateNotNullOrEmpty]
3444
public string Name { get; set; }
3545

@@ -40,6 +50,12 @@ public override void ExecuteCmdlet()
4050
base.ExecuteCmdlet();
4151
ExecuteClientAction(() =>
4252
{
53+
if (ParameterSetName.Equals(IdParameterSet) && string.IsNullOrEmpty(Name))
54+
{
55+
ResourceIdentifier parsedId = new ResourceIdentifier(Id);
56+
this.Name = parsedId.ResourceName;
57+
}
58+
4359
var op = this.VirtualMachineClient.StartWithHttpMessagesAsync(
4460
this.ResourceGroupName,
4561
this.Name).GetAwaiter().GetResult();

src/Compute/Compute/VirtualMachine/Action/StopAzureVMCommand.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
using Microsoft.Azure.Commands.Compute.Common;
2121
using Microsoft.Azure.Commands.Compute.Models;
2222
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
23+
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
24+
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
2325

2426
namespace Microsoft.Azure.Commands.Compute
2527
{
@@ -30,9 +32,17 @@ public class StopAzureVMCommand : VirtualMachineActionBaseCmdlet
3032
[Parameter(
3133
Mandatory = true,
3234
Position = 1,
35+
ParameterSetName = ResourceGroupNameParameterSet,
36+
ValueFromPipelineByPropertyName = true,
37+
HelpMessage = "The virtual machine name.")]
38+
[Parameter(
39+
Mandatory = false,
40+
Position = 1,
41+
ParameterSetName = IdParameterSet,
3342
ValueFromPipelineByPropertyName = true,
3443
HelpMessage = "The virtual machine name.")]
3544
[ResourceNameCompleter("Microsoft.Compute/virtualMachines", "ResourceGroupName")]
45+
[CmdletParameterBreakingChange("Name", ChangeDescription = "Name will be removed from the Id parameter set in an upcoming breaking change release.")]
3646
[ValidateNotNullOrEmpty]
3747
public string Name { get; set; }
3848

@@ -57,6 +67,12 @@ public override void ExecuteCmdlet()
5767
if (this.ShouldProcess(Name, VerbsLifecycle.Stop)
5868
&& (this.Force.IsPresent || this.ShouldContinue(Properties.Resources.VirtualMachineStoppingConfirmation, Properties.Resources.VirtualMachineStoppingCaption)))
5969
{
70+
if (ParameterSetName.Equals(IdParameterSet) && string.IsNullOrEmpty(Name))
71+
{
72+
ResourceIdentifier parsedId = new ResourceIdentifier(Id);
73+
this.Name = parsedId.ResourceName;
74+
}
75+
6076
Action<Func<string, string, Dictionary<string, List<string>>, CancellationToken, Task<Rest.Azure.AzureOperationResponse>>> call = f =>
6177
{
6278
var op = f(this.ResourceGroupName, this.Name, null, CancellationToken.None).GetAwaiter().GetResult();

src/Compute/Compute/VirtualMachine/Operation/RemoveAzureVMCommand.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
using Microsoft.Azure.Commands.Compute.Common;
1818
using Microsoft.Azure.Commands.Compute.Models;
1919
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
20+
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
21+
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
2022

2123
namespace Microsoft.Azure.Commands.Compute
2224
{
@@ -28,9 +30,17 @@ public class RemoveAzureVMCommand : VirtualMachineActionBaseCmdlet
2830
[Parameter(
2931
Mandatory = true,
3032
Position = 1,
33+
ParameterSetName = ResourceGroupNameParameterSet,
34+
ValueFromPipelineByPropertyName = true,
35+
HelpMessage = "The resource name.")]
36+
[Parameter(
37+
Mandatory = false,
38+
Position = 1,
39+
ParameterSetName = IdParameterSet,
3140
ValueFromPipelineByPropertyName = true,
3241
HelpMessage = "The resource name.")]
3342
[ResourceNameCompleter("Microsoft.Compute/virtualMachines", "ResourceGroupName")]
43+
[CmdletParameterBreakingChange("Name", ChangeDescription = "Name will be removed from the Id parameter set in an upcoming breaking change release.")]
3444
[ValidateNotNullOrEmpty]
3545
public string Name { get; set; }
3646

@@ -45,6 +55,12 @@ public override void ExecuteCmdlet()
4555
base.ExecuteCmdlet();
4656
ExecuteClientAction(() =>
4757
{
58+
if (!string.IsNullOrEmpty(Id) && string.IsNullOrEmpty(Name))
59+
{
60+
ResourceIdentifier parsedId = new ResourceIdentifier(Id);
61+
this.Name = parsedId.ResourceName;
62+
}
63+
4864
if (this.ShouldProcess(Name, VerbsCommon.Remove)
4965
&& (this.Force.IsPresent ||
5066
this.ShouldContinue(Properties.Resources.VirtualMachineRemovalConfirmation,

src/Compute/Compute/help/Remove-AzVM.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---
1+
---
22
external help file: Microsoft.Azure.PowerShell.Cmdlets.Compute.dll-Help.xml
33
Module Name: Az.Compute
44
ms.assetid: A16C2084-30A4-4AB8-AE22-28CC6E74FD48
@@ -21,7 +21,7 @@ Remove-AzVM [-Name] <String> [-Force] [-ResourceGroupName] <String> [-AsJob]
2121

2222
### IdParameterSetName
2323
```
24-
Remove-AzVM [-Name] <String> [-Force] [-Id] <String> [-AsJob] [-DefaultProfile <IAzureContextContainer>]
24+
Remove-AzVM [[-Name] <String>] [-Force] [-Id] <String> [-AsJob] [-DefaultProfile <IAzureContextContainer>]
2525
[-WhatIf] [-Confirm] [<CommonParameters>]
2626
```
2727

@@ -104,7 +104,7 @@ The resource name.
104104
105105
```yaml
106106
Type: System.String
107-
Parameter Sets: (All)
107+
Parameter Sets: ResourceGroupNameParameterSetName
108108
Aliases: ResourceName, VMName
109109

110110
Required: True
@@ -114,6 +114,18 @@ Accept pipeline input: True (ByPropertyName)
114114
Accept wildcard characters: False
115115
```
116116
117+
```yaml
118+
Type: System.String
119+
Parameter Sets: IdParameterSetName
120+
Aliases: ResourceName, VMName
121+
122+
Required: False
123+
Position: 1
124+
Default value: None
125+
Accept pipeline input: True (ByPropertyName)
126+
Accept wildcard characters: False
127+
```
128+
117129
### -ResourceGroupName
118130
Specifies the name of a resource group.
119131

0 commit comments

Comments
 (0)