forked from dataplat/dbatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.ps1
211 lines (184 loc) · 7.24 KB
/
install.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
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
[CmdletBinding()]
param (
[string]$Path,
[switch]$Beta
)
function Write-LocalMessage {
[CmdletBinding()]
param (
[string]$Message
)
if (Test-Path function:Write-Message) { Write-Message -Level Output -Message $Message }
else { Write-Host $Message }
}
try {
if (Get-InstalledModule dbatools -Erroraction Stop) {
Update-Module dbatools -Erroraction Stop
Write-LocalMessage -Message "Updated using the PowerShell Gallery"
return
}
} catch {
Write-LocalMessage -Message "dbatools was not installed by the PowerShell Gallery, continuing with web install."
}
$currentVersionTls = [Net.ServicePointManager]::SecurityProtocol
$currentSupportableTls = [Math]::Max($currentVersionTls.value__, [Net.SecurityProtocolType]::Tls.value__)
$availableTls = [enum]::GetValues('Net.SecurityProtocolType') | Where-Object {
$_ -gt $currentSupportableTls
}
$availableTls | ForEach-Object {
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor $_
}
$dbatools_copydllmode = $true
foreach ($modpath in $($env:PSModulePath -split [IO.Path]::PathSeparator)) {
#Grab the user's default home directory module path for later
if ($modpath -like "*$([Environment]::UserName)*") {
$userpath = $modpath
}
try {
$temppath = Join-Path -Path $modpath -ChildPath "dbatools"
$localpath = (Get-ChildItem $temppath -ErrorAction Stop).FullName
} catch {
$localpath = $null
}
}
if ($null -eq $localpath) {
# In case dbatools is not currently installed in any PSModulePath put it in the $userpath
if (Test-Path -Path $userpath) {
$localpath = Join-Path -Path $userpath -ChildPath "dbatools"
}
} else {
Write-LocalMessage -Message "Updating current install"
}
try {
if (-not $path) {
if ($PSCommandPath.Length -gt 0) {
$path = Split-Path $PSCommandPath
if ($path -match "github") {
Write-LocalMessage -Message "Looks like this installer is run from your GitHub Repo, defaulting to psmodulepath"
$path = $localpath
}
} else {
$path = $localpath
}
}
} catch {
$path = $localpath
}
if (-not $path -or (Test-Path -Path "$path\.git")) {
$path = $localpath
}
If ($lib = [appdomain]::CurrentDomain.GetAssemblies() | Where-Object FullName -like "dbatools, *") {
$wildcardpath = Join-Path -Path $Path -ChildPath *
if ($lib.Location -like "$wildcardpath") {
Write-LocalMessage @"
We have detected dbatools to be already imported from
$path
In a manner that prevents us from updating it, since dll files have been locked.
In order to ensure a valid update, please:
- Close all consoles that have dbatools imported (Remove-Module dbatools is NOT enough)
- Start a new PowerShell console
- Run '`$dbatools_copydllmode = `$true' (without the single-quotes)
- Import dbatools and run Update-Dbatools
If done in this order, the binaries will be copied to another location before import, allowing for a save update.
"@
return
}
}
Write-LocalMessage -Message "Installing module to $path"
if (!(Test-Path -Path $path)) {
try {
Write-LocalMessage -Message "Creating directory: $path"
New-Item -Path $path -ItemType Directory | Out-Null
} catch {
throw "Can't create $Path. You may need to Run as Administrator: $_"
}
}
if ($beta) {
$url = 'https://dbatools.io/devzip'
$branch = "development"
} else {
$url = 'https://dbatools.io/zip'
$branch = "master"
}
$temp = ([System.IO.Path]::GetTempPath())
$zipfile = Join-Path -Path $temp -ChildPath "dbatools.zip"
Write-LocalMessage -Message "Downloading archive from github"
try {
(New-Object System.Net.WebClient).DownloadFile($url, $zipfile)
} catch {
try {
#try with default proxy and usersettings
Write-LocalMessage -Message "Probably using a proxy for internet access, trying default proxy settings"
$wc = (New-Object System.Net.WebClient)
$wc.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$wc.DownloadFile($url, $zipfile)
} catch {
Write-Warning "Error downloading file :( $_"
return
}
}
# Unblock if there's a block
if (($PSVersionTable.PSVersion.Major -lt 6) -or ($PSVersionTable.Platform -and $PSVersionTable.Platform -eq 'Win32NT')) {
Write-LocalMessage -Message "Unblocking"
Unblock-File $zipfile -ErrorAction SilentlyContinue
}
Write-LocalMessage -Message "Unzipping"
$branchpath = Join-Path -Path $temp -ChildPath "dbatools-$branch"
$oldpath = Join-Path -Path $temp -ChildPath "dbatools-old"
$wildcardoldpath = Join-Path -Path $oldpath -ChildPath *
$wildcardbranchpath = Join-Path -Path $branchpath -ChildPath *
Remove-Item -ErrorAction SilentlyContinue $branchpath -Recurse -Force
Remove-Item -ErrorAction SilentlyContinue $oldpath -Recurse -Force
$null = New-Item $oldpath -ItemType Directory
if (($PSVersionTable.Keys -contains "Platform") -and $psversiontable.Platform -ne "Win32NT") {
$destinationFolder = $temp
Expand-Archive -Path $zipfile -DestinationPath $destinationFolder -Force
} else {
# Keep it backwards compatible
$shell = New-Object -ComObject Shell.Application
$zipPackage = $shell.NameSpace($zipfile)
$destinationFolder = $shell.NameSpace($temp)
$destinationFolder.CopyHere($zipPackage.Items())
}
Write-LocalMessage -Message "Applying Update"
Write-LocalMessage -Message "1) Backing up previous installation"
Copy-Item -Path $wildcardpath -Destination $oldpath -ErrorAction Stop
try {
Write-LocalMessage -Message "2) Cleaning up installation directory"
Remove-Item $wildcardpath -Recurse -Force -ErrorAction Stop
} catch {
Write-LocalMessage -Message @"
Failed to clean up installation directory, rolling back update.
This usually has one of two causes:
- Insufficient privileges (need to run as admin)
- A file is locked - generally a dll file from having the module imported in some process.
You can run the following line before importing dbatools to prevent file locking:
`$dbatools_copydllmode = `$true
But it increases the time needed to import the module, so we only recommend using it for updates.
Exception:
$_
"@
Copy-Item -Path $wildcardoldpath -Destination $path -ErrorAction Ignore -Recurse
Remove-Item $oldpath -Recurse -Force
return
}
Write-LocalMessage -Message "3) Setting up current version"
Move-Item -Path $wildcardbranchpath -Destination $path -ErrorAction SilentlyContinue -Force
Remove-Item -Path $branchpath -Recurse -Force
Remove-Item $oldpath -Recurse -Force
Remove-Item -Path $zipfile -Recurse -Force
Write-LocalMessage -Message "Done! Please report any bugs to dbatools.io/issues"
if (Get-Module dbatools) {
Write-LocalMessage -Message @"
Please restart PowerShell before working with dbatools.
"@
} else {
$psd1 = Join-Path -Path $path -ChildPath "dbatools.psd1"
Import-Module $psd1 -Force
Write-LocalMessage @"
dbatools v $((Get-Module dbatools).Version)
# Commands available: $((Get-Command -Module dbatools -CommandType Function | Measure-Object).Count)
"@
}
[Net.ServicePointManager]::SecurityProtocol = $currentVersionTls
Write-LocalMessage -Message "`n`nIf you experience any function missing errors after update, please restart PowerShell or reload your profile."