Skip to content

Commit 7e177b5

Browse files
author
BradleyBartlett
committed
minor RBAC logging change, updated documentation
(cherry picked from commit cb1086b)
1 parent 547fcee commit 7e177b5

File tree

1 file changed

+79
-45
lines changed

1 file changed

+79
-45
lines changed

Registration/RegisterWithAzure.psm1

Lines changed: 79 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,22 @@ if(-not $Global:AzureRegistrationLog)
2020
<#
2121
.SYNOPSIS
2222
23-
This script can be used to register Azure Stack with Azure. To run this script, you must have a public Azure subscription of any type.
23+
Add-AzsRegistration can be used to register Azure Stack with Azure. To run this function, you must have a public Azure subscription of any type.
2424
You must also have access to an account that is an owner or contributor to that subscription.
2525
2626
.DESCRIPTION
2727
28-
RegisterWithAzure runs scripts already present in Azure Stack from the ERCS VM to connect your Azure Stack to Azure.
28+
Add-AzsRegistration runs scripts already present in Azure Stack from the ERCS VM to connect your Azure Stack to Azure.
2929
After connecting with Azure, you can download products from the marketplace (See the documentation for more information: https://docs.microsoft.com/en-us/azure/azure-stack/azure-stack-download-azure-marketplace-item).
3030
Running this script with default parameters will enable marketplace syndication and usage data will default to being reported to Azure.
31-
To turn these features off see examples below.
31+
NOTE: Default billing model is 'Development' and is only usable for proof of concept builds.
32+
To disable syndication or usage reporting see examples below.
3233
3334
This script will create the following resources by default:
3435
- A service principal to perform resource actions
3536
- A resource group in Azure (if needed)
3637
- A registration resource in the created resource group in Azure
38+
- A custom RBAC role for the resource in Azure
3739
- An activation resource group and resource in Azure Stack
3840
3941
See documentation for more detail: https://docs.microsoft.com/en-us/azure/azure-stack/azure-stack-register
@@ -47,6 +49,10 @@ If not supplied script will request manual input of username and password
4749
4850
The subscription Id that will be used for marketplace syndication and usage. The Azure Account Id used during registration must have resource creation access to this subscription.
4951
52+
.PARAMETER AzureDirectoryTenantName
53+
54+
The Azure tenant directory where you would like your registration resource in Azure to be created.
55+
5056
.PARAMETER JeaComputerName
5157
5258
Just-Enough-Access Computer Name, also known as Emergency Console VM.(Example: AzS-ERCS01 for the ASDK)
@@ -69,15 +75,16 @@ The name of the Azure Environment where resources will be created. Defaults to "
6975
7076
.PARAMETER BillingModel
7177
72-
The billing model that the subscription uses. Select from "Capacity","PayAsYouUse", and "Development". Defaults to "Development". Please see documentation for more information: https://docs.microsoft.com/en-us/azure/azure-stack/azure-stack-billing-and-chargeback
78+
The billing model that the subscription uses. Select from "Capacity","PayAsYouUse", and "Development". Defaults to "Development" which is usable for POC installments.
79+
Please see documentation for more information: https://docs.microsoft.com/en-us/azure/azure-stack/azure-stack-billing-and-chargeback
7380
7481
.PARAMETER MarketplaceSyndicationEnabled
7582
7683
This is a switch that determines if this registration will allow you to download products from the Azure Marketplace. Defaults to $true
7784
7885
.PARAMETER UsageReportingEnabled
7986
80-
This is a switch that determines if usage records are reported to Azure. Defaults to $true
87+
This is a switch that determines if usage records are reported to Azure. Defaults to $true. Note: This cannot be disabled with billing model set to PayAsYouUse.
8188
8289
.PARAMETER AgreementNumber
8390
@@ -87,36 +94,31 @@ Used when the billing model is set to capacity. If this is the case you will nee
8794
8895
This example registers your AzureStack environment with Azure, enables syndication, and enables usage reporting to Azure.
8996
90-
.\RegisterWithAzure.ps1 -CloudAdminCredential $CloudAdminCredential -AzureSubscriptionId $SubscriptionId -JeaComputername "Azs-ERCS01"
97+
Add-AzsRegistration -CloudAdminCredential $CloudAdminCredential -AzureSubscriptionId $SubscriptionId -AzureDirectoryTenantName "contoso.onmicrosoft.com" -JeaComputername "Azs-ERCS01"
9198
9299
.EXAMPLE
93100
94101
This example registers your AzureStack environment with Azure, enables syndication, and disables usage reporting to Azure.
95102
96-
.\RegisterWithAzure.ps1 -CloudAdminCredential $CloudAdminCredential -AzureSubscriptionId $SubscriptionId -JeaComputername "Azs-ERC01" -UsageReportingEnabled:$false
103+
Add-AzsRegistration -CloudAdminCredential $CloudAdminCredential -AzureSubscriptionId $SubscriptionId -AzureDirectoryTenantName "contoso.onmicrosoft.com" -JeaComputername "Azs-ERCS01" -BillingMode 'Capacity' -UsageReportingEnabled:$false -AgreementNumber $MyAgreementNumber
97104
98105
.EXAMPLE
99106
100107
This example registers your AzureStack environment with Azure, enables syndication and usage and gives a specific name to the resource group and registration resource.
101108
102-
.\RegisterWithAzure.ps1 -CloudAdminCredential $CloudAdminCredential -AzureSubscriptionId $SubscriptionId -JeaComputername "<PreFix>-ERCS01" -ResourceGroupName "ContosoStackRegistrations" -RegistrationName "Registration01"
109+
Add-AzsRegistration -CloudAdminCredential $CloudAdminCredential -AzureSubscriptionId $SubscriptionId -AzureDirectoryTenantName "contoso.onmicrosoft.com" -JeaComputername "Azs-ERCS02" -ResourceGroupName "ContosoStackRegistrations" -RegistrationName "ContosoRegistration"
103110
104111
.EXAMPLE
105112
106-
This example un-registers by disabling syndication and stopping usage sent to Azure. Note that usage will still be collected, just not sent to Azure.
113+
This example disables syndication and disables usage reporting to Azure. Note that usage will still be collected, just not sent to Azure.
107114
108-
.\RegisterWithAzure.ps1 -CloudAdminCredential $CloudAdminCredential -AzureSubscriptionId $SubscriptionId -JeaComputername "<Prefix>-ERC01" -MarketplaceSyndicationEnabled:$false -UsageReportingEnabled:$false
115+
Add-AzsRegistration -CloudAdminCredential $CloudAdminCredential -AzureSubscriptionId $SubscriptionId -AzureDirectoryTenantName "contoso.onmicrosoft.com" -JeaComputername "Azs-ERCS01" -BillingModel Development -MarketplaceSyndicationEnabled:$false -UsageReportingEnabled:$false
109116
110117
.NOTES
111118
112-
If you would like to un-Register with you Azure by turning off marketplace syndication and usage reporting you can run this script again with both enableSyndication and reportUsage set to false.
113-
This will unconfigure usage bridge so that syndication isn't possible and usage data is not reported. This is only possible with billing model of Development or Capacity.
119+
If you would like to un-Register with you Azure by turning off marketplace syndication, disabling usage reporting, and removing the registration resource from Azure you can run Remove-AzsRegistration.
114120
115-
If you would like to use a different subscription for registration there are two functions to be run before re-registering:
116-
- Add-RegistrationRoleAssignment: Use this function If your next subscription Id is under the same account as the current registration
117-
- Remove-RegistrationResource: Use this function if your next subscription Id is under a different account than the current registration
118-
119-
Once you have run the appropriate function you can call RegisterWithAzure again to re-register.
121+
If you would like to use a different subscription for registration you can run Set-AzsRegistrationSubscription
120122
121123
#>
122124

@@ -186,14 +188,12 @@ Function Add-AzsRegistration{
186188
187189
.SYNOPSIS
188190
189-
Removes a registration resource from Azure
191+
Sets current registration parameters MarketplaceSyndicationEnabled and EnableUsageReporting to $false, then removes registration resource from Azure.
190192
191193
.DESCRIPTION
192194
193195
If no registration resource name is supplied then then this script will use this environments CloudId to search for a registration resource and remove it from Azure.
194-
If a RegistrationName and ResourceGroupName are supplied this script will remove the specified registration resource from Azure. This will disable marketplace syndication
195-
and allow you to run RegisterWithAzure with a different subscription Id. Note: If the provided subscription for a subsequent RegisterWithAzure is under the same Azure Account
196-
as the previous registration you MUST run Add-RegistrationRoleAssignment before attempting RegisterWithAzure.
196+
If a RegistrationName and ResourceGroupName are supplied this script will remove the specified registration resource from Azure.
197197
198198
.PARAMETER CloudAdminCredential
199199
@@ -204,6 +204,10 @@ If not supplied script will request manual input of username and password.
204204
205205
The subscription Id that was previously used to register this Azure Stack environment with Azure.
206206
207+
.PARAMETER AzureDirectoryTenantName
208+
209+
The Azure tenant directory previously used to register this Azure Stack environment with Azure.
210+
207211
.PARAMETER JeaComputerName
208212
209213
Just-Enough-Access Computer Name, also known as Emergency Console VM.(Example: AzS-ERCS01 for the ASDK).
@@ -214,23 +218,21 @@ This is the name of the resource group in Azure where the previous registration
214218
215219
.PARAMETER RegistrationName
216220
217-
This is the name of the previous registration resource that was created in Azure. This resource will be removed Defaults to "AzureStack-<CloudId>"
221+
This is the name of the previous registration resource that was created in Azure. This resource will be removed. Defaults to "AzureStack-<CloudId>"
218222
219223
.PARAMETER AzureEnvironmentName
220224
221225
The name of the Azure Environment where registration resources have been created. Defaults to "AzureCloud"
222226
223227
.EXAMPLE
224228
225-
This example removes a registration resource in Azure that was created from a prior successful run of RegisterWithAzure and uses defaults for RegistrationName and ResourceGroupName.
229+
This example removes a registration resource in Azure that was created from a prior successful run of Add-AzsRegistration and uses defaults for RegistrationName and ResourceGroupName.
226230
227-
Remove-RegistrationResource -CloudAdminCredential $CloudAdminCredential -AzureSubscriptionId $AzureSubscriptionId -JeaComputerName $JeaComputerName
231+
Remove-AzsRegistration -CloudAdminCredential $CloudAdminCredential -AzureSubscriptionId $AzureSubscriptionId -AzureDirectoryTenantName 'contoso.onmicrosoft.com' -JeaComputerName $JeaComputerName
228232
229233
.NOTES
230234
231-
This script should be used in conjuction with running RegisterWithAzure to disable marketplace syndication and usage reporting (if able). If after running this script
232-
you attempt to re-register with a different subscription Id that is under the same account as the previous registration you will recieve an error related to the custom
233-
RBAC role for registration resources. To fix this, please run Add-RegistrationRoleAssignment to re-register with a subscription under the previously registered account.
235+
This will always set syndication and usage reporting to false as well as remove the provided registration resource from Azure.
234236
235237
#>
236238

@@ -338,34 +340,61 @@ function Remove-AzsRegistration{
338340
339341
.SYNOPSIS
340342
341-
This script is used to prepare the current environment for registering with a new subscription Id under the same account.
343+
Set-AzsRegistrationSubscription calls Remove-AzsRegistration on the current registration resource and then calls Add-AzsRegistration with the new parameters
342344
343345
.DESCRIPTION
344346
345-
Add-RegistrationRoleAssignment will add the provided alternate subscription Id to the list of assignable scopes for the custom RBAC role that is defined and assigned to registration resources.
346-
This RBAC role is created / assigned during the RegisterWithAzure function.
347+
Set-AzsRegistrationSubsription requires the parameters for the current registration as well as parameters for a new registration resource. The function
348+
attempts to add the custom RBAC role created during Add-AzsRegistration to the new subscription passed in. If not possible the function will continue as normal.
349+
Set-AzsRegistrationSubscription will call Remove-AzsRegistration on the current registration resource and then pass the new subscription Id and new
350+
Azure directory tenant name into Add-AzsRegistration.
347351
348-
.PARAMETER AzureSubscriptionId
352+
.PARAMETER CloudAdminCredential
353+
354+
Powershell object that contains credential information i.e. user name and password.The CloudAdmin has access to the JEA Computer (also known as Emergency Console) to call whitelisted cmdlets and scripts.
355+
If not supplied script will request manual input of username and password.
356+
357+
.PARAMETER CurrentAzureSubscriptionId
349358
350359
The subscription Id that was previously used to register this Azure Stack environment with Azure.
351360
352-
.PARAMETER AlternateSubscriptionId
361+
.PARAMETER AzureDirectoryTenantName
362+
363+
The Azure tenant directory previously used to register this Azure Stack environment with Azure.
353364
354-
The new subscription Id that this environment will be registered to in Azure.
365+
.PARAMETER NewAzureSubscriptionId
366+
367+
The subscription Id you would like to change your registration to.
368+
369+
.PARAMETER JeaComputerName
370+
371+
Just-Enough-Access Computer Name, also known as Emergency Console VM.(Example: AzS-ERCS01 for the ASDK).
372+
373+
.PARAMETER NewAzureDirectoryTenantName
374+
375+
The new Azure tenant directory you would like used during registration. This can be the same as the previous tenant name.
376+
377+
.PARAMETER ResourceGroupName
378+
379+
This is the name of the resource group in Azure where the previous registration resource was stored. Defaults to "azurestack"
380+
381+
.PARAMETER RegistrationName
382+
383+
This is the name of the previous registration resource that was created in Azure. This resource will be removed. Defaults to "AzureStack-<CloudId>"
355384
356385
.PARAMETER AzureEnvironmentName
357386
358387
The name of the Azure Environment where registration resources have been created. Defaults to "AzureCloud"
359388
360389
.EXAMPLE
361390
362-
Add-RegistrationRoleAssignment -AzureSubscriptionId $CurrentRegisteredSubscription -AlternateSubscriptionId $FutureRegisteredSubscription
391+
Set-AzsRegistrationSubscription -CloudAdminCredential $CloudAdminCredential -CurrentAzureSubscriptionId $CurrentSubscriptionId -AzureDirectoryTenantName 'contoso.onmicrosoft.com' -NewAzureSubscriptionId $NewAzureSubscriptionId `
392+
-JeaComputerName <Prefix>-ERCS01 -NewAzureDirectoryTenantname 'microsoft.onmicrosoft.com'
363393
364394
.NOTES
365395
366-
This function should only be used if you have a currently registered environment and would like to switch the subscription used to register to a different subscription
367-
that is under the same account. If you would like to register to a subscription Id that is under a separate account then you must use Remove-RegistrationResource before
368-
calling RegisterWithAzure again.
396+
If you would like to register with a different resource group, resource name, or resource group location you cannot currently use Set-AzsRegistrationSubsription for that.
397+
To do so you should call Remove-AzsRegistration followed by Add-AzsRegistration with the new parameters you would like.
369398
370399
#>
371400

@@ -432,7 +461,7 @@ function Set-AzsRegistrationSubscription{
432461
Log-Output "Logging in to Azure."
433462
$connection = Connect-AzureAccount -SubscriptionId $CurrentAzureSubscriptionId -AzureEnvironment $AzureEnvironmentName -AzureDirectoryTenantName $AzureDirectoryTenantName -Verbose
434463

435-
$role = Get-AzureRmRoleDefinition -Name 'Registration Reader'
464+
$role = Get-AzureRmRoleDefinition -Name 'Registration Reader' -Scope "/subscriptions/$CurrentAzureSubscriptionId"
436465
if($role)
437466
{
438467
if(-not($role.AssignableScopes -icontains "/subscriptions/$NewAzureSubscriptionId"))
@@ -445,9 +474,9 @@ function Set-AzsRegistrationSubscription{
445474
}
446475
catch
447476
{
448-
if($_.Exception -ilike "*LinkedAuthorizationFailed:*")
477+
if($_.Exception -ilike "*AuthorizationFailed:*")
449478
{
450-
Log-Warning "Unable to add the new subscription: $NewAzureSubscriptionId to the scope of existing RBAC role definition. Continuing with transfer of registration"
479+
Log-Warning "Unable to add the new subscription: $NewAzureSubscriptionId to the scope of existing RBAC role definition. Continuing with transfer of registration `r`n$($_.Exception)"
451480
}
452481
else
453482
{
@@ -753,8 +782,8 @@ Determines if a new Azure connection is required.
753782
754783
.DESCRIPTION
755784
756-
If the current powershell environment is not currently logged in to an Azure Account or is calling either RegisterWithAzure or
757-
Initialize-AlternateRegistration with a subscription id that does not match the current environment's subscription then Connect-AzureAccount will prompt the user to log in
785+
If the current powershell environment is not currently logged in to an Azure Account or is calling Add-AzsRegistration
786+
with a subscription id that does not match one available under the current context then Connect-AzureAccount will prompt the user to log in
758787
to the correct account.
759788
760789
#>
@@ -1003,23 +1032,28 @@ function Get-TenantIdFromName
10031032

10041033
$response = Invoke-RestMethod -Uri $uri -Method Get -Verbose
10051034

1006-
Write-Verbose -Message "using token_endpoint $($response.token_endpoint) to parse tenant id" -Verbose
10071035
$tenantId = $response.token_endpoint.Split('/')[3]
10081036

10091037
$tenantIdGuid = [guid]::NewGuid()
10101038
$result = [guid]::TryParse($tenantId, [ref] $tenantIdGuid)
10111039

10121040
if(-not $result)
10131041
{
1014-
Write-Error "Error obtaining tenant id from tenant name"
1042+
Log-Throw -Message "Error obtaining tenant id from tenant name $tenantName `r`n$($_.Exception)" -CallingFunction $PSCmdlet.MyInvocation.InvocationName
10151043
}
10161044
else
10171045
{
1018-
Write-Verbose -Message "Tenant Name: $tenantName Tenant id: $tenantId" -Verbose
1046+
Log-Output "Tenant Name: $tenantName Tenant id: $tenantId" -Verbose
10191047
return $tenantId
10201048
}
10211049
}
10221050

1051+
<#
1052+
.SYNOPSIS
1053+
1054+
Returns the common AzureURIs associated with the provided AzureEnvironmentName
1055+
1056+
#>
10231057
function Get-AzureURIs
10241058
{
10251059
[CmdletBinding()]

0 commit comments

Comments
 (0)