-
Notifications
You must be signed in to change notification settings - Fork 0
/
Start-DeployVMs.ps1
166 lines (148 loc) · 6.19 KB
/
Start-DeployVMs.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
#$OutputEncoding = [Console]::OutputEncoding = [Text.UTF8Encoding]::UTF8
param(
$HOSTFILE,
$VMFILE
)
# All exported functions
foreach ($function in (Get-ChildItem "$PSScriptRoot\functions\*.ps1")) {
$ExecutionContext.InvokeCommand.InvokeScript($false, ([scriptblock]::Create([io.file]::ReadAllText($function))), $null, $null)
}
$ConfigFiles = dir "Hosts\*-HOSTS.psd1"
$VMsConfigContent = dir "Roles\DeployVMs\Config\*-Vms.json"
if ($HOSTFILE -and $VMFILE)
{
if ((Test-Path $HOSTFILE) -and (Test-Path $VMFILE))
{
$SelectForm = New-Object PSObject -Property @{
HostsConfig = $HOSTFILE.Split("\")[-1]
VMsConfig = $VMFILE.Split("\")[-1]
}
}
else
{
Write-Host ""
Write-Host -ForegroundColor Red "The file you specified does not exist"
Write-Host -ForegroundColor Red " Exiting..."
exit
}
}
else
{
#Create list of Config Files Hosts
[array]$DropDownHostsArray = $null
foreach ($CFs in $ConfigFiles)
{
[array]$DropDownHostsArray += $CFs.Name
}
#Create list of Config Files VMs
[array]$DropDownVmsArray = $null
foreach ($Vm in $VMsConfigContent) {
[array]$DropDownVmsArray += $Vm.Name
}
#Menu Function
function Return-DropDown
{
$Choice = $DropDownHosts.SelectedItem.ToString()
$Form.Close()
}
[array]$DropDownHostsArray += "EXIT"
#Generate GUI input box for Config File Selection
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null
$Form = New-Object System.Windows.Forms.Form
$Form.width = 700
$Form.height = 150
$Form.StartPosition = "CenterScreen"
$Form.Text = "Hosts File to use"
$DropDownHosts = new-object System.Windows.Forms.ComboBox
$DropDownHosts.Location = new-object System.Drawing.Size(100,10)
$DropDownHosts.Size = new-object System.Drawing.Size(550,30)
ForEach ($Item in $DropDownHostsArray)
{
$DropDownHosts.Items.Add($Item) | Out-Null
}
$Form.Controls.Add($DropDownHosts)
$DropDownHostsLabel = new-object System.Windows.Forms.Label
$DropDownHostsLabel.Location = new-object System.Drawing.Size(1,10)
$DropDownHostsLabel.size = new-object System.Drawing.Size(255,20)
$DropDownHostsLabel.Text = "Hosts File"
$Form.Controls.Add($DropDownHostsLabel)
$DropDownVMs = new-object System.Windows.Forms.ComboBox
$DropDownVMs.Location = new-object System.Drawing.Size(100, 40)
$DropDownVMs.Size = new-object System.Drawing.Size(550, 30)
ForEach ($Item in $DropDownVmsArray) {
$DropDownVMs.Items.Add($Item) | Out-Null
}
$Form.Controls.Add($DropDownVMs)
$DropDownVmsLabel = new-object System.Windows.Forms.Label
$DropDownVmsLabel.Location = new-object System.Drawing.Size(1, 40)
$DropDownVmsLabel.size = new-object System.Drawing.Size(255, 20)
$DropDownVmsLabel.Text = "Vms File"
$Form.Controls.Add($DropDownVmsLabel)
$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(300,70)
$Button.Size = new-object System.Drawing.Size(75,25)
$Button.Text = "Select"
$Button.Add_Click({Return-DropDown})
$form.Controls.Add($Button)
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog() | Out-Null
#Check for valid entry
if (($DropDownHosts.SelectedItem -eq $null) -and ($DropDownVMs.SelectedItem -eq $null))
{
Write-Host -ForegroundColor Red "Nothing Selected"
exit
}
#Check to see if EXIT selected
if ($DropDownHosts.SelectedItem -eq "EXIT")
{
Write-Host -ForegroundColor DarkBlue "You have chosen to EXIT the script"
exit
}
#Set the data configuration file
#$ConfigFile = $DropDownHosts.SelectedItem
#$ConfigFileDir = $CFs.Directory
$SelectForm = New-Object PSObject -Property @{
HostsConfig = $DropDownHosts.SelectedItem
VMsConfig = $DropDownVMs.SelectedItem
}
}
#Write-Host $SelectForm.HostsConfig
# Deploy VMs
.\Roles\DeployVMs\DSC\DSCDeployVMs.ps1 -ConfPath .\Hosts\$($SelectForm.HostsConfig) -RoleConfig .\Roles\DeployVMs\Config\$($SelectForm.VMsConfig)
Start-DscConfiguration -Path ".\Roles\DeployVMs\DSC\temp\" -Verbose -Wait -Force
Get-ChildItem ".\Roles\DeployVMs\DSC\temp\" -Include *.mof -Recurse | Remove-Item
#Provision Vms
$VMsConfigContent = (Get-Content .\Roles\DeployVMs\Config\$($SelectForm.VMsConfig) | ConvertFrom-Json)
$VHDImageCredentials = Import-CliXml -Path .\Credentials\VHDImageCredentials.xml
$DomainCredentials = Import-Clixml -Path .\Credentials\DomainCredentials.xml
foreach ($vm in $VMsConfigContent.vms) {
$ComputerName = $vm.ComputerName
$vmname = $vm.vmname
$domain = $vm.domain
$IpAddress = $vm.VMNetworkAdapters[0].IpAddress
Set-TrustedHosts $IpAddress.ToString()
Write-Host "Added $IpAddress, $ComputerName to TrustedHosts"
Write-Host "Waiting for WinRM..."
while ((Invoke-Command -ComputerName $IpAddress -Credential $VHDImageCredentials {"Test"} -ErrorAction SilentlyContinue) -ne "Test") {Start-Sleep -Seconds 1}
Write-Host "Enable ping on all profiles"
Invoke-Command -ComputerName $IpAddress -Credential $VHDImageCredentials -ScriptBlock {
Set-NetFirewallRule -Name FPS-ICMP4-ERQ-In -Enabled True -Profile Any -RemoteAddress Any
Set-NetFirewallRule -Name FPS-ICMP4-ERQ-Out -Enabled True -Profile Any -RemoteAddress Any
}
if($domain){
Write-Host "Join Domain"
.\Roles\DeployVMs\DSC\DSCDomainJoin.ps1 -NodeName $IpAddress -Credential $DomainCredentials -DomainName $domain -ComputerName $ComputerName
Start-DscConfiguration -Path ".\Roles\DeployVMs\DSC\temp\" -Verbose -Wait -Force -Credential $VHDImageCredentials
Get-ChildItem ".\Roles\DeployVMs\DSC\temp\" -Include *.mof -Recurse | Remove-Item
}
if ($vm.VMScsiControllers){
Write-Host "Formatting data drive and making ready for first use"
Invoke-Command -ComputerName $IpAddress -Credential $VHDImageCredentials -ScriptBlock {
Get-Disk |
Where-Object PartitionStyle -eq 'RAW' |
Initialize-Disk -PartitionStyle GPT -PassThru |
New-Partition -AssignDriveLetter -UseMaximumSize |
Format-Volume -FileSystem NTFS -NewFileSystemLabel "Data" -confirm:$false }
}
}