-
Notifications
You must be signed in to change notification settings - Fork 26
/
MasterScript.ps1
144 lines (125 loc) · 5.61 KB
/
MasterScript.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<#
.Synopsis
The script that gets called by the ARM template when it deploys a custom script extension.
It sets up a scheduled task to upload usage data to OMS.
.DESCRIPTION
It Sets up git and download repository containing the necessary scripts, stores necessary
information onto the host and then sets up a windows scheduled task to upload usage data
daily.
.EXAMPLE
This script is meant to be called from an ARM template.
.\MasterScript `
-DeploymentGuid <deployment guid> `
-OMSWorkspaceID "myomsworkspaceGUID" `
-OMSSharedKey "myomssharedkeyGUID" `
-azureStackAdminUsername "[email protected]" `
-azureStackAdminPassword $Password `
-CloudName "Cloud#1" `
-Region "local" `
-Fqdn "azurestack.external"
-OEM "HPE"
#>
[CmdletBinding()]
param(
[Parameter( Mandatory = $true)]
[string] $DeploymentGuid,
[Parameter(Mandatory = $true)]
[string] $OMSWorkspaceID,
[Parameter(Mandatory = $true)]
[string] $OMSSharedKey,
[Parameter(ParameterSetName='AdminAccount',Mandatory = $true)]
[string] $azureStackAdminUsername,
[Parameter(ParameterSetName='AdminAccount',Mandatory = $true)]
[string] $azureStackAdminPassword,
[Parameter(Mandatory = $true)]
[string] $CloudName,
[Parameter(Mandatory = $true)]
[string] $Region,
[Parameter(Mandatory = $true)]
[string] $Fqdn,
[Parameter(Mandatory = $true)]
[string] $Oem,
[Parameter(ParameterSetName='CertSPN',Mandatory = $true)]
[string] $CertificateThumbprint,
[Parameter(ParameterSetName='CertSPN',Mandatory = $true)]
[string] $ApplicationId,
[Parameter(ParameterSetName='CertSPN',Mandatory = $true)]
[Parameter(ParameterSetName='AdminAccount',Mandatory = $false)]
[string] $TenantId
)
if($pscmdlet.ParameterSetName -eq "AdminAccount")
{
$azureStackAdminPasswordSecureString = $azureStackAdminPassword | ConvertTo-SecureString -Force -AsPlainText
}
cd c:\
try {
# Set TLS 1.2 (3072) as that is the minimum required by Chocolatey.org.
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
} catch {
Write-Output 'Unable to set PowerShell to use TLS 1.2. This is required for contacting Chocolatey as of 03 FEB 2020. https://chocolatey.org/blog/remove-support-for-old-tls-versions. If you see underlying connection closed or trust errors, you may need to do one or more of the following: (1) upgrade to .NET Framework 4.5+ and PowerShell v3+, (2) Call [System.Net.ServicePointManager]::SecurityProtocol = 3072'
}
# install git
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
# refresh the PATH to recognize "choco" command
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
choco install git.install -y
# refresh the PATH to recognize git
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
git clone "https://github.com/Azure-Samples/AzureStack-AdminPowerShell-OMSIntegration.git" C:\AZSAdminOMSInt
# installing powershell modules for azure stack.
# NuGet required for Set-PsRepository PSGallery.
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
Set-PsRepository PSGallery -InstallationPolicy Trusted
Get-Module -ListAvailable | where-Object {$_.Name -like "Azure*"} | Uninstall-Module
Install-Module -Name AzureRm.BootStrapper -Force
Use-AzureRmProfile -Profile 2019-03-01-hybrid -Force
Install-Module -Name AzureStack -RequiredVersion 1.8.3
Switch($pscmdlet.ParameterSetName)
{
"AdminAccount" {
# store data required by scheduled task to use AdminAccount in files.
$info = @{
ParameterSet = $pscmdlet.ParameterSetName;
DeploymentGuid = $DeploymentGuid;
CloudName = $CloudName;
Region = $Region;
Fqdn = $Fqdn;
OmsWorkspaceID = $OMSWorkspaceID;
OmsSharedKey = $OMSSharedKey;
Oem = $Oem;
AzureStackAdminUsername = $azureStackAdminUsername;
}
if($TenantId)
{#If a TenantId was provided add it to the data that will be stored
$info.Add("TenantId", $TenantId)
}
#store passwords in txt files.
$passwordText = $azureStackAdminPasswordSecureString | ConvertFrom-SecureString
Set-Content -Path "C:\AZSAdminOMSInt\azspassword_$CloudName.txt" -Value $passwordText
}
"CertSPN" {
# store data required by scheduled task to use CertSPN in files.
$info = @{
ParameterSet = $pscmdlet.ParameterSetName;
DeploymentGuid = $DeploymentGuid;
CloudName = $CloudName;
Region = $Region;
Fqdn = $Fqdn;
OmsWorkspaceID = $OMSWorkspaceID;
OmsSharedKey = $OMSSharedKey;
Oem = $Oem;
CertificateThumbprint = $CertificateThumbprint;
ApplicationId = $ApplicationId;
TenantId = $TenantId;
}
}
}
$infoJson = ConvertTo-Json $info
Set-Content -Path "C:\AZSAdminOMSInt\info_$CloudName.txt" -Value $infoJson
#Download Azure Stack Tools VNext
cd c:\AZSAdminOMSInt
invoke-webrequest https://github.com/Azure/AzureStack-Tools/archive/vnext.zip -OutFile vnext.zip
expand-archive vnext.zip -DestinationPath . -Force
# schedule windows scheduled task
cd C:\AZSAdminOMSInt
& .\schedule_usage_upload.ps1 -CloudName $CloudName