Skip to content

Commit f0b08d6

Browse files
authored
Add Support for TenantID with AdminAccount
Adding support for using Tenant ID with AdminAccount in addition to using it with CertSPN
1 parent 5bd9e06 commit f0b08d6

6 files changed

Lines changed: 415 additions & 392 deletions

File tree

InvokeMasterScript.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ Set-ExecutionPolicy -ExecutionPolicy Unrestricted
1313
#Uncomment the below 2 lines if using Admin Credentials to gather data
1414
#-azureStackAdminUsername "<Replace with your service admin account to access the admin portal/apis>" `
1515
#-azureStackAdminPassword "<Replace with your service admin password>"
16-
#Uncomment the below 3 lines if using a SPN Cert to gather data
16+
#Uncomment the below 2 lines if using a SPN Cert to gather data
1717
#-CertificateThumbprint "<Replace with the thumbprint of your cert used for SPN>" `
1818
#-ApplicationId "<Replace with the ClientID of the SPN>" `
19+
#Uncomment the below line if using TenantID as part of sign in of the Management Endpoint
1920
#-TenantId "<Replace with the TenantId for the AzureStack>"
2021

2122

MasterScript.ps1

Lines changed: 140 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,140 @@
1-
<#
2-
.Synopsis
3-
4-
The script that gets called by the ARM template when it deploys a custom script extension.
5-
It sets up a scheduled task to upload usage data to OMS.
6-
7-
.DESCRIPTION
8-
9-
It Sets up git and download repository containing the necessary scripts, stores necessary
10-
information onto the host and then sets up a windows scheduled task to upload usage data
11-
daily.
12-
13-
.EXAMPLE
14-
This script is meant to be called from an ARM template.
15-
.\MasterScript `
16-
-DeploymentGuid <deployment guid> `
17-
-OMSWorkspaceID "myomsworkspaceGUID" `
18-
-OMSSharedKey "myomssharedkeyGUID" `
19-
-azureStackAdminUsername "serviceadmin@contoso.onmicrosoft.com" `
20-
-azureStackAdminPassword $Password `
21-
-CloudName "Cloud#1" `
22-
-Region "local" `
23-
-Fqdn "azurestack.external"
24-
-OEM "HPE"
25-
26-
#>
27-
[CmdletBinding()]
28-
param(
29-
[Parameter( Mandatory = $true)]
30-
[string] $DeploymentGuid,
31-
[Parameter(Mandatory = $true)]
32-
[string] $OMSWorkspaceID,
33-
[Parameter(Mandatory = $true)]
34-
[string] $OMSSharedKey,
35-
[Parameter(ParameterSetName='AdminAccount',Mandatory = $true)]
36-
[string] $azureStackAdminUsername,
37-
[Parameter(ParameterSetName='AdminAccount',Mandatory = $true)]
38-
[string] $azureStackAdminPassword,
39-
[Parameter(Mandatory = $true)]
40-
[string] $CloudName,
41-
[Parameter(Mandatory = $true)]
42-
[string] $Region,
43-
[Parameter(Mandatory = $true)]
44-
[string] $Fqdn,
45-
[Parameter(Mandatory = $true)]
46-
[string] $Oem,
47-
[Parameter(ParameterSetName='CertSPN',Mandatory = $true)]
48-
[string] $CertificateThumbprint,
49-
[Parameter(ParameterSetName='CertSPN',Mandatory = $true)]
50-
[string] $ApplicationId,
51-
[Parameter(ParameterSetName='CertSPN',Mandatory = $true)]
52-
[string] $TenantId
53-
54-
)
55-
if($pscmdlet.ParameterSetName -eq "AdminAccount")
56-
{
57-
$azureStackAdminPasswordSecureString = $azureStackAdminPassword | ConvertTo-SecureString -Force -AsPlainText
58-
}
59-
60-
cd c:\
61-
62-
# install git
63-
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
64-
# refresh the PATH to recognize "choco" command
65-
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
66-
choco install git.install -y
67-
# refresh the PATH to recognize git
68-
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
69-
git clone "https://github.com/Azure-Samples/AzureStack-AdminPowerShell-OMSIntegration.git" C:\AZSAdminOMSInt
70-
71-
72-
# installing powershell modules for azure stack.
73-
# NuGet required for Set-PsRepository PSGallery.
74-
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
75-
Set-PsRepository PSGallery -InstallationPolicy Trusted
76-
Get-Module -ListAvailable | where-Object {$_.Name -like "Azure*"} | Uninstall-Module
77-
Install-Module -Name AzureRm.BootStrapper -Force
78-
Install-Module -Name AzureRm.Resources -Force
79-
Install-Module -Name AzureStack -Force
80-
Install-Module -Name AzureRM.AzureStackAdmin -Force
81-
Install-Module -Name Azs.Infrastructureinsights.Admin -Force
82-
Install-Module -Name Azs.Update.Admin -Force
83-
Install-Module -Name Azs.Fabric.Admin -Force
84-
85-
86-
Switch($pscmdlet.ParameterSetName)
87-
{
88-
"AdminAccount" {
89-
# store data required by scheduled task to use AdminAccount in files.
90-
$info = @{
91-
ParameterSet = $pscmdlet.ParameterSetName;
92-
DeploymentGuid = $DeploymentGuid;
93-
CloudName = $CloudName;
94-
Region = $Region;
95-
Fqdn = $Fqdn;
96-
OmsWorkspaceID = $OMSWorkspaceID;
97-
OmsSharedKey = $OMSSharedKey;
98-
Oem = $Oem;
99-
AzureStackAdminUsername = $azureStackAdminUsername;
100-
101-
}
102-
#store passwords in txt files.
103-
$passwordText = $azureStackAdminPasswordSecureString | ConvertFrom-SecureString
104-
Set-Content -Path "C:\AZSAdminOMSInt\azspassword_$CloudName.txt" -Value $passwordText
105-
}
106-
107-
"CertSPN" {
108-
# store data required by scheduled task to use CertSPN in files.
109-
$info = @{
110-
ParameterSet = $pscmdlet.ParameterSetName;
111-
DeploymentGuid = $DeploymentGuid;
112-
CloudName = $CloudName;
113-
Region = $Region;
114-
Fqdn = $Fqdn;
115-
OmsWorkspaceID = $OMSWorkspaceID;
116-
OmsSharedKey = $OMSSharedKey;
117-
Oem = $Oem;
118-
CertificateThumbprint = $CertificateThumbprint;
119-
ApplicationId = $ApplicationId;
120-
TenantId = $TenantId;
121-
}
122-
}
123-
}
124-
125-
$infoJson = ConvertTo-Json $info
126-
Set-Content -Path "C:\AZSAdminOMSInt\info_$CloudName.txt" -Value $infoJson
127-
128-
129-
#Download Azure Stack Tools VNext
130-
cd c:\AZSAdminOMSInt
131-
invoke-webrequest https://github.com/Azure/AzureStack-Tools/archive/vnext.zip -OutFile vnext.zip
132-
expand-archive vnext.zip -DestinationPath . -Force
133-
134-
# schedule windows scheduled task
135-
cd C:\AZSAdminOMSInt
136-
& .\schedule_usage_upload.ps1 -CloudName $CloudName
1+
<#
2+
.Synopsis
3+
4+
The script that gets called by the ARM template when it deploys a custom script extension.
5+
It sets up a scheduled task to upload usage data to OMS.
6+
7+
.DESCRIPTION
8+
9+
It Sets up git and download repository containing the necessary scripts, stores necessary
10+
information onto the host and then sets up a windows scheduled task to upload usage data
11+
daily.
12+
13+
.EXAMPLE
14+
This script is meant to be called from an ARM template.
15+
.\MasterScript `
16+
-DeploymentGuid <deployment guid> `
17+
-OMSWorkspaceID "myomsworkspaceGUID" `
18+
-OMSSharedKey "myomssharedkeyGUID" `
19+
-azureStackAdminUsername "serviceadmin@contoso.onmicrosoft.com" `
20+
-azureStackAdminPassword $Password `
21+
-CloudName "Cloud#1" `
22+
-Region "local" `
23+
-Fqdn "azurestack.external"
24+
-OEM "HPE"
25+
26+
#>
27+
[CmdletBinding()]
28+
param(
29+
[Parameter( Mandatory = $true)]
30+
[string] $DeploymentGuid,
31+
[Parameter(Mandatory = $true)]
32+
[string] $OMSWorkspaceID,
33+
[Parameter(Mandatory = $true)]
34+
[string] $OMSSharedKey,
35+
[Parameter(ParameterSetName='AdminAccount',Mandatory = $true)]
36+
[string] $azureStackAdminUsername,
37+
[Parameter(ParameterSetName='AdminAccount',Mandatory = $true)]
38+
[string] $azureStackAdminPassword,
39+
[Parameter(Mandatory = $true)]
40+
[string] $CloudName,
41+
[Parameter(Mandatory = $true)]
42+
[string] $Region,
43+
[Parameter(Mandatory = $true)]
44+
[string] $Fqdn,
45+
[Parameter(Mandatory = $true)]
46+
[string] $Oem,
47+
[Parameter(ParameterSetName='CertSPN',Mandatory = $true)]
48+
[string] $CertificateThumbprint,
49+
[Parameter(ParameterSetName='CertSPN',Mandatory = $true)]
50+
[string] $ApplicationId,
51+
[Parameter(Mandatory = $false)]
52+
[string] $TenantId
53+
54+
)
55+
if($pscmdlet.ParameterSetName -eq "AdminAccount")
56+
{
57+
$azureStackAdminPasswordSecureString = $azureStackAdminPassword | ConvertTo-SecureString -Force -AsPlainText
58+
}
59+
60+
cd c:\
61+
62+
# install git
63+
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
64+
# refresh the PATH to recognize "choco" command
65+
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
66+
choco install git.install -y
67+
# refresh the PATH to recognize git
68+
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
69+
git clone "https://github.com/Azure-Samples/AzureStack-AdminPowerShell-OMSIntegration.git" C:\AZSAdminOMSInt
70+
71+
72+
# installing powershell modules for azure stack.
73+
# NuGet required for Set-PsRepository PSGallery.
74+
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
75+
Set-PsRepository PSGallery -InstallationPolicy Trusted
76+
Get-Module -ListAvailable | where-Object {$_.Name -like "Azure*"} | Uninstall-Module
77+
Install-Module -Name AzureRm.BootStrapper -Force
78+
Install-Module -Name AzureRm.Resources -Force
79+
Install-Module -Name AzureStack -Force
80+
Install-Module -Name AzureRM.AzureStackAdmin -Force
81+
Install-Module -Name Azs.Infrastructureinsights.Admin -Force
82+
Install-Module -Name Azs.Update.Admin -Force
83+
Install-Module -Name Azs.Fabric.Admin -Force
84+
85+
86+
Switch($pscmdlet.ParameterSetName)
87+
{
88+
"AdminAccount" {
89+
# store data required by scheduled task to use AdminAccount in files.
90+
$info = @{
91+
ParameterSet = $pscmdlet.ParameterSetName;
92+
DeploymentGuid = $DeploymentGuid;
93+
CloudName = $CloudName;
94+
Region = $Region;
95+
Fqdn = $Fqdn;
96+
OmsWorkspaceID = $OMSWorkspaceID;
97+
OmsSharedKey = $OMSSharedKey;
98+
Oem = $Oem;
99+
AzureStackAdminUsername = $azureStackAdminUsername;
100+
101+
}
102+
if($TenantId)
103+
{#If a TenantId was provided add it to the data that will be stored
104+
$info.Add("TenantId", $TenantId)
105+
}
106+
#store passwords in txt files.
107+
$passwordText = $azureStackAdminPasswordSecureString | ConvertFrom-SecureString
108+
Set-Content -Path "C:\AZSAdminOMSInt\azspassword_$CloudName.txt" -Value $passwordText
109+
}
110+
111+
"CertSPN" {
112+
# store data required by scheduled task to use CertSPN in files.
113+
$info = @{
114+
ParameterSet = $pscmdlet.ParameterSetName;
115+
DeploymentGuid = $DeploymentGuid;
116+
CloudName = $CloudName;
117+
Region = $Region;
118+
Fqdn = $Fqdn;
119+
OmsWorkspaceID = $OMSWorkspaceID;
120+
OmsSharedKey = $OMSSharedKey;
121+
Oem = $Oem;
122+
CertificateThumbprint = $CertificateThumbprint;
123+
ApplicationId = $ApplicationId;
124+
TenantId = $TenantId;
125+
}
126+
}
127+
}
128+
129+
$infoJson = ConvertTo-Json $info
130+
Set-Content -Path "C:\AZSAdminOMSInt\info_$CloudName.txt" -Value $infoJson
131+
132+
133+
#Download Azure Stack Tools VNext
134+
cd c:\AZSAdminOMSInt
135+
invoke-webrequest https://github.com/Azure/AzureStack-Tools/archive/vnext.zip -OutFile vnext.zip
136+
expand-archive vnext.zip -DestinationPath . -Force
137+
138+
# schedule windows scheduled task
139+
cd C:\AZSAdminOMSInt
140+
& .\schedule_usage_upload.ps1 -CloudName $CloudName

OpsDataToOMS.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Switch($Authtype)
3333
$UserName2= $info.AzureStackAdminUsername
3434
$Password2= Get-Content "C:\AZSAdminOMSInt\azspassword_$CloudName.txt"| ConvertTo-SecureString
3535
$Credential2=New-Object PSCredential($UserName2,$Password2)
36+
$TenantId2 = $info.TenantId
3637
}
3738
#Using CertSPN
3839
"CertSPN"{
@@ -56,8 +57,13 @@ Switch($Authtype)
5657
{
5758
#Set to AdminAccount or not set(old info file)
5859
{($_ -eq "AdminAccount") -or ($_ -eq $null)}{
60+
if($TenantId2){#Use TenantID if one was provided
61+
Add-AzureRmAccount -EnvironmentName $cloudName2 -Credential $Credential2 -Tenant $TenantId2
62+
}
63+
else{
5964
Add-AzureRmAccount -EnvironmentName $cloudName2 -Credential $Credential2
6065
}
66+
}
6167
#Using CertSPN
6268
"CertSPN"{
6369
Add-AzureRmAccount -Environment $cloudName2 -ServicePrincipal -CertificateThumbprint $CertificateThumbprint2 -ApplicationId $ApplicationId2 -TenantId $TenantId2

asUsageToOMS.ps1

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
[CmdletBinding()]
2-
param(
3-
[Parameter(Mandatory = $true)]
4-
[string] $CloudName
5-
)
6-
7-
Start-Transcript -Path "C:\AZSAdminOMSInt\asUsageToOMS_$CloudName.log"
8-
& .\usagesummaryjson.ps1 -CloudName $CloudName
9-
10-
# set execution policy and import OMS Ingestion API.
11-
Set-ExecutionPolicy -ExecutionPolicy Bypass -Force
12-
Install-Module -Name AzureRM.OperationalInsights -Force
13-
Install-Module -Name OMSIngestionAPI -Force
14-
15-
& .\uploadToOMS.ps1 -CloudName $CloudName
16-
exit
1+
[CmdletBinding()]
2+
param(
3+
[Parameter(Mandatory = $true)]
4+
[string] $CloudName
5+
)
6+
7+
Start-Transcript -Path "C:\AZSAdminOMSInt\asUsageToOMS_$CloudName.log"
8+
& .\usagesummaryjson.ps1 -CloudName $CloudName
9+
10+
# set execution policy and import OMS Ingestion API.
11+
Set-ExecutionPolicy -ExecutionPolicy Bypass -Force
12+
Install-Module -Name AzureRM.OperationalInsights -Force
13+
Install-Module -Name OMSIngestionAPI -Force
14+
15+
& .\uploadToOMS.ps1 -CloudName $CloudName
16+
exit

uploadToOMS.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Switch($Authtype)
3131
$UserName2= $info.AzureStackAdminUsername
3232
$Password2= Get-Content "C:\AZSAdminOMSInt\azspassword_$CloudName.txt"| ConvertTo-SecureString
3333
$Credential2=New-Object PSCredential($UserName2,$Password2)
34+
$TenantId2 = $info.TenantId
3435
}
3536
#Using CertSPN
3637
"CertSPN"{
@@ -58,8 +59,13 @@ Switch($Authtype)
5859
{
5960
#Set to AdminAccount or not set(old info file)
6061
{($_ -eq "AdminAccount") -or ($_ -eq $null)}{
62+
if($TenantId2){#Use TenantID if one was provided
63+
Add-AzureRmAccount -EnvironmentName $cloudName2 -Credential $Credential2 -Tenant $TenantId2
64+
}
65+
else{
6166
Add-AzureRmAccount -EnvironmentName $cloudName2 -Credential $Credential2
6267
}
68+
}
6369
#Using CertSPN
6470
"CertSPN"{
6571
Add-AzureRmAccount -Environment $cloudName2 -ServicePrincipal -CertificateThumbprint $CertificateThumbprint2 -ApplicationId $ApplicationId2 -TenantId $TenantId2

0 commit comments

Comments
 (0)