Skip to content

Commit cc7d1b0

Browse files
authored
Merge pull request #7592 from praries880/7479_fix
Fix for issue #7479
2 parents e15dc5b + 706fffc commit cc7d1b0

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/ResourceManager/Resources/Commands.Resources/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Current Release
21+
* Fix isssue where Get-AzureRMRoleDefinition throws an unintelligible excpetion (when the default profile has no subscription in it and no scope is specified) by adding a meaningful exception in the scenario. Also set the default param set to `RoleDefinitionNameParameterSet`.
2122

2223
## Version 6.6.0
2324
* Add missing -Mode parameter to Set-AzureRmPolicyDefinition

src/ResourceManager/Resources/Commands.Resources/RoleDefinitions/GetAzureRoleDefinitionCommand.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace Microsoft.Azure.Commands.Resources
2525
/// <summary>
2626
/// Get the available role Definitions for certain resource types.
2727
/// </summary>
28-
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "RoleDefinition"), OutputType(typeof(PSRoleDefinition))]
28+
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "RoleDefinition", DefaultParameterSetName = ParameterSet.RoleDefinitionName), OutputType(typeof(PSRoleDefinition))]
2929
public class GetAzureRoleDefinitionCommand : ResourcesBaseCmdlet
3030
{
3131
[Parameter(Position = 0, Mandatory = false, ValueFromPipelineByPropertyName = true, ParameterSetName = ParameterSet.RoleDefinitionName, HelpMessage = "Role definition name. For e.g. Reader, Contributor, Virtual Machine Contributor.")]
@@ -54,15 +54,25 @@ public override void ExecuteCmdlet()
5454
Scope = Scope,
5555
ResourceIdentifier = new ResourceIdentifier
5656
{
57-
Subscription = DefaultProfile.DefaultContext.Subscription.Id.ToString()
57+
Subscription = DefaultProfile.DefaultContext.Subscription.Id?.ToString()
5858
},
5959
RoleDefinitionId = Id,
6060
RoleDefinitionName = Name,
6161
};
6262

63+
if (options.Scope == null && options.ResourceIdentifier.Subscription == null)
64+
{
65+
WriteTerminatingError("No subscription was found in the default profile and no scope was specified. Either specify a scope or use a tenant with a subscription to run the command.");
66+
}
67+
6368
AuthorizationClient.ValidateScope(options.Scope, true);
6469

6570
WriteObject(PoliciesClient.FilterRoleDefinitions(options), enumerateCollection: true);
6671
}
72+
73+
private void WriteTerminatingError(string message, params object[] args)
74+
{
75+
ThrowTerminatingError(new ErrorRecord(new Exception(String.Format(message, args)), "Error", ErrorCategory.NotSpecified, null));
76+
}
6777
}
6878
}

0 commit comments

Comments
 (0)