-
Notifications
You must be signed in to change notification settings - Fork 3
/
functions.ps1
137 lines (130 loc) · 4.21 KB
/
functions.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
function Write-Status
{
process
{
if ($_.Status -eq 'Ok')
{
Write-Output "`u{2713}"
}
else
{
Write-Output "$($_.Status) $($_.ExtendedErrorCode)"
}
}
}
function Install-PackageWithStatus (
[Parameter(Mandatory = $true)]
[string]$packageName,
[Parameter(Mandatory = $true)]
[string]$packageId,
[switch]$user,
[switch]$cli
)
{
Start-WithStatus "Installing $packageName" {
try
{
if ($user)
{
if ($cli)
{
winget install --accept-package-agreements --accept-source-agreements --disable-interactivity --id $packageId -h --scope user
}
else
{
($psResult = Install-WinGetPackage -Scope UserOrUnknown -Mode Silent -MatchOption EqualsCaseInsensitive -Force -Id $packageId) | Write-Status
}
}
else
{
if ($cli)
{
winget install --accept-package-agreements --accept-source-agreements --disable-interactivity --id $packageId -h --scope machine
}
else
{
($psResult = Install-WinGetPackage -Scope SystemOrUnknown -Mode Silent -MatchOption EqualsCaseInsensitive -Force -Id $packageId) | Write-Status
}
}
if ($cli -and $LASTEXITCODE -ne 0)
{
Write-Warning "WinGet CLI returned exit code $LASTEXITCODE - attempting install without scope"
winget install --accept-package-agreements --accept-source-agreements --disable-interactivity --id $packageId -h
}
elseif (-not $cli -and $psResult.Status -ne 'Ok')
{
Write-Warning "WinGet Powershell returned status '$($psResult.Status)' - attempting install with CLI"
if ($user)
{
Install-PackageWithStatus -packageName "$packageName (no-scope)" -packageId $packageId -cli -user
}
else
{
Install-PackageWithStatus -packageName "$packageName (no-scope)" -packageId $packageId -cli
}
}
}
catch
{
if (-not $cli)
{
Write-Warning "Error installing '$packageName': $($_.Exception.Message) - attempting install with CLI"
if ($user)
{
Install-PackageWithStatus -packageName "$packageName (cli)" -packageId $packageId -cli -user
}
else
{
Install-PackageWithStatus -packageName "$packageName (cli)" -packageId $packageId -cli
}
}
else
{
Write-Error "Error installing '$packageName': $($_.Exception.Message)"
}
}
}
}
function Start-WithStatus (
[Parameter(Mandatory = $true)]
[string]$status,
[Parameter(Mandatory = $true)]
[scriptblock]$scriptBlock
)
{
Write-Host "$status..."
& $scriptBlock > $null
Write-Host "$status - Done."
}
function UnpinFrom-Taskbar ([string]$appname)
{
try
{
((New-Object -Com Shell.Application).NameSpace('shell:::{4234d49b-0245-4df3-b780-3893943456e1}').Items() | Where-Object { $_.Name -eq $appname }).Verbs() | Where-Object { $_.Name.replace('&', '') -match 'Unpin from Taskbar' } | ForEach-Object { $_.DoIt() }
return "App '$appname' unpinned from Taskbar"
}
catch
{
Write-Error "Error Unpinning '$appname': $($_.Exception.Message)"
}
}
function Set-RegistryKeyValue([string]$Path, [string]$Name, $Value)
{
try
{
if (!(Test-Path $Path))
{
$regKey = New-Item -Path $Path
}
else
{
$regKey = Get-Item -Path $Path
}
$regKey | Set-ItemProperty -Name $Name -Value $Value -Force > $null
return "Registry key '$Path' value '$Name' set to '$Value'"
}
catch
{
Write-Error "Error setting registry key '$Path' value '$Name' to '$Value': $($_.Exception.Message)"
}
}