-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeploySAW.ps1
More file actions
executable file
·142 lines (135 loc) · 5.07 KB
/
DeploySAW.ps1
File metadata and controls
executable file
·142 lines (135 loc) · 5.07 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
# DeploySAW.ps1
# Stop on errors
[CmdletBinding()]
Param(
[switch]$Destroy,
[switch]$NoDeploy,
[switch]$SkipNetwork,
[switch]$SkipVDIEnv,
[switch]$SkipRegKey,
[ValidateScript({ if ($_) { Test-Path $_ } })]
[string]$ConfigFile = "$(( get-item $PSScriptRoot ).FullName)\config\SAWDeployerConfigItems.ps1"
)
$ErrorActionPreference = 'Stop'
$timestamp = Get-Date -Format o | ForEach-Object { $_ -replace ":", "." }
$script_name = $MyInvocation.MyCommand.Name
$script_path = $PSScriptRoot
$example_config_file = "$(( get-item $PSScriptRoot ).FullName)\config\EXAMPLE_SAWDeployerConfigItems.ps1"
Start-Transcript -Append "$script_path\logs\$script_name-$timestamp.log"
try {
Write-Debug -Message "Script name: $script_name"
Write-Host "### Start at: $(Get-Date) ###"
# Function to copy a file removing prefix in filname and replacing with a new prefix
function Copy-FileWithNewPrefix {
param (
[Parameter(Mandatory)]
[string]$SourceFile,
[Parameter(Mandatory)]
[string]$DestinationFile,
[Parameter(Mandatory)]
[string]$OldPrefix,
[Parameter(Mandatory)]
[string]$NewPrefix
)
$NewFileName = $DestinationFile -replace $OldPrefix, $NewPrefix
Copy-Item -Path $SourceFile -Destination $NewFileName
}
# Check for ConfigFile.ps1
if (!(Test-Path $ConfigFile) -and (Test-Path $example_config_file)) {
write-host "Only the EXAMPLE_SAWDeployerConfigItems.ps1 file found in ./config, shall I make a copy of this as ./config/SAWDeployerConfigItems.ps1 for use?:" -Confirm
if ($confirmation -eq 'y') {
Copy-FileWithNewPrefix .\config\EXAMPLE_SAWDeployerConfigItems.ps1 .\config\SAWDeployerConfigItems.ps1 'EXAMPLE_' ''
}
else {
Write-Error -Message "No appropriate config file in ./config dir. Exiting. Check source repo for further details: https://github.com/zoak-solutions/AzureVirtualSAW" -ErrorAction Stop
}
}
else {
. $ConfigFile
}
function Install-DependentModule {
param (
[Parameter(Mandatory = $true)][string]$ModuleName
)
if (!(Get-Module -ListAvailable -Name $ModuleName)) {
Write-Host "Installing module: $ModuleName"
Install-Module -Name $ModuleName -Repository PSGallery -Force -Scope CurrentUser
}
else {
Write-Host "Module: $ModuleName installed already."
}
}
function Login-Azure() {
# Bug on local machine here, https://learn.microsoft.com/en-us/answers/questions/1299863/how-to-fix-method-get-serializationsettings-does-n
$context = Get-AzContext
if (!$context) {
Connect-AzAccount
}
else {
Write-Host " Already connected"
}
}
function Destroy-SAWEnvironment {
param (
[Parameter(Mandatory)]
[string]$RGToDestroy
)
Write-Host "Destroying resource group: $RGToDestroy"
if (!(Get-AzResourceGroup -Name $RGToDestroy -ErrorAction SilentlyContinue)) {
Write-Error -Message "Resource group: $RGToDestroy not found. Exiting." -ErrorAction Stop
}
else {
Write-Host "Destroying Resource group: $RGToDestroy (this can take some time...)."
Remove-AzResourceGroup -Name $RGToDestroy -Force
Write-Host "Resource group: $RGToDestroy destroyed."
}
}
# MAIN
Write-Host "Checking for required modules..."
$ModuleDependencies = @('Az', 'Microsoft.Graph.Beta')
Write-Host("Current Execution Policy: ")
Get-ExecutionPolicy -Scope CurrentUser
foreach ($Module in $ModuleDependencies) {
Install-DependentModule -ModuleName $Module
}
# Login to Azure
Write-Host "Logging into Azure if not already logged in..."
Login-Azure
# Run with switches controlling what to do
if ($Destroy) {
Destroy-SAWEnvironment -RGToDestroy $SAWResourceGroupName
} if ($NoDeploy) {
Write-Host "###############################################"
Write-Host "-NoDeploy switch present, skipping deployment and exiting."
Write-Host "###############################################"
Exit
} if ($SkipVDIEnv) {
Write-Host "Skipping VDIEnv deploy"
}
else {
Write-Host "Deploying SAW VDI environment..."
.\scripts\CreateAzureVDIEnv.ps1
Write-Host "SAW environment deployed."
} if ($SkipNetwork) {
Write-Host "Skipping network deploy"
}
else {
Write-Host "Deploying SAW network..."
.\scripts\CreateSAWNets.ps1
Write-Host "SAW network deployed."
} if ($SkipRegKey) {
Write-Host "Skipping Reg Key create"
}
else {
Write-Host "Creating session host Registration Key.."
.\scripts\CreateRegKey.ps1
Write-Host "SAW Registration Key Created"
}
Write-Host "### DONE at: $(Get-Date) ###"
} # End try
catch {
Write-Error "Error in script: $_" -ErrorAction Stop
}
finally {
Stop-Transcript
}