-
Notifications
You must be signed in to change notification settings - Fork 0
/
Run.ps1
200 lines (159 loc) · 9.52 KB
/
Run.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#region variables
$resourceGroupName="SP2016Dev"
$location="WestEurope"
$sharepointBinaryUrl='https://itsokov.blob.core.windows.net/installblob/officeserver.img'
$sqlBinaryUrl='https://itsokov.blob.core.windows.net/installblob/SQLServer2016SP2-FullSlipstream-x64-ENU.iso'
$storageAccountShareName="assets"
$randSAName= -join ((97..122) | Get-Random -Count 9 | % {[char]$_})
$SASKU = 'Standard_LRS'
$yourAdminPassword=Read-Host -Prompt "Please enter the password you will use for all accounts"
$VirtNetName = 'VNPOC1'
$VMName = -join ((97..122) | Get-Random -Count 9 | % {[char]$_})
$VMSize ="Standard_DS3_v2"
$ServerSKU="2016-Datacenter"
$netbiosname='contoso'
$setupAccount='sp_setup'
$scriptsContainer="scripts"
$gitHubAssets='https://github.com/itsokov/AzureSharePoint2016Install/archive/master.zip'
#endregion
filter timestamp {"$(Get-Date -Format G): $_"}
if (Get-Module -ListAvailable -Name AzureRM) {
Write-Output "Azure RM Module exists. Continuing with the next steps." | timestamp
} else {
Write-Output "Module does not exist. Installing..." | timestamp
Install-Module AzureRM
}
Login-AzureRmAccount
Write-Output "Installation has started, check back in 1.5 hours" | timestamp
Write-Output "Selecting first available subscription." | timestamp
(Get-AzureRmSubscription)[0] | Select-AzureRmSubscription
$resourceGroup=New-AzureRmResourceGroup "$resourceGroupName" -Location $location
$storageAcct=New-AzureRmStorageAccount -Name $randSAName -ResourceGroupName $resourceGroupName -SkuName $SASKU -Location $location
$ScriptBlobKey = Get-AzureRmStorageAccountKey -ResourceGroupName $resourceGroupName -AccountName $randSAName
$ScriptBlobKey=$ScriptBlobKey[0].Value
start-sleep -Seconds 10
#Create Resources for new deployment
Write-Output "Setting up VM resources and variables" | timestamp
#Get latest image
$AzureImageSku = Get-AzureRmVMImage -Location $location -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Skus $ServerSKU
$AzureImageSku = $AzureImageSku | Sort-Object Version -Descending #put the newest first which is the highest patched version
$AzureImage = $AzureImageSku[0] #Newest
#Create a Virtual Network
$subnet = New-AzureRmVirtualNetworkSubnetConfig -Name 'StaticSub' -AddressPrefix "10.10.1.0/24"
$vnet = New-AzureRmVirtualNetwork -Force -Name $VirtNetName -ResourceGroupName $resourceGroupName `
-Location $location -AddressPrefix "10.10.0.0/16" -Subnet $subnet # -DnsServer "10.10.1.10" don't set yet
#If VM points to itself and not offering DNS yet the agents will hang during install
#Create VM
$vm = New-AzureRmVMConfig -VMName $VMName -VMSize $VMSize
#Create NIC
#For demo for easy access give a public IP
$pip = New-AzureRmPublicIpAddress -ResourceGroupName $resourceGroupName -Name ('PubIP' + $VMName) `
-Location $Location -AllocationMethod Dynamic -DomainNameLabel $vmname.ToLower()
$nic = New-AzureRmNetworkInterface -Force -Name ('nic' + $VMName) -ResourceGroupName $resourceGroupName `
-Location $Location -SubnetId $vnet.Subnets[0].Id -PrivateIpAddress 10.10.1.10 `
-PublicIpAddressId $pip.Id
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
$osDiskName = $VMName+'-OSDisk'
$osDiskCaching = 'ReadWrite'
$osDiskVhdUri = "https://$randSAName.blob.core.windows.net/vhds/"+$VMName+"-OS.vhd"
# Setup OS & Image
$user = $setupAccount
$password = $yourAdminPassword
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword)
$vm = Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $VMName -Credential $cred
$vm = Set-AzureRmVMSourceImage -VM $vm -PublisherName $AzureImage.PublisherName -Offer $AzureImage.Offer -Skus $AzureImage.Skus -Version $AzureImage.Version
$vm = Set-AzureRmVMOSDisk -VM $vm -VhdUri $osDiskVhdUri -name $osDiskName -CreateOption fromImage -Caching $osDiskCaching
$vm = Set-AzureRmVMBootDiagnostics -VM $vm -Disable
#Add two data disks
$dataDisk1VhdUri = "https://$randSAName.blob.core.windows.net/vhds/"+$VMName+"-Data1.vhd"
$dataDisk1Name = $VMName+'-data1Disk'
$vm = Add-AzureRmVMDataDisk -VM $vm -Name $dataDisk1Name -Caching None -CreateOption Empty -DiskSizeInGB 127 -VhdUri $dataDisk1VhdUri -Lun 1
$dataDisk2VhdUri = "https://$randSAName.blob.core.windows.net/vhds/"+$VMName+"-Data2.vhd"
$dataDisk2Name = $VMName+'-data2Disk'
$vm = Add-AzureRmVMDataDisk -VM $vm -Name $dataDisk2Name -Caching None -CreateOption Empty -DiskSizeInGB 512 -VhdUri $dataDisk2VhdUri -Lun 2
# Create Virtual Machine
Write-Output "Creating the VM" | timestamp
$NewVM = New-AzureRmVM -ResourceGroupName $resourceGroupName -Location $Location -VM $vm
Write-Output "VM creation complete" | timestamp
###create scripts container
New-AzureStorageContainer -Name $scriptsContainer -Context $storageAcct.Context -Permission Off
#download locally Scripts from GitHub and edit the Storage Account Keys and passwords
New-Item -Path c:\ -Name Temp -ItemType Directory
$file = "c:\temp\gitassets.zip"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
(New-Object System.Net.WebClient).DownloadFile($gitHubAssets, "$file")
# Unzip the file to specified location
$shell_app=new-object -com shell.application
$zip_file = $shell_app.namespace($file)
$destination = $shell_app.namespace("c:\temp")
$destination.Copyhere($zip_file.items())
Copy-Item -Path C:\Temp\AzureSharePoint2016Install-master\* -Destination C:\Temp -confirm:$false -Force -Recurse
Remove-Item C:\Temp\AzureSharePoint2016Install-master -Force -Confirm:$false -Recurse
Remove-Item $file -Force -Confirm:$false
$script=Get-Content C:\temp\BootScripts\FirstBoot.ps1
$script=$script -replace "<your admin pass>",$yourAdminPassword
$script=$script -replace "<your netbios name>",$netbiosname
Set-Content -Value $script -Path C:\temp\BootScripts\FirstBoot.ps1 -Encoding UTF8
$script=Get-Content C:\temp\BootScripts\SecondBoot.ps1
$script=$script -replace "<your admin pass>",$yourAdminPassword
$script=$script -replace "<your netbios name>",$netbiosname
$script=$script -replace "<sharePoint iso source>",$sharepointBinaryUrl
$script=$script -replace "<SQL Binary URL>",$sqlBinaryUrl
$script=$script -replace "<GitHub Assets>",$gitHubAssets
$script=$script -replace "<Setup Account>",$setupAccount
Set-Content -Value $script -Path C:\temp\BootScripts\SecondBoot.ps1 -Encoding UTF8
#Upload these scripts to the blob or file share
$blobName = "FirstBoot.ps1"
$localFile = "C:\Temp\BootScripts\$blobName"
Set-AzureStorageBlobContent -File $localFile -Container $scriptsContainer -Blob $blobName -Context $storageAcct.Context -Force
$blobName = "SecondBoot.ps1"
$localFile = "C:\Temp\BootScripts\$blobName"
Set-AzureStorageBlobContent -File $localFile -Container $scriptsContainer -Blob $blobName -Context $storageAcct.Context -Force
#Now make a DC by running the first boot script
$ScriptBlobURL = "https://$randSAName.blob.core.windows.net/$scriptsContainer/"
$ScriptName = "FirstBoot.ps1"
$ExtensionName = 'FirstBootScript'
$ExtensionType = 'CustomScriptExtension'
$Publisher = 'Microsoft.Compute'
$Version = '1.9'
$timestamp = (Get-Date).Ticks
$ScriptLocation = $ScriptBlobURL + $ScriptName
$ScriptExe = ".\$ScriptName"
$PrivateConfiguration = @{"storageAccountName" = "$randSAName";"storageAccountKey" = "$ScriptBlobKey"}
$PublicConfiguration = @{"fileUris" = [Object[]]"$ScriptLocation";"timestamp" = "$timestamp";"commandToExecute" = "powershell.exe -ExecutionPolicy Unrestricted -Command $ScriptExe"}
Write-Output "Injecting First Boot PowerShell" | timestamp
Set-AzureRmVMExtension -ResourceGroupName $resourceGroupName -VMName $VMName -Location $Location `
-Name $ExtensionName -Publisher $Publisher -ExtensionType $ExtensionType -TypeHandlerVersion $Version `
-Settings $PublicConfiguration -ProtectedSettings $PrivateConfiguration
((Get-AzureRmVM -Name $VMName -ResourceGroupName $resourceGroupName -Status).Extensions | Where-Object {$_.Name -eq $ExtensionName}).Substatuses
Write-Output "Waiting 3 minutes for reboot to complete" | timestamp
Start-Sleep -Seconds 180 #Wait 3 minutes
#Have to remove the previous before creating a new one
Remove-AzureRmVMExtension -ResourceGroupName $resourceGroupName -VMName $VMName -Name FirstBootScript -Force
#Now run the second boot script to install SQL and SharePoint
$ScriptName = "SecondBoot.ps1"
$ExtensionName = 'SecondBootScript'
$timestamp = (Get-Date).Ticks
$ScriptLocation = $ScriptBlobURL + $ScriptName
$ScriptExe = ".\$ScriptName"
$PrivateConfiguration = @{"storageAccountName" = "$randSAName";"storageAccountKey" = "$ScriptBlobKey"}
$PublicConfiguration = @{"fileUris" = [Object[]]"$ScriptLocation";"timestamp" = "$timestamp";"commandToExecute" = "powershell.exe -ExecutionPolicy Unrestricted -Command $ScriptExe"}
Write-Output "Injecting Second Boot PowerShell" | timestamp
Set-AzureRmVMExtension -ResourceGroupName $resourceGroupName -VMName $VMName -Location $Location `
-Name $ExtensionName -Publisher $Publisher -ExtensionType $ExtensionType -TypeHandlerVersion $Version `
-Settings $PublicConfiguration -ProtectedSettings $PrivateConfiguration
((Get-AzureRmVM -Name $VMName -ResourceGroupName $resourceGroupName -Status).Extensions | Where-Object {$_.Name -eq $ExtensionName}).Substatuses
Write-Output "Waiting 2 minutes for reboot to complete" | timestamp
Start-Sleep -Seconds 120 #Wait 2 minutes
Remove-AzureRmVMExtension -ResourceGroupName $resourceGroupName -VMName $VMName -Name SecondBootScript -Force
Write-Output "You can now login to $($pip.DnsSettings.Fqdn) with $netbiosname\$setupAccount and $yourAdminPassword. The installation will be done in 40 minutes" | timestamp
###delete share and blob container