forked from ViDou83/SDNNested
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNew-SDNNestedAzHost.ps1
More file actions
215 lines (155 loc) · 8.98 KB
/
Copy pathNew-SDNNestedAzHost.ps1
File metadata and controls
215 lines (155 loc) · 8.98 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
Param(
[Parameter(Mandatory = $false, ParameterSetName = "ConfigurationFile")]
[String] $ConfigurationDataFile = ".\configfiles\SDNNestedAzHost.psd1"
)
Import-Module -Name Az.Compute
$Connected = Get-AzSubscription -ErrorAction Continue -OutVariable null
if ( $Connected ) {
"Already connected to the subscription"
}
else {
Connect-AzAccount
}
# Script version, should be matched with the config files
$ScriptVersion = "2.0"
#Validating passed in config files
if ($psCmdlet.ParameterSetName -eq "ConfigurationFile") {
Write-Host "Using configuration file passed in by parameter."
$configdata = [hashtable] (iex (gc $ConfigurationDataFile | out-string))
}
elseif ($psCmdlet.ParameterSetName -eq "ConfigurationData") {
Write-Host "Using configuration data object passed in by parameter."
$configdata = $configurationData
}
if ($Configdata.ScriptVersion -ne $scriptversion) {
Write-Host "Configuration file $ConfigurationDataFile version $($ConfigData.ScriptVersion) is not compatible with this version of SDN express."
Write-Host "Please update your config file to match the version $scriptversion example."
return
}
$ForbiddenChar = @("-", "_", "\", "/", "@", "<", ">", "#")
# Credentials for Local Admin account you created in the sysprepped (generalized) vhd image
$VMLocalAdminUser = $configdata.VMLocalAdminUser
$VMLocalAdminSecurePassword = ConvertTo-SecureString $configdata.VMLocalAdminSecurePassword -AsPlainText -Force
## Azure Account
$VMSize = $configdata.VMSize
$LocationName = $configdata.LocationName
$ResourceGroupName = $configdata.ResourceGroupName
$VnetName = $configdata.VnetName
$SubnetName = $configdata.SubnetName
$VMName = $configdata.VMName
$SecurityGroupName = "$($VMName)_NetSecurityGroup"
$PublicIPAddressName = "$($VMName)_PIP1"
$subscription = $configdata.subscription
$NICName = "$($VMName)_NIC1"
$DNSNameLabel = $VMName
$storageType = $configdata.storageType
$NSGName = $configdata.NSGName
foreach ($c in $ForbiddenChar) { $DNSNameLabel = $DNSNameLabel.ToLower().replace($c, "") }
$Credential = New-Object System.Management.Automation.PSCredential ($VMLocalAdminUser, $VMLocalAdminSecurePassword)
$VNET = Get-AzVirtualNetwork -Name $VnetName -ResourceGroupName $ResourceGroupName
if ( $null -eq $VNET ) {
Write-Host -ForegroundColor Yellow "No VNET found in $ResourceGroupName so going to create one"
$SubnetName = Read-Host "SubnetName(ex:MySubnet)"
$VnetAddressPrefix = Read-Host "Prefix(ex:10.0.0.0/16)"
$SubnetAddressPrefix = Read-Host "Prefix(ex:10.0.0.0/24)"
$SingleSubnet = New-AzVirtualNetworkSubnetConfig -Name $SubnetName `
-AddressPrefix $SubnetAddressPrefix
$VNET = New-AzVirtualNetwork -Name $VnetName -ResourceGroupName $ResourceGroupName
-Location $LocationName -AddressPrefix $VnetAddressPrefix -Subnet $SingleSubnet
}
$NSG = Get-AzNetworkSecurityGroup -ResourceName $NSGName -ResourceGroupName $ResourceGroupName
if ( !( $NSG.SecurityRules | ? destinationPortRange -eq "3389") ) {
$NSG | Add-AzNetworkSecurityRuleConfig -Name "Rdp-Rule" -Description "Allow WinRM" -Access "Allow" -Protocol "Tcp" -Direction "Inbound" `
-Priority 100 -SourceAddressPrefix "Internet" -SourcePortRange "*" -DestinationAddressPrefix "*" -DestinationPortRange "3389" | Set-AzNetworkSecurityGroup
}
if ( !( $NSG.SecurityRules | ? destinationPortRange -eq "5985-5986") ) {
$NSG | Add-AzNetworkSecurityRuleConfig -Name "WinRM-Rule" -Description "Allow WinRM" -Access "Allow" -Protocol "Tcp" -Direction "Inbound" `
-Priority 150 -SourceAddressPrefix "Internet" -SourcePortRange "*" -DestinationAddressPrefix "*" -DestinationPortRange "5985-5986" | Set-AzNetworkSecurityGroup
}
$PIP = New-AzPublicIpAddress -Name $PublicIPAddressName -DomainNameLabel $DNSNameLabel -ResourceGroupName $ResourceGroupName `
-Location $LocationName -AllocationMethod Dynamic -Force
$NIC = New-AzNetworkInterface -Name $NICName -ResourceGroupName $ResourceGroupName `
-Location $VNET.Location -SubnetId $VNET.Subnets[0].Id -PublicIpAddressId $PIP.Id `
-NetworkSecurityGroupId $NSG.Id -Force
$VirtualMachine = New-AzVMConfig -VMName $VMName -VMSize $VMSize
$VirtualMachine = Set-AzVMOperatingSystem -VM $VirtualMachine -Windows -ComputerName $VMName `
-Credential $Credential -ProvisionVMAgent -EnableAutoUpdate
$VirtualMachine = Set-AzVMSourceImage -VM $VirtualMachine -PublisherName 'MicrosoftWindowsServer' `
-Offer 'WindowsServer' -Skus '2019-Datacenter' -Version latest
$VirtualMachine = Add-AzVMNetworkInterface -VM $VirtualMachine -Id $NIC.Id
$VirtualMachine = Set-AzVMOSDisk -StorageAccountType $storageType -VM $VirtualMachine -CreateOption "FromImage"
Write-Host -ForegroundColor Green "Creating the AZ VM $VMName"
New-AzVm -ResourceGroupName $ResourceGroupName -Location $LocationName `
-VM $VirtualMachine -Verbose
Write-Host -ForegroundColor Green "AZ VM $VMName successfully created"
$VirtualMachine = get-AzVm -VMName $VMName
$VirtualMachine | Stop-AzVM -Force
Write-Host -ForegroundColor Green "AZ VM $VMName successfully stopped to add SSD data disk"
$AzDiskConfig = New-AzDiskConfig -Location $LocationName -DiskSizeGB $configdata.DiskSizeGB `
-AccountType $storageType -CreateOption Empty
for ($i = 0; $i -lt $configdata.DiskNumber; $i++) {
$AzDisk = New-AzDisk -ResourceGroupName $ResourceGroupName -Disk $AzDiskConfig `
-DiskName "$($VMName)_DataDisk$i"
$VirtualMachine = Add-AzVMDataDisk -Name "$($VMName)_DataDisk$i" -Caching 'ReadWrite' -Lun $i `
-ManagedDiskId $AzDisk.Id -CreateOption Attach -VM $VirtualMachine
Write-Host -ForegroundColor Green "AZ VM $VMName SSD Disk $i successfully added"
}
Update-AzVM -ResourceGroupName $ResourceGroupName -VM $VirtualMachine
Write-Host -ForegroundColor Green "AZ VM $VMName successfully updated"
$VirtualMachine | Start-AzVM
Sleep 120
New-Item -ItemType File -Path $env:temp\injectedscript.ps1
$Content = "winrm qc /force
netsh advfirewall firewall add rule name= WinRMHTTP dir=in action=allow protocol=TCP localport=5985
netsh advfirewall firewall add rule name= WinRMHTTPS dir=in action=allow protocol=TCP localport=5986
"
Add-Content $env:temp\injectedscript.ps1 $Content
Write-Host -ForegroundColor Yellow "AZ VM $VMName Adding WinRM Firewall rules"
Invoke-AzVMRunCommand -ResourceGroupName $ResourceGroupName -VMName $VMName -ScriptPath $env:temp\injectedscript.ps1 `
-CommandId 'RunPowerShellScript'
Remove-Item $env:temp\injectedscript.ps1
winrm set winrm/config/client '@{TrustedHosts="*"}' | Out-Null
while ((Invoke-Command $PIP.DnsSettings.Fqdn -Credential $Credential { $env:COMPUTERNAME } `
-ea SilentlyContinue) -ne $VMName) { Start-Sleep -Seconds 1 }
Invoke-Command $PIP.DnsSettings.Fqdn -Credential $Credential {
#https://docs.microsoft.com/fr-fr/windows-server/storage/storage-spaces/deploy-standalone-storage-spaces
$configdata = $args[0]
$VDiskResiliency = "Simple"
$disks = Get-physicaldisk | where canpool -eq $true
$StoragePool = Get-StorageSubsystem | New-StoragePool -Friendlyname MyPool -PhysicalDisks $disks
$virtualDisk = new-VirtualDisk -StoragePoolFriendlyName $StoragePool.FriendlyName -FriendlyName "VirtualDisk1" `
-ResiliencySettingName $VDiskResiliency -UseMaximumSize -NumberOfColumns $disks.Count
$DriveLetter = $configdata.vDiskDriveLetter
Get-VirtualDisk -FriendlyName $virtualDisk.FriendlyName | Get-Disk | Initialize-Disk -PassThru | New-Partition -DriveLetter $DriveLetter -UseMaximumSize | Format-Volume
Set-Item WSMan:\localhost\Client\TrustedHosts -Value "*.sdn.lab" -Force
Add-MpPreference -ExclusionExtension "vhd"
Add-MpPreference -ExclusionExtension "vhdx"
if (! (Test-Path "$($DriveLetter):\VMs") ) {
mkdir "$($DriveLetter):\VMs"
mkdir "$($DriveLetter):\VMs\Template"
}
if (! (Test-Path "$($DriveLetter):\VMs\Template") ) {
mkdir "$($DriveLetter):\VMs\Template"
}
$AzFileShare = $configdata.AzFileShare
$AzFQDN = ($AzFileShare).replace("\\", "").split("\")[0]
$AZFileUser = $configdata.AZFileUser
$AZFilePwd = $configdata.AZFilePwd
#cmdkey /add:$AzFQDN /user:$AZFileUser /pass:$AZFilePwd
net use Z: $AzFileShare /user:$AZFileUser $AZFilePwd /persistent:yes
if ( Test-Path "Z:\Template") {
cp Z:\Template\*.vhdx "$($DriveLetter):\VMs\Template"
}
else{
Write-Host -ForegroundColor Yellow "Cannot get VHDX Template from $AZFileShare. You need to place it manually to $($DriveLetter):\VMs\Template"
}
if ( Test-Path "Z:\apps") {
cp Z:\apps C:\ -Recurse
}
Install-WindowsFeature -Name Hyper-V -IncludeManagementTools -Restart
#Set-VMHost -EnableEnhancedSessionMode $true
} -ArgumentList $configdata
Write-Host -ForegroundColor Green "AZ VM $VMName is running and can be RDP on $($PIP.DnsSettings.Fqdn)"
Write-Host "mstsc /v:$($PIP.DnsSettings.Fqdn)"
Write-Host -ForegroundColor Yellow `
"You are ready to deploy SDN Stack. Before running the SDNNEsted.ps1 script please ensure to have VHD template uploaded to the AzureVM"