forked from dataplat/dbatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.ps1
187 lines (162 loc) · 6.22 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
[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) {
try {
$localpath = (Get-ChildItem "$modpath\dbatools" -ErrorAction Stop).FullName
} catch {
$localpath = $null
}
}
if ($null -eq $localpath) {
$localpath = "$HOME\Documents\WindowsPowerShell\Modules\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, *") {
if ($lib.Location -like "$Path\*") {
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()).TrimEnd("\")
$zipfile = "$temp\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
Unblock-File $zipfile -ErrorAction SilentlyContinue
Write-LocalMessage -Message "Unzipping"
# Keep it backwards compatible
Remove-Item -ErrorAction SilentlyContinue "$temp\dbatools-$branch" -Recurse -Force
Remove-Item -ErrorAction SilentlyContinue "$temp\dbatools-old" -Recurse -Force
$null = New-Item "$temp\dbatools-old" -ItemType Directory
$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 "$Path\*" -Destination "$temp\dbatools-old" -ErrorAction Stop
try {
Write-LocalMessage -Message "2) Cleaning up installation directory"
Remove-Item "$Path\*" -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 "$temp\dbatools-old\*" -Destination $path -ErrorAction Ignore -Recurse
Remove-Item "$temp\dbatools-old" -Recurse -Force
return
}
Write-LocalMessage -Message "3) Setting up current version"
Move-Item -Path "$temp\dbatools-$branch\*" -Destination $path -ErrorAction SilentlyContinue -Force
Remove-Item -Path "$temp\dbatools-$branch" -Recurse -Force
Remove-Item "$temp\dbatools-old" -Recurse -Force
Remove-Item -Path $zipfile -Recurse -Force
Write-LocalMessage -Message "Done! Please report any bugs to dbatools.io/issues or [email protected]."
if (Get-Module dbatools) {
Write-LocalMessage -Message @"
Please restart PowerShell before working with dbatools.
"@
} else {
Import-Module "$path\dbatools.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."