1212// limitations under the License.
1313// ----------------------------------------------------------------------------------
1414
15- using System . Management . Automation ;
15+ using Microsoft . Azure . Commands . Common . Authentication . Models ;
1616using Microsoft . Azure . Commands . Profile . Models ;
17+ using Microsoft . Azure . Commands . Profile . Properties ;
1718using Microsoft . Azure . Commands . ResourceManager . Common ;
1819using Microsoft . WindowsAzure . Commands . Common ;
19- using Microsoft . Azure . Commands . Profile . Properties ;
2020using System ;
21- using Microsoft . Azure . Commands . Common . Authentication . Models ;
21+ using System . Collections . ObjectModel ;
22+ using System . Management . Automation ;
2223
2324namespace Microsoft . Azure . Commands . Profile
2425{
2526 /// <summary>
26- /// Cmdlet to change current Azure context.
27+ /// Cmdlet to change current Azure context.
2728 /// </summary>
2829 [ Cmdlet ( VerbsCommon . Set , "AzureRmContext" , DefaultParameterSetName = SubscriptionNameParameterSet ) ]
2930 [ Alias ( "Select-AzureRmSubscription" ) ]
3031 [ OutputType ( typeof ( PSAzureContext ) ) ]
31- public class SetAzureRMContextCommand : AzureRMCmdlet
32+ public class SetAzureRMContextCommand : AzureRMCmdlet , IDynamicParameters
3233 {
3334 private const string SubscriptionNameParameterSet = "SubscriptionName" ;
3435 private const string SubscriptionIdParameterSet = "SubscriptionId" ;
3536 private const string ContextParameterSet = "Context" ;
36-
37- [ Parameter ( ParameterSetName = SubscriptionNameParameterSet , Mandatory = false , HelpMessage = "TenantId name or ID" , ValueFromPipelineByPropertyName = true ) ]
38- [ Parameter ( ParameterSetName = SubscriptionIdParameterSet , Mandatory = false , HelpMessage = "TenantId name or ID" , ValueFromPipelineByPropertyName = true ) ]
39- [ Alias ( "Domain" ) ]
40- [ ValidateNotNullOrEmpty ]
41- public string TenantId { get ; set ; }
42-
43- [ Parameter ( ParameterSetName = SubscriptionIdParameterSet , Mandatory = false , HelpMessage = "Subscription" , ValueFromPipelineByPropertyName = true ) ]
44- [ ValidateNotNullOrEmpty ]
45- public string SubscriptionId { get ; set ; }
37+ private RuntimeDefinedParameter _tenantId ;
38+ private RuntimeDefinedParameter _subscriptionId ;
4639
4740 [ Parameter ( ParameterSetName = SubscriptionNameParameterSet , Mandatory = false , HelpMessage = "Subscription Name" , ValueFromPipelineByPropertyName = true ) ]
4841 [ ValidateNotNullOrEmpty ]
@@ -51,6 +44,22 @@ public class SetAzureRMContextCommand : AzureRMCmdlet
5144 [ Parameter ( ParameterSetName = ContextParameterSet , Mandatory = true , HelpMessage = "Context" , ValueFromPipeline = true ) ]
5245 public PSAzureContext Context { get ; set ; }
5346
47+ private string TenantId
48+ {
49+ get
50+ {
51+ return _tenantId == null ? null : ( string ) _tenantId . Value ;
52+ }
53+ }
54+
55+ private string SubscriptionId
56+ {
57+ get
58+ {
59+ return _subscriptionId == null ? null : ( string ) _subscriptionId . Value ;
60+ }
61+ }
62+
5463 public override void ExecuteCmdlet ( )
5564 {
5665 if ( ParameterSetName == ContextParameterSet )
@@ -60,7 +69,7 @@ public override void ExecuteCmdlet()
6069 }
6170 else if ( ParameterSetName == SubscriptionNameParameterSet || ParameterSetName == SubscriptionIdParameterSet )
6271 {
63- if ( string . IsNullOrWhiteSpace ( SubscriptionId )
72+ if ( string . IsNullOrWhiteSpace ( SubscriptionId )
6473 && string . IsNullOrWhiteSpace ( SubscriptionName )
6574 && string . IsNullOrWhiteSpace ( TenantId ) )
6675 {
@@ -82,7 +91,7 @@ public override void ExecuteCmdlet()
8291 AzureRmProfileProvider . Instance . Profile . Context . Subscription != null &&
8392 AzureRmProfileProvider . Instance . Profile . Context . Subscription . State != null &&
8493 ! AzureRmProfileProvider . Instance . Profile . Context . Subscription . State . Equals (
85- "Enabled" ,
94+ "Enabled" ,
8695 StringComparison . OrdinalIgnoreCase ) )
8796 {
8897 WriteWarning ( string . Format (
@@ -91,5 +100,55 @@ public override void ExecuteCmdlet()
91100 }
92101 WriteObject ( ( PSAzureContext ) AzureRmProfileProvider . Instance . Profile . Context ) ;
93102 }
103+
104+ public object GetDynamicParameters ( )
105+ {
106+ return CreateDynamicParameterDictionary ( ) ;
107+ }
108+
109+ private RuntimeDefinedParameterDictionary CreateDynamicParameterDictionary ( )
110+ {
111+ var runtimeDefinedParameterDictionary = new RuntimeDefinedParameterDictionary ( ) ;
112+
113+ var subscriptionIdAttributes = new Collection < Attribute >
114+ {
115+ new ParameterAttribute
116+ {
117+ ParameterSetName = SubscriptionIdParameterSet ,
118+ Mandatory = false ,
119+ HelpMessage = "Subscription" ,
120+ ValueFromPipelineByPropertyName = true
121+ } ,
122+ new ValidateSetAttribute ( AzureRmProfileProvider . Instance . Profile . Context . Account . GetPropertyAsArray ( AzureAccount . Property . Subscriptions ) ) ,
123+ } ;
124+
125+ var tenantIdAttributes = new Collection < Attribute >
126+ {
127+ new ParameterAttribute
128+ {
129+ ParameterSetName = SubscriptionNameParameterSet ,
130+ Mandatory = false ,
131+ HelpMessage = "TenantId name or ID" ,
132+ ValueFromPipelineByPropertyName = true
133+ } ,
134+ new ParameterAttribute
135+ {
136+ ParameterSetName = SubscriptionIdParameterSet ,
137+ Mandatory = false ,
138+ HelpMessage = "TenantId name or ID" ,
139+ ValueFromPipelineByPropertyName = true
140+ } ,
141+ new AliasAttribute ( "Domain" ) ,
142+ new ValidateSetAttribute ( AzureRmProfileProvider . Instance . Profile . Context . Account . GetPropertyAsArray ( AzureAccount . Property . Tenants ) ) ,
143+ } ;
144+
145+ _tenantId = new RuntimeDefinedParameter ( "TenantId" , typeof ( string ) , tenantIdAttributes ) ;
146+ _subscriptionId = new RuntimeDefinedParameter ( "SubscriptionId" , typeof ( string ) , subscriptionIdAttributes ) ;
147+
148+ runtimeDefinedParameterDictionary . Add ( "SubscriptionId" , _subscriptionId ) ;
149+ runtimeDefinedParameterDictionary . Add ( "TenantId" , _tenantId ) ;
150+
151+ return runtimeDefinedParameterDictionary ;
152+ }
94153 }
95154}
0 commit comments