-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgramModule.psm1
More file actions
88 lines (81 loc) · 2.86 KB
/
Copy pathProgramModule.psm1
File metadata and controls
88 lines (81 loc) · 2.86 KB
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
function Get-ProgramInstalled {
param (
[Parameter(Mandatory=$true)]
[string]$ProgramName
)
try {
return $(Get-Package -Name $ProgramName -ErrorAction Stop).Count -gt 0
} catch {
if ($_.Exception.Message -like "No package found for '*'.") {
return $false
} else {
Write-Host "An error occurred in function ""Get-ProgramInstalled"", exiting with exception message ""$($_.Exception.Message)"""
exit 1
}
}
}
function Get-ChocolateyAvailable {
param ()
try {
choco --noop | Out-Null
return $true
} catch {
return $false
}
}
function Ensure-ChocolateyInstalled {
param ()
if ($(Get-ChocolateyAvailable) -eq $false) {
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
}
if ($(Get-ChocolateyAvailable) -eq $false) {
Write-Host "Chocolatey not available after running installer. Exiting."
exit 1
}
}
function Get-AllowGlobalConfirmationState {
param ()
$allowGlobalConfirmationState = choco feature get allowGlobalConfirmation --limit-output
if ($allowGlobalConfirmationState -eq "Enabled") {
return $true
} elseif ($allowGlobalConfirmationState -eq "Disabled") {
return $false
}
Write-Host "Couldn't explicitly determine state of Chocolatey ""allowGlobalConfirmation"" config. Exiting."
exit (1)
}
function Ensure-AllowGlobalConfirmationEnabled {
param ()
$state = Get-AllowGlobalConfirmationState
if ($state -eq $false) {
if ($(choco feature enable allowGlobalConfirmation --limit-output) -ne "Enabled allowGlobalConfirmation") {
Write-Host "An error occured when enabling ""allowGlobalConfirmation"" in Chocolatey"
exit 1
}
}
}
#
function PrepareChocolatey {
param ()
Ensure-ChocolateyInstalled
Ensure-AllowGlobalConfirmationEnabled
try {
choco upgrade chocolatey --no-progress --nocolor --limit-output
} catch {
Write-Host "An error occurred in function ""PrepareChocolatey"", exiting with exception message ""$($_.Exception.Message)"""
}
}
# This function has no validation and does nothing to confirm it actually installed.
# It is simply a best-effort function, relying on usage of this function and inputs being tested
function Install-ChocolateyProgram {
param (
[Parameter(Mandatory=$true)]
[string]$ProgramName
)
try {
choco install $ProgramName --no-progress --nocolor --limit-output
} catch {
Write-Host "An error occurred when trying to install Chocolatey program $ProgramName. Exiting."
exit 1
}
}