forked from dataplat/dbatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanup.ps1
109 lines (105 loc) · 5.2 KB
/
cleanup.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
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')]
param(
$module = "dbatools"
)
# Which process should we be looking for?
if ($psedition -eq 'Core') {
$process = "pwsh"
} else {
$process = "powershell"
}
if (($PSVersionTable.PSVersion.Major -le 5) -or ($PSVersionTable.PSVersion.Major -gt 6 -and $PSVersionTable.OS -contains "Windows")) {
$isElevated = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
$ise = Get-Process powershell_ise -ErrorAction SilentlyContinue
if ($ise) {
return "PowerShell ISE found in use. Please close this program before using this script."
}
} else {
$isElevated = $null;
$ise = $null;
}
$installedVersion = Get-InstalledModule $module -AllVersions | Select-Object Version, InstalledLocation
Write-Output "The currently installed version(s) of $module is/are: "
$installedVersion.Version
$results =
foreach ($v in $installedVersion) {
if ($v.InstalledLocation -match "C:\\Users") {
Add-Member -Force -InputObject $v -MemberType NoteProperty -Name IsUserScope -value $true
} else {
if (-not $isElevated) {
Write-Output "$module version $v.Version cannot be removed without elevated session."
}
Add-Member -Force -InputObject $v -MemberType NoteProperty -Name IsUserScope -value $false
}
$v
}
$newestVersion = Find-Module $module | Select-Object Version
Write-Output "`nThe latest version of $module in the PSGallery is: $($newestVersion.Version)"
if ($installedVersion.Count -gt 1) {
$olderVersions = @($installedVersion | Where-Object { [version]$_.Version -lt [version]$newestVersion.Version })
}
if ( ($olderVersions.Count -gt 0) -and $newestVersion.Version -in $installedVersion.Version ) {
Write-Output "Latest version of $module found on $env:COMPUTERNAME."
Write-Output "Older versions of $module also found. These will be uninstalled now."
if ($isElevated) {
$processes = Get-Process $process -IncludeUserName -ErrorAction SilentlyContinue | Where-Object Id -NE $pid
} else {
$processes = Get-Process $process -ErrorAction SilentlyContinue | Where-Object Id -NE $PID
}
if ($processes.Count -gt 0) {
if ($Pscmdlet.ShouldProcess("$env:COMPUTERNAME", "Killing $($processes.Count) processes of powershell running")) {
Write-Output "Death to the following process(es): $(($processes.Id) -join ",")"
$processes | Stop-Process -ErrorVariable dangit -ErrorAction SilentlyContinue -Force
if ($dangit) {
Write-Warning "Not able to kill following processes: $((Get-Process $process | Where-Object Id -NE $pid).Id -join ",")"
}
}
}
if ($Pscmdlet.ShouldProcess("$env:COMPUTERNAME", "Removing old versions of $module.")) {
foreach ($v in $olderVersions.Version) {
Uninstall-Module $module -RequiredVersion $v -ErrorVariable dangit -ErrorAction SilentlyContinue -Force
if ($dangit) {
if ($dangit.Exception -like "*Administrator rights*") {
Write-Warning "Elevated session is required to uninstall $module version: $v"
} else {
Write-Warning "Unable to remove $module version [$v] due to: `n`t$($dangit.Exception)"
}
}
}
}
Write-Output "The End"
} elseif ( ($olderVersions.Count -gt 0) -and $newestVersion.Version -notin $installedVersion.Version ) {
Write-Output "Update of $module is available"
Write-Output "Older versions of $module found. These will be uninstalled now."
if ($isElevated) {
$processes = Get-Process $process -ErrorAction SilentlyContinue -IncludeUserName | Where-Object Id -NE $pid
} else {
$processes = Get-Process $process -ErrorAction SilentlyContinue | Where-Object Id -NE $PID
}
if ($processes.Count -gt 0) {
if ($Pscmdlet.ShouldProcess("$env:COMPUTERNAME", "Killing $($processes.Count) processes of powershell running")) {
Write-Output "Death to the following process(es): $(($processes.Id) -join ",")"
$processes | Stop-Process -ErrorVariable dangit -ErrorAction SilentlyContinue -Force
if ($dangit) {
Write-Warning "Not able to kill following processes: $((Get-Process $process | Where-Object Id -NE $pid).Id -join ",")"
}
}
}
if ($Pscmdlet.ShouldProcess("$env:COMPUTERNAME", "Removing old versions of $module.")) {
foreach ($v in $olderVersions.Version) {
Uninstall-Module $module -RequiredVersion $v -ErrorVariable dangit -ErrorAction SilentlyContinue -Force
if ($dangit) {
if ($dangit.Exception -like "*Administrator rights*") {
Write-Warning "Elevated session is required to uninstall $module version: $v"
} else {
Write-Warning "Unable to remove $module version [$v] due to: `n`t$($dangit.Exception)"
}
}
}
}
Write-Output "Continuing to install latest release of $module"
Install-Module $module -Force
Write-Output "The End"
} else {
Write-Output "No update/actions required."
}