-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnewRRASServer.ps1
148 lines (147 loc) · 10.6 KB
/
newRRASServer.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
function new-RRASServer {
param(
[Parameter(ParameterSetName = 'RRASClass')]
[RRAS]
$RRASConfig,
[Parameter(ParameterSetName = 'NoClass')]
[string]
$VHDXPath,
[Parameter(ParameterSetName = 'NoClass')]
[string]
$RefVHDX,
[Parameter(ParameterSetName = 'NoClass')]
[pscredential]
$localadmin,
[parameter(ParameterSetName = 'NoClass', Mandatory = $false)]
[switch]
$vmSnapshotenabled,
[Parameter(ParameterSetName = 'NoClass')]
[string]
$Name,
[Parameter(ParameterSetName = 'NoClass')]
[int]
$cores,
[Parameter(ParameterSetName = 'NoClass')]
[int]
$RAM,
[Parameter(ParameterSetName = 'NoClass')]
[string]
$IPaddress,
[Parameter(ParameterSetName = 'NoClass')]
[string]
$network
)
if (!$PSBoundParameters.ContainsKey('RRASConfig')) {
$RRASConfig = [RRAS]::new()
$RRASConfig.Name = $name
$RRASConfig.Cores = $cores
$RRASConfig.Ram = $RAM
$RRASConfig.ipaddress = $IPaddress
$RRASConfig.Network = $network
$RRASConfig.localadmin = $localadmin
$RRASConfig.vmSnapshotenabled = $vmSnapshotenabled
$RRASConfig.VHDXpath = $VHDXPath
$RRASConfig.RefVHDX = $RefVHDX
}
Write-LogEntry -Message "RRAS Server started $(Get-Date)" -type Information
Write-LogEntry -Message "RRAS Settings are: $($RRASConfig | ConvertTo-Json)" -Type Information
if ((Invoke-Pester -TagFilter "RRASVM" -PassThru -Output None).result -ne "Passed") {
Write-LogEntry -Type Information -Message "Path for the VHDX for RRAS is: $($RRASConfig.VHDXpath)"
if ((Invoke-Pester -TagFilter "RRASVHDX" -PassThru -Output None).result -eq "Passed") {
Write-LogEntry -Type Error -Message "RRAS VHDX Already Exists at path: $($RRASConfig.VHDXpath) Please clean up and Rerun. Build STOPPED"
throw "RRAS VHDX Already Exists at path: $($RRASConfig.VHDXpath) Please clean up and Rerun."
}
else {
Copy-Item -Path $RRASConfig.RefVHDX -Destination $RRASConfig.VHDXpath
Write-LogEntry -Type Information -Message "Reference VHDX: $($RRASConfig.RefVHDX) has been copied to: $($RRASConfig.VHDXpath)"
}
if ((Invoke-Pester -TagFilter "RRASVHDX" -PassThru -Output None).result -ne "Passed") {
Write-LogEntry -Type Error -Message "Error Creating the VHDX for RRAS. Build STOPPED"
throw "Error Creating the VHDX for RRAS"
}
else {
Write-LogEntry -Type Information -Message "Starting to create RRAS Server"
$vm = new-vm -Name $RRASConfig.name -MemoryStartupBytes ($RRASConfig.RAM * 1Gb) -VHDPath $RRASConfig.VHDXpath -Generation 2 | out-null # | Set-VMMemory -DynamicMemoryEnabled:$false
$vm | Set-VMProcessor -Count $RRASConfig.cores
Enable-VMIntegrationService -VMName $RRASConfig.name -Name "Guest Service Interface"
if (!$RRASConfig.vmSnapshotenabled) {
set-vm -Name $RRASConfig.name -CheckpointType Disabled
}
Write-LogEntry -Type Information -Message "RRAS Server has been created"
if ((Invoke-Pester -TagFilter "RRASVM" -PassThru -Output None).result -ne "Passed")
{
Write-LogEntry -Type Error -message "Error Creating the VHDX for RRAS";
throw "Error Creating the VHDX for RRAS"
}
}
start-vm -Name $RRASConfig.name
Write-LogEntry -Type Information -Message "RRAS Server named $($RRASConfig.Name) has been started"
Get-VMNetworkAdapter -vmname $RRASConfig.name | Connect-VMNetworkAdapter -SwitchName 'Internet' | Set-VMNetworkAdapter -Name 'Internet' -DeviceNaming On
Write-LogEntry -Type Information -Message "vSwitch named Internet has been connected to the RRAS Server"
while ((Invoke-Command -VMName $RRASConfig.name -Credential $RRASConfig.localadmin { "Test" } -ErrorAction SilentlyContinue) -ne "Test") { Start-Sleep -Seconds 5 }
$RRASConfigSession = New-PSSession -VMName $RRASConfig.name -Credential $RRASConfig.localadmin
Write-LogEntry -Type Information -Message "PowerShell Direct session for $($RRASConfig.localadmin.UserName) has been initated with RRAS Server named: $($RRASConfig.name)"
if ((Invoke-Pester -TagFilter "RRASVPN" -PassThru -Output None).result -eq "Passed") {
Write-Verbose "RRAS Routing Already installed"
}
else {
$null = Invoke-Command -Session $RRASConfigSession -ScriptBlock { Install-WindowsFeature Routing -IncludeManagementTools }
Write-LogEntry -Type Information -Message "Routing and Remote Access services role now installed on: $($RRASConfig.name)"
if (((Invoke-Pester -TestName "RRAS" -PassThru -show None).TestResult | Where-Object { $_.name -match "RRAS Routing Installed" }).Result -notmatch "Passed") { Write-LogEntry -Type Error -Message "Error installing RRAS Routing, Build STOPPED"; throw "Error installing RRAS Routing" }
}
while ((Invoke-Command -VMName $RRASConfig.name -Credential $RRASConfig.localadmin { "Test" } -ErrorAction SilentlyContinue) -ne "Test") { Start-Sleep -Seconds 5 }
$RRASConfigSession = New-PSSession -VMName $RRASConfig.name -Credential $RRASConfig.localadmin
Write-LogEntry -Type Information -Message "PowerShell Direct session for $($RRASConfig.localadmin.UserName) has been initated with RRAS Server named: $($RRASConfig.name)"
if ((Invoke-Pester -TagFilter "RRASExtNIC" -PassThru -Output None).result -eq "Passed") {
Write-Verbose "RRAS NIC Already Named external"
}
else {
Invoke-Command -Session $RRASConfigSession -ScriptBlock { Get-NetAdapter -Physical -name Ethernet | rename-netadapter -newname "External" }
Write-LogEntry -Type Information -Message "Renamed Network Adaptor to 'External'"
if (((Invoke-Pester -TestName "RRAS" -PassThru -show None).TestResult | Where-Object { $_.name -match "RRAS External NIC Renamed" }).Result -notmatch "Passed") { write-logentry -Type Error -Message "RRAS NIC not renamed. Build STOPPED"; throw "RRAS NIC not renamed" }
}
Invoke-Command -Session $RRASConfigSession -ScriptBlock { Install-RemoteAccess -VpnType Vpn; netsh routing ip nat install; netsh routing ip nat add interface "External"; netsh routing ip nat set interface "External" mode=full }
Write-LogEntry -Type Information -Message "Routing configured for External Network adapter"
$RRASConfigSession | Remove-PSSession
Write-LogEntry -Type Information -Message "PowerShell Direct Session for $($RRASConfig.name) has been disconnected"
}
else {
Start-VM $RRASConfig.name -WarningAction SilentlyContinue -ErrorAction SilentlyContinue
Write-LogEntry -Type Information -Message "Starting Routing and Remote Access Services server named: $($RRASConfig.Name)"
while ((Invoke-Command -VMName $RRASConfig.name -Credential $RRASConfig.localadmin { "Test" } -ErrorAction SilentlyContinue) -ne "Test") { Start-Sleep -Seconds 5 }
}
if ((Get-VMNetworkAdapter -VMName $RRASConfig.name | Where-Object { $_.switchname -eq $RRASConfig.network }).count -eq 0) {
if ((Invoke-Pester -TagFilter "RRASLabIP" -PassThru -Output None).result -eq "Passed") {
Write-Verbose "RRAS NIC Already Named $($RRASConfig.Network)"
}
else {
$RRASConfigSession = New-PSSession -VMName $RRASConfig.name -Credential $RRASConfig.localadmin
Write-LogEntry -Type Information -Message "PowerShell Direct session for $($RRASConfig.localadmin.UserName) has been initated with RRAS Server named: $($RRASConfig.name)"
$RRASConfignics = Invoke-Command -Session $RRASConfigSession -ScriptBlock { Get-NetAdapter }
Write-LogEntry -Type Information -Message "The following Network Adaptors $($RRASConfignics -join ",") have been found on: $($RRASConfig.name)"
get-vm -Name $RRASConfig.name | Add-VMNetworkAdapter -SwitchName $RRASConfig.Network
Write-LogEntry -Type Information -Message "Network adaptor for switch: $($RRASConfig.Network) has been added to: $($RRASConfig.Name)"
Start-Sleep -Seconds 10
$RRASConfignewnics = Invoke-Command -Session $RRASConfigSession -ScriptBlock { Get-NetAdapter }
Write-LogEntry -Type Information -Message "The following Network Adaptors $($RRASConfignewnics -join ",") have been found on: $($RRASConfig.name)"
$t = Compare-Object -ReferenceObject $RRASConfignics -DifferenceObject $RRASConfignewnics -PassThru
$null = Invoke-Command -Session $RRASConfigSession -ScriptBlock { param($t, $i) new-NetIPAddress -InterfaceIndex $t -AddressFamily IPv4 -IPAddress "$i" -PrefixLength 24 } -ArgumentList $t.InterfaceIndex, $rrasconfig.IPaddress
Write-LogEntry -Type Information -Message "Ip address of $rrasconfig.IPAddress has been set on Network Adaptor $($RRASConfig.Network) for VM $($RRASConfig.Name)"
Invoke-Command -Session $RRASConfigSession -ScriptBlock { param($n, $t)Get-NetAdapter -InterfaceIndex $n | rename-netadapter -newname $t } -ArgumentList $t.InterfaceIndex, $RRASConfig.Network
Invoke-Command -Session $RRASConfigSession -ScriptBlock { param($n)get-service -name "remoteaccess" | Restart-Service -WarningAction SilentlyContinue; netsh routing ip nat add interface $n } -ArgumentList $RRASConfig.Network
Write-LogEntry -Type Information -Message "Network adaptor renamed to: $($RRASConfig.Network) and Routing configured."
if ((Invoke-Pester -TagFilter "RRASLabIP" -PassThru -Output None).result -ne "Passed")
{
Write-LogEntry -Type Error -Message "Lab IP address not added. Build STOPPED";
throw "Lab IP address not added"
}
}
Invoke-Command -Session $RRASConfigSession -ScriptBlock { Set-LocalUser -Name "Administrator" -PasswordNeverExpires 1 }
Write-LogEntry -type Information -message "Local admin account set to not expire"
Invoke-Command -Session $RRASConfigSession -ScriptBlock { Set-ItemProperty -path HKLM:\SOFTWARE\Microsoft\ServerManager -name DoNotOpenServerManagerAtLogon -Type DWord -value "1" -Force }
$RRASConfigSession | Remove-PSSession
Write-LogEntry -Type Information -Message "PowerShell Direct Session for $($RRASConfig.name) has been disconnected"
}
write-logentry -Type Information -Message "RRAS Server Completed: $(Get-Date)"
invoke-pester -TagFilter "RRAS" -Output Detailed
}