-
-
Notifications
You must be signed in to change notification settings - Fork 274
/
Manage-Software.psm1
187 lines (159 loc) · 8.13 KB
/
Manage-Software.psm1
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
Import-Module -DisableNameChecking "$PSScriptRoot\Manage-Chocolatey.psm1"
Import-Module -DisableNameChecking "$PSScriptRoot\Manage-Winget.psm1"
Import-Module -DisableNameChecking "$PSScriptRoot\..\Open-File.psm1"
Import-Module -DisableNameChecking "$PSScriptRoot\..\Title-Templates.psm1"
Import-Module -DisableNameChecking "$PSScriptRoot\..\ui\Show-MessageDialog.psm1"
function Install-Software() {
[CmdletBinding()]
[OutputType([ScriptBlock])]
param (
[Parameter(Position = 0, Mandatory)]
[String] $Name,
[Parameter(Position = 1, Mandatory)]
[String[]] $Packages,
[Parameter(Position = 2)]
[ValidateSet('Winget', 'MsStore', 'Chocolatey', 'WSL')]
[String] $PackageProvider = 'Winget',
[Parameter(Position = 3)]
[ScriptBlock] $InstallBlock = { winget install --exact $Package --accept-source-agreements --accept-package-agreements --silent },
[Switch] $NoDialog
)
[ScriptBlock] $CheckVersionCommand
[String] $Script:DoneTitle = "Information"
[String] $Script:DoneMessage = "$Name installed successfully!"
If ($PackageProvider -in @('Winget', 'MsStore')) {
$CheckVersionCommand = { winget --version }
}
If ($PackageProvider -eq 'Chocolatey') {
$CheckVersionCommand = { choco --version }
$InstallBlock = { choco install --ignore-dependencies --yes $Package }
Write-Status -Types "?" -Status "Chocolatey is configured to ignore dependencies (bloat), you may need to install it before using any program." -Warning
}
If ($PackageProvider -eq 'WSL') {
$CheckVersionCommand = { wsl --version }
$InstallBlock = { wsl --install --distribution $Package }
}
Try {
$Err = (Invoke-Expression "$CheckVersionCommand")
If (($LASTEXITCODE)) { throw $Err } # 0 = False, 1 = True
Write-Status -Types "?", $PackageProvider -Status "$PackageProvider is already installed." -Warning
} Catch {
If ($PackageProvider -in @('Winget', 'MsStore')) {
Install-Winget -Force
}
If ($PackageProvider -in @('Chocolatey')) {
Install-Chocolatey -Force
}
If ($PackageProvider -in @('WSL')) {
Open-PowerShellFilesCollection -RelativeLocation "src\scripts\other-scripts" -Scripts @("Install-WSL.ps1") -NoDialog
}
}
Write-Title "Installing $($Name) via $PackageProvider"
$DoneMessage += "`n`nInstalled via $PackageProvider`:`n"
ForEach ($Package in $Packages) {
If ($PackageProvider -eq 'MsStore') {
Write-Status -Types "?" -Status "Reminder: Press 'Y' and ENTER to continue if stuck (1st time only) ..." -Warning
$PackageName = (winget search --source 'msstore' --exact $Package)[-1].Replace("$Package Unknown", '').Trim(' ')
$Private:Counter = Write-TitleCounter "$Package - $PackageName" -Counter $Counter -MaxLength $Packages.Length
} Else {
$Private:Counter = Write-TitleCounter "$Package" -Counter $Counter -MaxLength $Packages.Length
}
Try {
Invoke-Expression "$InstallBlock" | Out-Host
If (($LASTEXITCODE)) { throw "Couldn't install package." } # 0 = False, 1 = True
If ($PackageProvider -eq 'MsStore') {
$DoneMessage += "+ $PackageName ($Package)`n"
} Else {
$DoneMessage += "+ $Package`n"
}
} Catch {
Write-Status -Types "!" -Status "Failed to install package via $PackageProvider" -Warning
If ($PackageProvider -eq 'MsStore') {
Start-Process "ms-windows-store://pdp/?ProductId=$Package"
$DoneMessage += "! $PackageName ($Package)`n"
} Else {
$DoneMessage += "! $Package`n"
}
}
}
Write-Host "$DoneMessage" -ForegroundColor Cyan
If (!($NoDialog)) {
Show-MessageDialog -Title "$DoneTitle" -Message "$DoneMessage"
}
return $DoneMessage
}
function Uninstall-Software() {
[CmdletBinding()]
[OutputType([String])]
param (
[Parameter(Position = 0, Mandatory)]
[String] $Name,
[Parameter(Position = 1, Mandatory)]
[String[]] $Packages,
[Parameter(Position = 2)]
[ValidateSet('Winget', 'MsStore', 'Chocolatey', 'WSL')]
[String] $PackageProvider = 'Winget',
[Parameter(Position = 3)]
[ScriptBlock] $UninstallBlock = { winget uninstall --exact $Package --accept-source-agreements --purge --silent },
[Switch] $NoDialog
)
$DoneTitle = "Information"
$DoneMessage = "$Name uninstalled successfully!"
If ($PackageProvider -eq 'Chocolatey') {
$UninstallBlock = { choco uninstall --remove-dependencies --yes $Package }
Write-Status -Types "?" -Status "Chocolatey is configured to remove dependencies (bloat), you may need to install it before using any program." -Warning
}
If ($PackageProvider -eq 'WSL') {
$UninstallBlock = { wsl --unregister $Package }
}
Write-Title "Uninstalling $($Name) via $PackageProvider"
$DoneMessage += "`n`nUninstalled via $PackageProvider`:`n"
ForEach ($Package in $Packages) {
If ($PackageProvider -eq 'MsStore') {
$PackageName = (winget search --source 'msstore' --exact $Package)[-1].Replace("$Package Unknown", '').Trim(' ')
$Private:Counter = Write-TitleCounter "$Package - $PackageName" -Counter $Counter -MaxLength $Packages.Length
} Else {
$Private:Counter = Write-TitleCounter "$Package" -Counter $Counter -MaxLength $Packages.Length
}
Try {
Invoke-Expression "$UninstallBlock" | Out-Host
If (($LASTEXITCODE)) { throw "Couldn't uninstall package." } # 0 = False, 1 = True
If ($PackageProvider -eq 'MsStore') {
$DoneMessage += "- $PackageName ($Package)`n"
} Else {
$DoneMessage += "- $Package`n"
}
} Catch {
Write-Status -Types "!" -Status "Failed to uninstall package via $PackageProvider" -Warning
If ($PackageProvider -eq 'MsStore') {
$DoneMessage += "! $PackageName ($Package)`n"
} Else {
$DoneMessage += "! $Package`n"
}
}
}
Write-Host "$DoneMessage" -ForegroundColor Cyan
If (!($NoDialog)) {
Show-MessageDialog -Title "$DoneTitle" -Message "$DoneMessage"
}
return $DoneMessage
}
<#
Example:
Install-Software -Name "Brave Browser" -Packages "BraveSoftware.BraveBrowser"
Install-Software -Name "Brave Browser" -Packages "BraveSoftware.BraveBrowser" -NoDialog
Install-Software -Name "Multiple Packages" -Packages @("Package1", "Package2", "Package3", ...) -PackageProvider 'Chocolatey'
Install-Software -Name "Multiple Packages" -Packages @("Package1", "Package2", "Package3", ...) -PackageProvider 'Chocolatey' -NoDialog
Install-Software -Name "Multiple Packages" -Packages @("Package1", "Package2", "Package3", ...) -PackageProvider 'MsStore'
Install-Software -Name "Multiple Packages" -Packages @("Package1", "Package2", "Package3", ...) -PackageProvider 'MsStore' -NoDialog
Install-Software -Name "Ubuntu" -Packages "Ubuntu" -PackageProvider 'WSL'
Install-Software -Name "Ubuntu" -Packages "Ubuntu" -PackageProvider 'WSL' -NoDialog
Uninstall-Software -Name "Brave Browser" -Packages "BraveSoftware.BraveBrowser"
Uninstall-Software -Name "Brave Browser" -Packages "BraveSoftware.BraveBrowser" -NoDialog
Uninstall-Software -Name "Multiple Packages" -Packages @("Package1", "Package2", "Package3", ...) -PackageProvider 'Chocolatey'
Uninstall-Software -Name "Multiple Packages" -Packages @("Package1", "Package2", "Package3", ...) -PackageProvider 'Chocolatey' -NoDialog
Uninstall-Software -Name "Multiple Packages" -Packages @("Package1", "Package2", "Package3", ...) -PackageProvider 'MsStore'
Uninstall-Software -Name "Multiple Packages" -Packages @("Package1", "Package2", "Package3", ...) -PackageProvider 'MsStore' -NoDialog
Uninstall-Software -Name "Ubuntu" -Packages "Ubuntu" -PackageProvider 'WSL'
Uninstall-Software -Name "Ubuntu" -Packages "Ubuntu" -PackageProvider 'WSL' -NoDialog
#>