-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstall-TenantSiteMetadataSyncModule.ps1
143 lines (116 loc) · 4.79 KB
/
Install-TenantSiteMetadataSyncModule.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
<#
.SYNOPSIS
Installs the TenantSiteMetadataSync Module from github
.DESCRIPTION
This script installs the PSModuleDevelopment Module from github.
It does so by ...
- downloading the specified branch as zip to $env:TEMP
- Unpacking that zip file to a folder in $env:TEMP
- Moving that content to a module folder in either program files (default) or the user profile
.PARAMETER Branch
The branch to install. Installs master by default.
Unknown branches will terminate the script in error.
.PARAMETER Scope
By default, the downloaded module will be moved to program files.
Setting this to 'CurrentUser' installs to the userprofile of the current user.
.PARAMETER Force
The install script will overwrite an existing module.
#>
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$false)]
[string]$Branch = "main",
[Parameter(Mandatory=$false)]
[ValidateSet('AllUsers', 'CurrentUser')]
[string]$Scope = "AllUsers",
[Parameter(Mandatory=$false)]
[switch]$Force
)
begin
{
# force TLS 1.2 to communicate with Github
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
# Name of the module that is being cloned
$moduleName = "TenantSiteMetadataSync"
# Name of the organization that is being cloned
$organizationName = "joerodgers"
# Base path to the github repository
$baseUrl = "https://github.com/$organizationName/$moduleName"
$fileName = "$Branch.zip"
$downloadDirectoryPath = Join-Path -Path $env:TEMP -ChildPath $moduleName
$downloadFilePath = Join-Path -Path $downloadDirectoryPath -ChildPath "$moduleName-$fileName"
$moduleInstallationRelativePath = "WindowsPowerShell\Modules\$moduleName"
Remove-Module -Name $moduleName -Force -ErrorAction Ignore
}
process
{
try
{
# remove any previous downloads
Remove-Item -Path $downloadFilePath -Force -Recurse -ErrorAction Ignore
Remove-Item -Path $downloadDirectoryPath -Force -Recurse -ErrorAction Ignore
# create download directory
$null = New-Item -Path $downloadDirectoryPath -ItemType Directory -Force -ErrorAction Stop
# download files
Write-Verbose "$(Get-Date) - Downloading $fileName to '$downloadFilePath'"
Invoke-WebRequest -Uri "$($BaseUrl)/archive/$fileName" -MaximumRedirection 100 -UseBasicParsing -OutFile $downloadFilePath -ErrorAction Stop
# extract zip
Write-Verbose "$(Get-Date) - Extracting '$downloadFilePath' to '$downloadDirectory'"
Expand-Archive -Path $downloadFilePath -DestinationPath $downloadDirectoryPath -ErrorAction Stop
# locate the module manifest psd1 file
Write-Verbose "$(Get-Date) - Searching for PowerShell manifest in $downloadDirectoryPath"
$moduleManifestPath = Get-ChildItem -Path $downloadDirectoryPath -Recurse -Filter "*.psd1" | Select-Object -First 1 -ExpandProperty FullName
# parse module manifest
Write-Verbose "$(Get-Date) - Parsing PowerShell manifest at $moduleManifestPath"
$moduleManifest = Import-PowerShellDataFile -Path $moduleManifestPath
$moduleRootFolder = Split-Path -Path $moduleManifestPath
# determine target installation root path
switch($Scope)
{
"AllUsers"
{
$installationRootPath = $env:ProgramFiles
}
"CurrentUser"
{
$installationRootPath = Split-Path -Path $profile.CurrentUserAllHosts
}
}
# build the full path to the module installation directory
$installationPath = Join-Path -Path $installationRootPath -ChildPath $moduleInstallationRelativePath
# append the downloaded module version to the installation Path
$installationPath = Join-Path -Path $installationPath -ChildPath $moduleManifest.ModuleVersion
# stop if the this version is already installed for this scope, unless -Force was supplied
if ( Test-Path -Path $installationPath -PathType Container )
{
if( -not $Force.IsPresent )
{
Write-Error -Message "A module with the name '$moduleName' and version $($moduleManifest.ModuleVersion) already exists. Use the -Force option to overwrite."
return
}
else
{
Write-Warning "$(Get-Date) - Removing folder '$installationPath'"
Remove-Item -Path $installationPath -Force -Recurse
}
}
# create installation folder
Write-Verbose "$(Get-Date) - Creating installation folder at '$installationPath'"
$null = New-Item -Path $installationPath -ItemType Directory -Force -ErrorAction Stop
# copy module files to folder
foreach ($file in (Get-ChildItem -Path $moduleRootFolder))
{
Write-Verbose "$(Get-Date) - Copying '$($file.FullName)'' to '$installationPath'"
Copy-Item -Path $file.FullName -Destination $installationPath -Force -Recurse -ErrorAction Stop
}
}
finally
{
# remove the downloaded folder
Remove-Item -Path $downloadDirectoryPath -Recurse -Force -ErrorAction Ignore
}
}
end
{
}